English 中文(简体)
恢复使用某种方法或构造的召唤器(非类别)
原标题:Retrieve the caller instance (not class) of a method or constructor

能否检索一个方法/施工者的打电话?

This question has already been posted, but each time the answers are talking about caller Class (using stacktrace) and not caller instance. If a solution exists, it can be really convenient to build object graph (with a common super type) and handle parent child navigation with default constructor.

public class TestCallStack {
    public static class BaseClass {
        BaseClass owner;
//      //ok, this is the correct way to do it
//      public BaseClass(BaseClass owner) {
//          this.owner = owner;
//      }
        public BaseClass() {
            //this.owner = ???????; 
        }
    }
    public static class Parent extends BaseClass {
        Child child = new Child();
    }
    public static class Child extends BaseClass {
    }

    public static void main(String[] args) {
        Parent parent = new Parent();
        System.out.println(parent.child.owner==parent); // must be true
    }
}
最佳回答

你的感觉是正确的——这是不可能的。 我个人认为,“od是一件事,因为这样会使法典在重新定位方面变得非常脆弱(将一些法典删除为固定方法——突然没有任何电动物体)。

如果你想表达某种所有权人关系,你应明确规定该所有人。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签