简体   繁体   English

Java转换为类的接口

[英]Java cast interface to class

I have a question about interface and class implementing interface. 我有一个关于接口和类实现接口的问题。

This is my code: 这是我的代码:

interface iMyInterface {
    public iMethod1();
}

public class cMyClass implements iMyInterface {
    public iMethod1() {
        // some code
    }
    protected iMethod2() {
        // some code
    }
}

I would like to create an instance of iMyInterface as this : 我想创建一个iMyInterface的实例,如下所示:

iMyInterface i = new cMyClass();
i.iMethod1();

It's ok, but how can I call iMethod2() from my interface instance? 没关系,但我如何从界面实例中调用iMethod2() Is this working and safe: 这是否有效且安全:

((cMyClass)i).iMethod2();

Thanks for help. 感谢帮助。

Yes, that will work (if you change the declaration of cMyClass to implement iMyInterface ) and it's safe so long as the reference really does refer to an instance of cMyClass . 是的,就可以了(如果你改变的声明cMyClass实现iMyInterface ),并作为参考确实是指的一个实例,它是安全的,只要cMyClass

However, it's a generally bad idea. 然而,这通常是个坏主意。 The whole point of using an interface is to be able to work with any implementation - it's to separate the abstraction from the implementation. 使用接口的全部意义是能够使用任何实现 - 它将抽象与实现分开。 If you're then going to require a specific implementation, you might as well make the type of i just cMyClass to start with. 如果你然后将需要一个具体的实现,你还不如让类型i只是cMyClass开始。

So suppose instead of calling the cMyClass constructor yourself, you receive a method parameter of type iMyInterface - it's a bad idea to cast to cMyClass at that point, as it could be a different implementation of the interface. 因此,假设,而不是调用cMyClass构造器,你自己,你收到类型的方法参数iMyInterface -这是一个坏主意,转换为cMyClass在这一点上,因为它可能是一个不同的接口的实现。

(On a separate note, it's a good idea to start following Java naming conventions, which state that classes and interfaces should be Pascal-cased - so ditch the c and i prefixes.) (另外一点,最好开始遵循Java命名约定,它规定类和接口应该是Pascal-cased - 所以抛弃ci前缀。)

There is some another alternative to cast Interface to a class. 还有一些替代方法可以将接口转换为类。 Here is example how. 这是如何示例。

interface iMyInterface {
   void iMethod1();
}

public class cMyClass implements iMyInterface {

private iMyInterface myInterface;

public cMyClass() {
    myInterface = this;
}

public void iMethod1(){
    System.out.println("Print from cMyClass iMethod1()");
}

protected void iMethod2() {
    System.out.println("Print from cMyClass iMethod2()");
}

/**
 * Getter so we can access to the interface methods.
 * @return
 */
public iMyInterface getMyInterface() {
    return myInterface;
}
}

And to get values from your interface, here is the code example. 要从您的界面获取值,这里是代码示例。

public class Main {
public static void main(String[] args) {

    cMyClass myClass = new cMyClass();
    myClass.getMyInterface().iMethod1();
    myClass.iMethod2();
}
}

Output: 输出:

Print from cMyClass iMethod1() 从cMyClass打印iMethod1()

Print from cMyClass iMethod2() 从cMyClass打印iMethod2()

I think this is a good example how you can separate interface code from the class code. 我认为这是一个很好的例子,您可以将接口代码与类代码分开。 Just create instance of the interface and every method use through getter with that interface. 只需创建接口的实例,并通过该接口的getter使用每个方法。

It will work (provided that cMyClass implements iMyInterface and you are in scope of protected modifier) but that is not the correct OO approch. 它将起作用(假设cMyClass实现了iMyInterface,并且你在受保护的修饰符的范围内),但这不是正确的OO approch。

If you want to use iMethod2 consider: 如果你想使用iMethod2考虑:

  • adding it to the interface 将其添加到界面
  • create another interface containing that method 创建包含该方法的另一个接口
  • Use cMyClass myClass = new cMyClass(); 使用cMyClass myClass = new cMyClass();

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

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