Java 如何实现内部类?
我在那里 我有这样一个场景:Java 如何实现内部类?,java,inner-classes,Java,Inner Classes,我在那里 我有这样一个场景: public class A{ //attributes and methods } public class B{ //attributes and methods } public class C{ private B b; //other attributes and methods } public class D{ private C c1, c2, c3; private List<A> a; //othe
public class A{
//attributes and methods
}
public class B{
//attributes and methods
}
public class C{
private B b;
//other attributes and methods
}
public class D{
private C c1, c2, c3;
private List<A> a;
//other attributes and methods
}
公共A类{
//属性和方法
}
公共B级{
//属性和方法
}
公共C类{
私人B,;
//其他属性和方法
}
D类公共服务{
私人c1、c2、c3;
私人名单a;
//其他属性和方法
}
每个类都有自己的文件。但是,我希望将类A、B和C作为类d的内部类,因为我没有在整个程序中使用它们,只是在其中的一小部分。我应该如何实施它们?我已经读过,但我仍然不确定什么是最好的选择:
选项1,使用静态类:
public class D{
static class A{
//attributes and methods
}
static class B{
//attributes and methods
}
static class C{
private B b;
//other attributes and methods
}
private C c1, c2, c3;
private List<A> a;
//other attributes and methods
}
公共类D{
静态A类{
//属性和方法
}
静态B类{
//属性和方法
}
静态C类{
私人B,;
//其他属性和方法
}
私人c1、c2、c3;
私人名单a;
//其他属性和方法
}
选项2,使用接口和实现它的类
public interface D{
class A{
//attributes and methods
}
class B{
//attributes and methods
}
class C{
private B b;
//other attributes and methods
}
}
public class Dimpl implements D{
private C c1, c2, c3;
private List<A> a;
//other attributes and methods
}
公共接口D{
甲级{
//属性和方法
}
B类{
//属性和方法
}
C类{
私人B,;
//其他属性和方法
}
}
公共类Dimpl实现了D{
私人c1、c2、c3;
私人名单a;
//其他属性和方法
}
我想知道哪种方法更好,以便使用原始场景获得相同的行为。如果我使用选项1并像这样使用类,可以吗
public method(){
List<D.A> list_A = new ArrayList<D.A>();
D.B obj_B = new D.B();
D.C obj_C1 = new D.C(obj_B);
D.C obj_C2 = new D.C(obj_B);
D.C obj_C3 = new D.C(obj_B);
D obj_D = new D(obj_C1, obj_C2, obj_C3, list_A);
}
public方法(){
List_A=新的ArrayList();
D.B obj_B=新的D.B();
D.C obj_C1=新的D.C(obj_B);
D.C obj_C2=新的D.C(obj_B);
D.C obj_C3=新的D.C(obj_B);
D obj_D=新的D(obj_C1、obj_C2、obj_C3、列表A);
}
基本上,我关心的是内部类的创建将如何影响外部类。在最初的场景中,我首先创建类A、B和C的实例,然后创建类D的实例。我可以对我提到的选项执行相同的操作吗?如果您只打算在类内部使用它们,您没有理由使用接口,因为接口是用于
公共访问的。使用第一种方法(并使类私有静态)内部类有两种类型:
静态内部类(称为顶级类)
内部类
静态内部类示例:
public class A {
static int x = 5;
int y = 10;
static class B {
A a = new A(); // Needed to access the non static Outer class data
System.out.println(a.y);
System.out.println(x); // No object of outer class is needed to access static memeber of the Outer class
}
public class A {
int x = 10;
public class B{
int x = 5;
System.out.println(this.x);
System.out.println(A.this.x); // Inner classes have implicit reference to the outer class
}
}
}
内部类示例:
public class A {
static int x = 5;
int y = 10;
static class B {
A a = new A(); // Needed to access the non static Outer class data
System.out.println(a.y);
System.out.println(x); // No object of outer class is needed to access static memeber of the Outer class
}
public class A {
int x = 10;
public class B{
int x = 5;
System.out.println(this.x);
System.out.println(A.this.x); // Inner classes have implicit reference to the outer class
}
}
要创建内部类(而非静态内部类)的对象,首先需要从外部创建外部类对象
A=新的A()
A.B=A.新的B() +1添加private
是我在其他良好的“选项1”实现中唯一需要更改的内容。谢谢。实际上,我将在D之外创建A、B和C的实例。总之,我希望将所有类都放在一个java文件中,但要像放在不同的java文件中一样。@sosegon-如果是这种情况,您可能应该避免,将它们分为不同的文件,这样更好、更清晰、更易于维护。@Binyamin Sharet谢谢。我只是在探索新的方法来完成一些事情。最后,在哪种情况下,静态内部类是有用的?