我正在审查Java中的内部类的概念。目前为止,从我理解和应用的角度来看,Java内部类与其外部/封闭类的方法和字段有关或可访问。
我的问题:
- 在什么情况下应该创建或定义内部类?
- 内部类是否被认为是“帮助类”?
- 您制作内部类的指标是什么,它们的其他目的是什么?
我正在审查Java中的内部类的概念。目前为止,从我理解和应用的角度来看,Java内部类与其外部/封闭类的方法和字段有关或可访问。
我的问题:
public class InnerClassTester{
public static void main(String []args){
// without the need to instantiate any class
// static class without instantiation
System.out.println(OuterClass.NestedStaticClass1.incrGlobalNum()); // outputs 1
// static class instantiated
OuterClass.NestedStaticClass2 koolObj = new OuterClass.NestedStaticClass2();
// works with instantiation as well
System.out.println(koolObj.incrGlobalNum()); // outputs 2
// inner classes always need to be instantiated
// and they can only be instantiated from within an instance of outer class
// think of them as instance member of outer class and this would make sense
OuterClass.NestedInnerClass1 koolObj2 = new OuterClass().new NestedInnerClass1();
// works with inner classes as well
System.out.println(koolObj2.incrGlobalNum()); // outputs 3
}
}
class OuterClass{
// global variable thats only accessible for select classes (or nested classes)
// we also learn a purpose for private static fields
private static int privateGlobalValue = 0;
// classes to be grouped
// static class
public static class NestedStaticClass1{
// no need to instantiate this class to access/update the global value
public static int incrGlobalNum(){
return ++privateGlobalValue;
}
}
public static class NestedStaticClass2{
// instantiate and still manipulate the global value
public int incrGlobalNum(){
return ++privateGlobalValue;
}
}
// inner class
public class NestedInnerClass1{
// instantiate and still manipulate the global value
public int incrGlobalNum(){
return ++privateGlobalValue;
}
}
}
大多数嵌套类的应用都是基于设计决策。这意味着,每个嵌套类的情况都可以用其他设计替换。
但是话虽如此,我们也可以用组合模式替换继承模式(最近正在获得动力),尽管当类之间的依赖关系非常复杂时,继承模式肯定更好,因为完全组合依赖关系会很丑陋。
案例2:
看一下这段代码:
// no additional classes in the package
public class InterfaceTester{
public static void main(String []args){
// same class returns 2 instances - both compliant to
// either interfaces and yet different output
IShark shark = OuterClass.getSharkInstance();
System.out.println(shark.bite()); // outputs "Die fast bosedk!"
IMosquito mosquito = OuterClass.getMosquitoInstance();
System.out.println(mosquito.bite()); // outputs "Die slow bosedk!"
}
}
interface IShark{
public String bite();
}
interface IMosquito{
public String bite();
}
class OuterClass implements IShark{
// dependency of inner class on private variable
private static String dieSlow = "Die slow bosedk!";
private static String dieFast = "Die fast bosedk!";
private static OuterClass outerInst;
private static InnerClass innerInst;
// private constructor to stop regular instantiation
private OuterClass(){}
// get a shark !
public static IShark getSharkInstance(){
return outerInst != null ? outerInst : new OuterClass();
}
// get a mosquito !
public static IMosquito getMosquitoInstance(){
return innerInst != null ? innerInst : new InnerClass();
}
// an implementation of bite
public String bite(){
return dieFast;
}
// inner class that implements the second interface
private static class InnerClass implements IMosquito{
// different implementation of bite
public String bite(){
return dieSlow;
}
}
}
这种设计决策案例很多,以上所有答案都列举了几个这样的案例。因此,认为这个功能是作为一种新模式而不是作为一个功能或特性引入的并不是错误的。
同时它也能增加封装性,因为内部类可以声明为私有的。
我认为这只是语言的一个特性。如果我们采用OOD并遵守SOLID原则,我不建议使用它。