<分区>
我想知道如何选择、操作创建对象的类。
代码:
public myclass(){
public anotherclass a = new anotherclass();
}
另一个类:
//how to use the class that created this class ?
标签 java
<分区>
我想知道如何选择、操作创建对象的类。
代码:
public myclass(){
public anotherclass a = new anotherclass();
}
另一个类:
//how to use the class that created this class ?
最佳答案
基本上,你不能。如果您的其他类需要知道创建它的实例或类,您应该通过构造函数传递该信息。例如:
public class Parent {
private final Child child;
public Parent() {
child = new Child(this);
}
}
public class Child {
private final Parent parent;
public Child(Parent parent) {
this.parent = parent;
}
}
(这使得父实例可用于子级 - 如果您只对类感兴趣,您将传递 Parent.class
和 Child
构造函数将使用 Class<?> parentClass
参数。
关于创建对象的 Java Call 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18096545/