English 中文(简体)
Java - 如何找到一个目标
原标题:Java - How to find the class an Object is part of
  • 时间:2010-11-09 19:47:25
  •  标签:
  • java

是否在 Java找到某类物体是其中一部分?

class WrapperClass
{
    SomeObject o;
    ...
    someMethodOfWrapperClass()
    { ... }
}

[...]

SomeObject foo = new SomeObject();
WrapperClass wrap = new WrapperClass();
wrap.o = foo;

Is there any way to find the corresponding WrapperClass object wrap from within the SomeObject object or one of it s methods?
Something along the lines of:

foo.getWrapperClassObject().someMethodOfWrapperClass();

非常感谢!

Thomas

问题回答

The short answer is "no".

你可以这样做,因为用户浏览器已经建议将包装类别列入<条码>SomeObject,但是在两个类别之间形成循环关系,<条码>WrapperClass已不再是 /em> 包装类别。

请问why?

In most cases when we create a wrapper class, we are wrapping code which we have no control over, so you wouldn t normally even be able to modify SomeObject s constructor.

你们是否想试图继承? 或许可在<条码>上上延伸<条码>;SomeObject,并增加<条码>一些MethodOfWrapperClass()作为额外行为。

也许这样:

public class ExtendingClass extend SomeObject
{
    public void someMethodOfExtendingClass()
    { ... }
}

// so you can define an instance  foo  which is ALSO an instance of SomeObject
ExtendingClass foo = new ExtendingClass();

// now you can call...
foo.someMethodOfExtendingClass();

你可以有母子关系,告诉奥明(Object)是谁的母子,这样,当你拿到“WrapperClassObject(WrapperClassObject)”时,你就只能把所储存的母子归还。

例:

SomeObject(WrapperClass parent) { this.parent = parent; }

In SomeObject: WrapperClass.this.someMethodOfWrapperClass (

我不能真心想一下你试图做什么是有意义的事......但你可能发现一个内部班子在这样做时有用......。

class Outer
{
    Inner inner = new Inner();

    class Inner
    {
        void foo()
        {
            bar();
        }
    }

    void bar()
    {
        System.out.println("bar");
    }

    public static void main(String[] argv)
    {
        Outer o;

        o = new Outer();
        o.inner.foo();
    }
}

robev的答复基本上相同,但robev具有更一般性的目的答案(Inner在我所贴的法典中提到外包商,它只是隐蔽的,由汇编者创建)。

正如其他人所说的那样,这非常可疑。

实现这一目标有几个途径。

  1. One would be to add a reference to the wrapped SomeObject but that leads to circular dependency like Justin pointed out.
  2. Second, would be to simply make SomeObjectWrapper extends SomeObject but do you even have control over that part of code?
  3. 最终将是制定一项公约,并利用思考来制定守则(我使用的是WrappedObject.name+“Wrapper”):

    package test;
    public class ObjectWrapper {
        Object o;
        public ObjectWrapper(Object o){
        this.o = o;
     }
    }
    
    
    Object o = new Object();
    String pack = "test";
    String name = package+o.getClass().getSimpleName()+"Wrapper";
    Class objWrapper = Class.forName(name);
    
    
    objWrapper.getConstructor(Object.class).newInstance(o);
    

但是,这还远远不足以成为任何用途。 举例说,如果你制定<条码>。 物体 o = 新的Integer 整个事物将从例外处理。

问题没有意义。 Objects是课堂的一部分。 <>References 是课堂的一部分,但指点为t。 多次提到同一目标。 因此,你甚至能够找到任何具体物体的独特参考资料,更不用说提及哪类。





相关问题
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 ...

热门标签