如何使子类仅从实例访问?

huangapple go评论74阅读模式
英文:

How to make a subclass accessible from instance only?

问题

我想创建一个子类只能从超类的实例访问而不能从超类自身访问以确保超类的变量已被初始化

例如

public class SuperClass
{
     int num;
     
     SuperClass(int number){
         num = number;
     }
     // 仅从实例访问 \/ 
     class SubClass
     {
         SubClass(){}

         public int read(){
             return num;
         }
     }
}
在另一个文件中

public void err(){
    SuperClass.SubClass obj = new SuperClass.SubClass(); // 错误!超类不是一个实例
    System.out.println(obj.read());
}
public void right(){
    SuperClass sup = new SuperClass(3);
    SuperClass.SubClass obj = new sup.SubClass(); // 正确,sup 是一个实例
    System.out.println(obj.read()) // 打印 3
英文:

I'd like to create a subclass accessible only from an instance of the superclass, but not from the superclass itself, to be sure that the superclass' variables have been initialized.
For example:

public class SuperClass 
{
     int num;
     
     SuperClass(int number){
         num = number;
     }
     //Make this\/ accessible from instance only
     class SubClass
     {
         SubClass(){}

         public int read(){
             return num;
         }
     }
}

In another file:

public void err(){
    SuperClass.SubClass obj = new SuperClass.SubClass(); //Error! Superclass is not an instance
    System.out.println(obj.read());
}
public void right(){
    SuperClass sup = new SuperClass(3);
    SuperClass.SubClass obj = new sup.SubClass(); //Correct, sup is an instance
    System.out.println(obj.read()) //Print 3

答案1

得分: 1

这是不可能的。非静态内部类必须通过外部类的实例进行实例化。请查看此链接了解更多信息。

英文:

That's not possible. Non-static inner classes have to be instantiated through an instance of an outer class. See this link for more info.

答案2

得分: 0

内部类

与实例方法和变量一样,内部类与其封闭类的实例相关联,并直接访问该对象的方法和字段。另外,由于内部类与实例相关联,它本身不能定义任何静态成员。

作为内部类实例的对象存在于外部类实例中。考虑以下类:

class OuterClass {
    ...
    class InnerClass {
        ...
    }
}

**InnerClass 的实例只能存在于 OuterClass 的实例中,并且可以直接访问其封闭实例的方法和字段。
要实例化内部类,必须首先实例化外部类。**然后,使用以下语法在外部对象中创建内部对象:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

有两种特殊类型的内部类:局部类和匿名类。

查看链接

英文:

Inner Classes

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

class OuterClass {
...
class InnerClass {
...
}
}

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.
To instantiate an inner class, you must first instantiate the outer class.
Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();
There are two special kinds of inner classes: local classes and anonymous classes.

check the link

huangapple
  • 本文由 发表于 2020年10月4日 03:27:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64188181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定