有没有办法让 Java 类知道它的实例化器?例如:
public class Foo() {
public Foo() {
// can I get Bar.myInteger from here somehow
// without passing it in to the constructor?
}
}
public class Bar {
private int myInteger;
public Bar() {
myInteger = 0;
Foo foo = new Foo();
}
}
最佳答案
您是否有任何特殊原因不想在构造函数中传递任何内容?
简而言之,这违反了封装原则……可能还违反了其他几个原则。
关于java - Java 类可以知道它的实例化器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2357334/