【问题标题】:Call some methods from interface without override all the methods in JAVA从接口调用一些方法而不覆盖JAVA中的所有方法
【发布时间】:2013-01-19 18:33:31
【问题描述】:

朋友们,我在 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 
}

谁能为此提出一些解决方案?

是否有任何可用的设计模式?如果有,请提供参考链接。

提前致谢。

【问题讨论】:

  • I dont want to override all the methods from interface 你不能从接口覆盖方法,你只能实现它们(你必须实现所有,这是Java的设计)

标签: java class design-patterns interface dependency-injection


【解决方案1】:

接口是合约。它说“我的班级实现了这些方法中的所有”。

见:http://docs.oracle.com/javase/tutorial/java/concepts/interface.html

如果您不希望这样,请不要使用接口。

【讨论】:

    【解决方案2】:

    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
        }
    }
    

    【讨论】:

      【解决方案3】:

      当你实现接口时,除了实现接口中的所有方法之外别无他法。在上面的“samp”类中,你实现了“samp1”和“samp2”接口,所以你必须在samp类中实现samp1和samp2中的所有方法。

      你说从接口覆盖方法。您不会覆盖接口中的方法,您只需实现方法。

      你可以使用抽象类来解决你的问题。

      abstract class AbstractSamp implement samp1,samp2{
      
       method1(){...}
       method4(){...}
      }
      

      您可以在您的 samp 类中扩展 Abstractsamp,如下所示

      class samp extends AbstractSamp{
      
      // here you can extend method1() and method4() by inheritence and you can also    override   them as you want
      
      }
      

      【讨论】:

        【解决方案4】:

        将类声明为abstract,您不需要实现所有方法。但是,请注意抽象类不能被实例化。

        【讨论】:

          【解决方案5】:

          如果没有更多关于您要实现的目标的背景信息,我建议您这样做:

          interface BaseSamp {
              void method1();
              void method4();
          }
          
          interface Samp1 extends BaseSamp {
               void method2();
               void method3();
          }
          
          interface Samp2 extends BaseSamp {
              void method5();
          }
          
          class YourClass implements BaseSamp {
           ....
          }
          

          【讨论】:

            【解决方案6】:
            interface MySpecializedInterface{
               public void method1();
               public void method4();
            }
            
            class YourClass implements MySpecializedInterface{
            
              // now you can implement method1 and method4... :|
            }
            

            【讨论】:

              猜你喜欢
              • 2022-01-01
              • 2014-07-17
              • 2016-08-12
              • 1970-01-01
              • 1970-01-01
              • 2021-08-12
              • 2014-11-20
              • 2014-05-23
              • 1970-01-01
              相关资源
              最近更新 更多