本文介绍了为什么要用Java公开私有内部类成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果仍无法在包含类的外部访问该私有内部类的成员,则在Java中声明该成员的原因是什么?
What is the reason of declaring a member of a private inner class public in Java if it still can't be accessed outside of containing class? Or can it?
public class DataStructure {
// ...
private class InnerEvenIterator {
// ...
public boolean hasNext() { // Why public?
// ...
}
}
}
推荐答案
如果 InnerEvenIterator
类没有扩展任何类或实现任何接口,我认为这是无稽之谈,因为没有其他类可以访问它的任何实例。
If the InnerEvenIterator
class does not extend any class or implement any interface, I think it is nonsense because no other class can access any instance of it.
但是,如果它扩展或实现了任何其他非私有类或接口,则是有意义的。示例:
However, if it extends or implements any other non private class or interface, it makes sense. An example:
interface EvenIterator {
public boolean hasNext();
}
public class DataStructure {
// ...
private class InnerEvenIterator implements EvenIterator{
// ...
public boolean hasNext() { // Why public?
// ...
}
}
InnerEvenIterator iterator;
public EvenIterator getIterator(){
return iterator;
}
}
这篇关于为什么要用Java公开私有内部类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!