java 从接口调用一些方法而不覆盖JAVA中的所有方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14417450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call some methods from interface without override all the methods in JAVA
提问by user1309724
Friends, I have an issue in Java: I'd like to implement one structure but I'm facing some difficulty in doing it, can anyone help me.
朋友们,我在 Java 中有一个问题:我想实现一个结构,但我在实现它时遇到了一些困难,任何人都可以帮助我。
interface samp1{
method1()
method2()
method3()
}
interface samp2{
method4()
method5()
}
class Samp implements samp1,samp2
{
// this class needs only method1 from interface samp1 and method 4 from interface samp2
// I don't want to override all the methods from interface
}
can anyone propose some solutions for this?
谁能为此提出一些解决方案?
Is there any design pattern available for this? If so, please provide links to reference.
有没有可用的设计模式?如果有,请提供参考链接。
Thanks in advance.
提前致谢。
回答by Brian Roach
An interface is a contract. It says "My class implements allof these methods".
一个接口就是一个契约。它说“我的班级实现了所有这些方法”。
See: http://docs.oracle.com/javase/tutorial/java/concepts/interface.html
请参阅:http: //docs.oracle.com/javase/tutorial/java/concepts/interface.html
If you don't want that, don't use an Interface.
如果您不想要那样,请不要使用接口。
回答by Kartik Soneji
Java 8 allows default methods in an interface, that are used if the particular method is not overridden when the interface is implemented. For example:
Java 8 允许接口中的默认方法,如果在实现接口时未覆盖特定方法,则使用默认方法。例如:
interface MyInterface{
//if a default method is not provided, you have to override it
public int Method1();
public default int Method2(){
return 2;
}
}
public class MyClass{
public static void main(String args[]){
MyInterface in = new MyInterface(){
public int Method1(){
return 0;
}
};
//uses the implemented method
System.out.println(in.Method1()); //prints 0
//uses the default method
System.out.println(in.Method2()); // prints 2
}
}
回答by Aniket Inge
interface MySpecializedInterface{
public void method1();
public void method4();
}
class YourClass implements MySpecializedInterface{
// now you can implement method1 and method4... :|
}
回答by ylnsagar
There is no other way than to implement all the methods in the interface, when you implement the interface. In the above class "samp" you are implementing "samp1" and "samp2" interface's, so you have to implement all the methods in samp1 and samp2 in samp class.
当您实现接口时,除了实现接口中的所有方法之外别无他法。在上面的“samp”类中,您正在实现“samp1”和“samp2”接口,因此您必须在 samp 类中实现 samp1 和 samp2 中的所有方法。
you said override methods from the interface. you don't override methods from interface, you just implement methods.
你说从接口覆盖方法。你不会从接口覆盖方法,你只是实现方法。
you can solve your problem by using abstract class.
您可以通过使用抽象类来解决您的问题。
abstract class AbstractSamp implement samp1,samp2{
method1(){...}
method4(){...}
}
and you can extend Abstractsamp in your samp class as below
你可以在你的 samp 类中扩展 Abstractsamp 如下
class samp extends AbstractSamp{
// here you can extend method1() and method4() by inheritence and you can also override them as you want
}
回答by Dan D.
Declare the class as abstract
and you won't need to implement all the methods. However, please note that abstract classes cannot be instantiated.
将类声明为abstract
,您将不需要实现所有方法。但是,请注意抽象类不能被实例化。
回答by jlordo
Without having more context on what you are trying to achieve, this is what I suggest to do:
如果没有更多关于您要实现的目标的背景信息,我建议您这样做:
interface BaseSamp {
void method1();
void method4();
}
interface Samp1 extends BaseSamp {
void method2();
void method3();
}
interface Samp2 extends BaseSamp {
void method5();
}
class YourClass implements BaseSamp {
....
}