简体   繁体   English

通过其实例调用接口或抽象类方法

[英]Calling Interface or abstract class method through its instance

如果我有一个接口或抽象类MyClass并且它具有方法getInstance() ,则可以通过它(接口或抽象类)执行以下操作。

MyClass obj=MyClass.getInstance();

getInstance seems to be a static method. getInstance似乎是一个静态方法。 If it is then you can have static method inside an Abstract class but not inside an Interface. 如果是这样,则可以在Abstract类中包含静态方法,但不能在Interface中包含静态方法。 And if your getInstance method returns object of class that is derived from MyClass then you can do that. 并且,如果您的getInstance方法返回派生自MyClass的类的对象,则可以执行此操作。

If you are using Java 8 then interfaces can have static methods. 如果使用Java 8,则接口可以具有静态方法。

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

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