简体   繁体   English

java 接口方法从子类中移除

[英]java interface method removing from subclass

Anyone provide suggestion for below mentioned:任何人提供以下提到的建议:

in java8 consider an interface having two methods (eg interface1,interface 2) implementing those to many subclass later i want to remove one method interface1 from one of my subclass without affecting other is any possible solution is there?在 java8 中,考虑一个接口有两个方法(例如 interface1,interface 2),稍后实现这些方法到许多子类我想从我的一个子类中删除一个方法 interface1 而不会影响其他有任何可能的解决方案吗?

If your subclass declares that it implements this interface, then you have no choice but to provide implementations for all methods, or declare the class abstract.如果您的子类声明它实现了此接口,那么您别无选择,只能为所有方法提供实现,或者声明 class 抽象。 If you want a concrete class which however does not functionally implement all methods in the interface, then here is one option:如果你想要一个具体的 class 但它不能在功能上实现接口中的所有方法,那么这里是一个选项:

public interface YourInterface {
    void method1();
    void method2();
}

public class YourSubClass implements YourInterface {
    @Override
    public void method1() {
        // actually do something
    }

    @Override
    public void method2() {
        throw new UnsupportedOperationException("method2() is not supported here.");
    }
}

Here while we do implement all methods, we throw a runtime exception should a caller try to access method2() .在这里,虽然我们确实实现了所有方法,但如果调用者尝试访问method2() ,我们会抛出运行时异常。

You can do this by providing a default method implementation for the interface1 method in the interface itself.您可以通过在接口本身中为interface1方法提供默认方法实现来做到这一点。

interface Interface {
    default void interface1() {
        System.out.println("interface1");
    }

    void interface2();
}

class Clazz implements Interface {
    @Override
    public void interface2() {
        System.out.println("interface2");
    }
}

Depends how you define 'remove one method'.取决于您如何定义“删除一种方法”。

If you have interface如果你有接口

interface Interface{
    void interface1();
    void interface2();
}

And for example two subclasses that extend it:例如,扩展它的两个子类:

 class Class1 implementes Interface {
     @Override
     public void interface1(){ ... }
     @Override
     public void interface2(){ ... }
 }
 class Class2 implementes Interface {
     @Override
     public void interface1(){ ... }
     @Override
     public void interface2(){ ... }
 }

Then there are two scenarios:那么有两种情况:

  1. You don't want to implement for example interface1() method in Class2:您不想在 Class2 中实现例如 interface1() 方法:
  2. You don't want to have interface1() method in Class2您不想在 Class2 中有 interface1() 方法

In case of 1. as Robby Cornelissen mentioned, you can simply provide default implementation in Interface:在 1. 如 Robby Cornelissen 提到的情况下,您可以简单地在接口中提供默认实现:

default void interface1() { /*do default thing*/ }

In case of 2. you need to remove the interface1() method from the Interface.在 2. 的情况下,您需要从接口中删除 interface1() 方法。

You can do that by simple moving definition of method interface1() to Class1 (and any other sublass that needs to have it).您可以通过将方法 interface1() 的简单定义移动到 Class1 (以及需要它的任何其他子类)来做到这一点。 but that is not really generic approach.但这并不是真正的通用方法。

Best is to extract for example Interface1 with method interface1() and use that interface in classes that need to have that method.最好是使用方法 interface1() 提取例如 Interface1 并在需要具有该方法的类中使用该接口。 You will end up with this situation:你最终会遇到这种情况:

interface Interface{
    void interface2();
}
interface Interface1{
    void interface2();
}

And for example two subclasses that extend it:例如,扩展它的两个子类:

 class Class1 implementes Interface, Interface1 {
     @Override
     public void interface1(){ ... }
     @Override
     public void interface2(){ ... }
 }
 class Class2 implementes Interface {
     @Override
     public void interface2(){ ... }
 }

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

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