转载请注明出处:

  项目中看到有一个类继承父类,并实现了对应的接口,但该类只重写了接口中的方法:

 代码示例如下:

  

package com.study;

import lombok.Data;
@Data
public class FatherTest { 
    private String childen;
    private String son;
    private String test;

    public String getTest() {
        System.out.println("test");
        return test;
    }
    public void setTest(String test) {
        this.test = test;
    } 
}
package com.study;

public interface InterfaceTest {

    String getChilden();
    
    String getSon();
}
package com.study;

public class SonTest extends FatherTest implements InterfaceTest{

    public static void main(String[] args) {
        SonTest son = new SonTest();
        son.getTest();
    }
}

  仔细发现上述示例之后发现,SonTest类并没有重写接口中的方法,因为父类中已经对接口中的方法进行了定义,而且在实例化调用父类和接口中相同方法的时候,父类方法的优先级要高于接口的优先级。

 

相关文章:

  • 2021-08-03
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-08
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-17
  • 2021-10-02
  • 2021-05-23
  • 2021-07-10
相关资源
相似解决方案