简体   繁体   English

如何找出调用方法的接口?

[英]How to find out which interface a method was called on?

I have a simple Java question. 我有一个简单的Java问题。 Consider the following interfaces: 考虑以下接口:

interface A { 
    void work();
    void a();
}

interface B {
    void work();
    void b();
}

So when a class is going to implement them, it should be like this: 所以当一个类要实现它们时,它应该是这样的:

class Impl implements A, B {
    void work() {
        /*some business*/
    }

    void a() {}
    void b() {}
}

My question is, in work method, how would I find out that, it has invoked by type A or B ? 我的问题是,在work方法中,我如何发现它,它是由类型AB调用的?

The above class in C# would be like this, and this separates both implementations very well: C#中的上述类就是这样,这样可以很好地区分两个实现:

class Impl : A, B
{
    void B::work() {}
    void A::work() {}
    void a() {}
    void b() {}
}

But how would I achieve something like C# model in Java?! 但是我如何在Java中实现类似C#模型的东西?!

Thanks in advance. 提前致谢。

Neither. 都不是。 The idea of an interface is that a class that implements it, agrees with the "contract" the interface implies. interface的概念是实现它的类,与接口所暗示的“契约”一致。 If you have two interfaces, requiring both to implement a method work() , and a class that implements both interfaces, then it has to implement work() to agree with the contract of both. 如果你有两个接口,要求实现方法work()和实现两个接口的类,那么它必须实现work()以同意两者的契约。

The JavaDoc says: JavaDoc说:

Implementing an interface allows a class to become more formal about the behavior it promises to provide. 实现接口允许类对其承诺提供的行为变得更加正式。 Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. 接口在类和外部世界之间形成契约,并且该合同在构建时由编译器强制执行。 If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. 如果您的类声称实现了一个接口,那么该接口定义的所有方法必须在其成功编译之前出现在其源代码中。

And that is exactly what you do by implementing a work() method: you satisfy both interfaces A and B . 这正是您通过实现work()方法所做的事情:您同时满足接口AB

Your interface only tells you the signatures of the methods that the implementing class needs to provide. 您的界面仅告诉您实现类需要提供的方法的签名。 In your example both A and B ask for a method work() that has void as return type. 在您的示例中, AB要求方法work()具有返回类型的void So basically they are both asking for the same method. 所以基本上他们都要求采用相同的方法。 I don't see how you could or would need to differentiate? 我不知道你怎么可能或需要区分?

You might have a problem with diamand implementation. 您可能遇到diamand实现问题。 You need to specified it by your own. 你需要自己指定它。

void work() { A.super.work(); } // or B.super.work();

The method work will satisfy the requirements of both interfaces. 方法work将满足两个接口的要求。 The method is contained on the class, which can instantiate both interfaces. 该方法包含在类中,可以实例化两个接口。 In order to implement the interface the class must possess the methods specified in the interface. 为了实现接口,类必须拥有接口中指定的方法。 It does not matter if the same method is used to satisfy the requirements of multiple interfaces. 如果使用相同的方法来满足多个接口的要求并不重要。

JVM will not be invoking A or B, but only the implementation Impl. JVM不会调用A或B,而只调用Impl实现。 You can cast back to A or B and your client can invoke methods based on the methods available in the specific interface. 您可以转换回A或B,并且您的客户端可以根据特定接口中可用的方法调用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM