如何访问“拥有”的实例; java中的方法?
在java中,是否可以在仅给出方法的情况下访问方法所属的实例?
例如:
public class ClassA {
private ClassB instanceB = new ClassB();
// ...
private void sendMethod () {
instanceB.receiveMethod(foo);
}
public void foo () {}
}
public class ClassB {
public void receiveMethod (Method method) {
Object o = foo.getInstanceOwner(); // just made that part up...
}
}
我的感觉是方法属于类,而不是类的实例,所以答案是否定的,但也许有一些我不知道的偷偷摸摸的反射技术。我总是可以将“this”与方法 foo 一起传递,但这似乎是额外的负担。
in java, is it possible to access the instance to which a method belongs, given only the method?
for example:
public class ClassA {
private ClassB instanceB = new ClassB();
// ...
private void sendMethod () {
instanceB.receiveMethod(foo);
}
public void foo () {}
}
public class ClassB {
public void receiveMethod (Method method) {
Object o = foo.getInstanceOwner(); // just made that part up...
}
}
my feeling is that methods belong to classes, not instances of a class, so the answer is no, but maybe there's some sneaky reflection technique i don't know about. i could always pass 'this' along with method foo, but that seems like extra baggage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
取自
您可以调用
Method#invoke
但您需要从方法文档中获取要调用该方法的对象的实例:因此,TL:DR 除非您拥有想要调用该方法的实际对象,否则这是不可能的。
Taken from
You can call
Method#invoke
but you will need the instance of the object you want to call the method on, from the method doc:So the TL:DR is unless you have the actual object you want you call the method on, it is not possible.
}
给你所属的类。实例不拥有方法。
}
gives you the owning Class. An instance doesn't own methods.
您可以这样做,但在您的示例中,正确的方法是使用接口,因为这似乎就是您想要的:您想要传递 ClassB 知道如何操作的对象。
You can do this, but the proper way in your example would be the use of an
interface
, because that seems to be what you want: You want to pass in an object that ClassB knows how to operate on.这就像问:
“给定一个来自苹果园的苹果,哪棵树拥有这个苹果?”
答案是:
“不知道,因为所有苹果树都结苹果,所以它可能属于任何树”。
...换句话说 - 您必须提供一个将调用该方法的实例
编辑
从您的评论中,我推测您正在寻找替代方法观察者模式。您说您不喜欢观察者模式的混乱,并且它对您来说不够“通用”。
我认为它可能是现有的最不混乱的模式之一,并且根据定义,接口是最通用的!
所以,也许这是您遇到的实施问题。幸运的是,我已经在 SO 上发布了 JAVA 中的观察者实现,以展示它是多么强大和优雅。
多态性和接口Java(可以使用多态性来实现接口...为什么?)
事实上:反射比使用接口更混乱,因为你无法在编译时保证您正在调用方法实例的对象,甚至支持该方法! (没有一些错误检查代码)。与接口相比,甚至不可能出现这个问题。
This is like asking:
"Given an apple from an Apple orchard, which tree owns this apple?"
The answer to which is:
"No idea, since all apple trees produce apples, it could belong to any tree".
... in other words - you must supply an instance from which the method will be called
EDIT
From one of your comments, I gather you are looking for an alternative of the Observer pattern. You say you don't like the messiness of the Observer pattern and that it is not "generic" enough for you.
I would argue that it is probably one of the least messiest patterns in existence, AND interfaces are by definition as generic as things get!
So, perhaps its an implementation problem you're having. Luckily, I have already posted on SO an Observer implementation in JAVA, to demonstrate how powerful and elegant it is.
Polymorphism and Interfaces in Java (can polymorphism be used to implement interfaces...why?)
In fact: reflection is messier than using an interface, since you can't guarantee at compile time that the type of Object you are invoking an instance of a Method on, even supports that method! (without some error checking code). Versus with interfaces, its not possible to even have that problem.