java 为什么在java中嵌套抽象类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13437922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why nested abstract class in java
提问by peter
I am wondering what does it mean to have a nested abstract class ? for example,
我想知道嵌套抽象类是什么意思?例如,
abstract class A{
abstract class B{
}
}
Are there any use cases or scenario that we might need such as design ? or is there something useful in such pattern ? and why Java allows us to do it ?
是否有我们可能需要的用例或场景,例如设计?或者在这种模式中有什么有用的东西吗?为什么 Java 允许我们这样做?
回答by Maxim Shoustin
In design, you want the base class class A
to present only an interface for its derived classes. This means, you don't want anyone to actually instantiate an object of the base class. You only want to upcastto it (implicit upcasting, which gives you polymorphic behavior), so that its interface can be used. This is accomplished by making that class abstract using the abstract keyword. In other hand you want to use only part of functionality of class A
so you create class B
(as child) to reduce the coupling or implementation dependencies between systems and prevent duplicates.
在设计中,您希望基类class A
只为其派生类提供一个接口。这意味着,您不希望任何人实际实例化基类的对象。您只想向上转换(隐式向上转换,它为您提供多态行为),以便可以使用它的接口。这是通过使用 abstract 关键字使该类抽象来实现的。另一方面,您只想使用 的部分功能,class A
因此您创建class B
(作为子项)以 减少系统之间的耦合或实现依赖性并防止重复。
But bear in mind when you define an inner class, code without inner classes is more maintainable and readable. When you access private data members of the outer class, the JDK compiler creates package-access member functions in the outer class for the inner class to access the private members. This leaves a security hole. In general we should avoid using inner classes. Use inner class only when an inner class is only relevant in the context of the outer class and/or inner class can be made private so that only outer class can access it. Inner classes are used primarily to implement helper classes like Iterators, Comparators etc which are used in the context of an outer class. About abstract class
, it should be abstract to helpers, suppose your helpersshould be too complicated to write abstract form for them.
但是请记住,当您定义内部类时,没有内部类的代码更易于维护和可读。当您访问外部类的私有数据成员时,JDK 编译器会在外部类中创建包访问成员函数,供内部类访问私有成员。这留下了安全漏洞。一般来说,我们应该避免使用内部类。仅当内部类仅在外部类的上下文中相关和/或内部类可以设为私有以便只有外部类可以访问它时才使用内部类。内部类主要用于实现辅助类,如在外部类的上下文中使用的迭代器、比较器等。关于abstract class
,对助手来说应该是抽象的,假设你的助手为他们写抽象形式应该太复杂了。
In your case, I don't remember extensive usage of nested abstract classes, maybe in Swing
world.
在你的情况下,我不记得嵌套抽象类的广泛使用,也许在Swing
世界上。
回答by Frank Thomas
abstract classes are used to provide a partial implementation of a class for inheritance. it allows you to define the scheme of a class without providing the full definiton, so that it can be specified in a child class. it works somewhat like a Interface in that you can perform any operation specified in the abstract class upon an instance of any classes derived from it. Nested abstracted classes are designed to be inherited by other inner classes (even anonymous ones I think) but not by classes defined outside the outermost class.
抽象类用于为继承提供类的部分实现。它允许您在不提供完整定义的情况下定义类的方案,以便可以在子类中指定它。它的工作方式有点像接口,因为您可以对从抽象类派生的任何类的实例执行抽象类中指定的任何操作。嵌套抽象类旨在由其他内部类(我认为甚至是匿名类)继承,但不能由最外层类之外定义的类继承。
public class HelloEveryone{
abstract class Hello{
void SayHello(){
System.out.println("Hello!");
}
abstract void SayHelloAlt();
}
public class HelloWorld extends Hello{
public void SayHelloAlt(){
System.out.println("HelloWorld!");
}
}
public class HelloUniverse extends Hello{
public void SayHelloAlt(){
System.out.println("HelloUniverse!");
}
}
void Go(){
ArrayList<Hello> hellos = new ArrayList<Hello>();
hellos.add(new HelloWorld());
hellos.add(new HelloUniverse());
for (Hello h : hellos){
h.SayHello();
h.SayHelloAlt();
}
}
}
static void main(){
HelloEveryone hello = new HelloEveryone();
hello.Go();
}