简体   繁体   English

如何在该接口类型的对象上调用实现接口的类中的方法?

[英]How can I call a method which is in a class which implements an Interface, on an object of that interface's type?

I have a class which implements an interface, but has a couple of extra methods. 我有一个实现接口的类,但有一些额外的方法。 I can't edit the interface to add any extra methods and one of the methods of that interface returns a type of the interface itself. 我无法编辑接口来添加任何额外的方法,该接口的一种方法返回一种接口本身。

public interface ExampleInterface{
    public ExampleInterface method1();
}

The class has an extra method I wish to use which will print the contents of the class. 该类有一个我想使用的额外方法,它将打印该类的内容。

public class ExampleInterfaceImpl implements ExampleInterface {
    public ExampleInterface method1() {
        //method code
    }
    public void print() {
        //print method code
    }
}

So my question is if i have an object of type ExampleInterface , ie what is returned by method1() , and I then want to print it using print() , how can that be done seeing that print() is in ExampleInterfaceImpl so can't be called on an object of type ExampleInterface . 所以我的问题是,如果我有一个类型为ExampleInterface的对象,即method1()返回的内容,然后我想用print()打印它,怎么能看到print()ExampleInterfaceImpl中所以可以'在类型为ExampleInterface的对象上调用

Check if the ExampleInterface object is an instanceof ExampleInterfaceImpl . 检查ExampleInterface对象是否是ExampleInterfaceinstanceof ExampleInterfaceImpl Then you can cast it to that and call the method -- ((ExampleInterfaceImpl)object).print(); 然后你可以将它转换为它并调用方法 - ((ExampleInterfaceImpl)object).print(); .

public void maybePrint(ExampleInterface obj) {
    if (obj instanceof ExampleInterfaceImpl) {
        ((ExampleInterfaceImpl)obj).print();
    }
}

This works for specific implementing classes, but not if you have several different implementations that may or may not implement the print() method (in that case, you should create a subinterface with the print() method, and implement that in all the applicable classes). 这适用于特定的实现类,但是如果你有几个不同的实现可能会或可能不会实现print()方法(在这种情况下,你应该使用print()方法创建一个子接口,并在所有适用的实现类)。

It would also be possible to use reflection to check whether the object's class has a print() method, but that's not very elegant, nor recommended. 也可以使用反射来检查对象的类是否有print()方法,但这不是很优雅,也不推荐。

If you really cannot modify the interface, you'll have to resort to runttime type checking and casting: 如果你真的无法修改界面,你将不得不求助于runttime类型检查和转换:

if (myObject instanceof ExampleInterfaceImpl) {
    ((ExampleInterfaceImpl) myObject).print();
}

If you can't change the interface, I suggest you add one which you can. 如果您无法更改界面,我建议您添加一个即可。

interface MyExampleInterface extends ExampleInterface {
    MyExampleInterface method1();
    void print();
}

class MyExampleInterfaceImpl implements MyExampleInterface {

// later
MyExampleInterface mei = new MyExampleInterfaceImpl();
mei.method1().print();

Firstly from comments @RealSkeptic, implementing toString method to your problem is right. 首先来自评论@RealSkeptic,为你的问题实现toString方法是正确的。 If not 如果不

Better design would be to create an another interface which can be Printable like 更好的设计是创建另一个可以像Printable一样的界面

public interface Printable {
    void print();
}

and implement all the classes which can be printable, in your case it is ExampleInterfaceImpl and check the objects type like shown below 并实现所有可以打印的类,在你的情况下它是ExampleInterfaceImpl并检查对象类型,如下所示

if(object instanceof Printable) {
    Printable object = (Printable) object; //this can be ExampleIntefaceImpl object
    object.print(); //is valid
}

If object is stored in interface reference you need to cast that reference to concrete class. 如果object存储在接口引用中,则需要将该引用强制转换为具体类。

      ExampleInterface exampleInterface = new ExampleInterfaceImpl();

    ((ExampleInterfaceImpl)exampleInterface).print();

So my question is if i have an object of type ExampleInterface, ie what is returned by method1(), 所以我的问题是,如果我有一个类型为ExampleInterface的对象,即method1()返回的对象,

you can create the object of class MyExampleInterfaceImpl by referencing type of interface ExampleInterface 您可以通过引用接口ExampleInterface的类型来创建类MyExampleInterfaceImpl的对象

ExampleInterface mei = new MyExampleInterfaceImpl();

when you invoke method1() ie, mei.method(); 当你调用method1() ie, mei.method(); then it simply call the method from your concrete class which has implemented the interface ExampleInterface 然后它只是从已经实现接口ExampleInterface具体类中调用该方法

how can that be done seeing that print() is in 怎么能看到print()在

Simple do explicit cast the refer variable of ExampleInterface into MyExampleInterfaceImpl() then invoke method print() 简单地将ExampleInterface的refer变量显式ExampleInterfaceMyExampleInterfaceImpl()然后调用方法print()

 ((ExampleInterfaceImpl)mei).print();

more on explicit casting 更多关于显式铸造

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

相关问题 如何通过接口从实现接口的类调用方法? - How to call a method from a class which implements an interface, through the interface? 哪个类在运行时实现接口的方法 - Which class implements interface's method at runtime 如何有效控制实现多接口的类对象? - How can I control the class object which implements multi interface effectively? 如果对象实现该接口,该如何调用接口方法? - How do I call an interface method if an object implements that interface? 实现接口的类的类型是什么 - What is the type of a class which implements a interface 哪个类实现了接口操作方法 Perform() - Which class implements Interface Action method Perform() 是否可以在实现接口的 class 中重载方法? - Is it possible to overload a method in a class which implements an Interface? 实现接口方法的类(没有显式实现该接口)是否扩展了该特定接口? - Does a class which implements an interface's method (without explicitly implementing that interface) extend that specific interface? 如何创建实现接口的抽象类对象(JAVA) - How to create abstract class object which implements interface ( JAVA ) 如何使用来自实现父接口的 class 的构造函数初始化接口类型的 object? - How can I initialize an object of type interface with a constructor from a class that implements the parent interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM