Java中interface的重写和代码复用

技术分享
336 0

interface

interface中默认是调用的他重写的方法,但是如果父类接口中写了默认的话,并且没被重写才调用的是interface中默认的内容

public interface interfaceTest {
    default void say(){
        System.out.println("hi");  //设置了默认方法,如果没被重写则会调用这个方法
    };
}
@Service
public class interfaceTests implements interfaceTest{
    @Override //将interface进行重写
    public void say() {
        System.out.println("interface Test");
    }
}
@RestController
public class interfaceTest {
    @Autowired
    interfaceTest interfaceTest;
    @GetMapping("interfaceTest")
    public void testinterface() {
        interfaceTest.say()  //调用方法则是执行重写的方法
    }
}

调用方法则是执行重写的方法,如果被多次重写的话,一般都调用指定的那个重写的子接口的,或者传入的是interface的集合类,在进行使用索引指定元素

@Autowired
AnimalInterface[] animalInterfaces; //将符合的类接口都放到这个数组里

@RequestMapping(value = "test",method = RequestMethod.GET)
@ResponseBody
String test(){
    cat01.say();
    System.out.println(Arrays.toString(animalInterfaces));
    animalInterfaces[0].say();
    animalInterfaces[1].say();
    return "test";
}

第二个用法就是,子接口可以调用父接口中默认的方法(如果父接口没写具体方法的话,则是需要子接口进行重写的)

public interface interfaceTest {
    default void say(){
        System.out.println("hi");  //设置了默认方法,如果没被重写则会调用这个方法
    };
}

如果接口只被重写了一次的话,用户调用这个父接口中的内容可以自动使用重写的那个方法,当然用户调用其他接口其他接口继承这个子接口的话也可以调用到重写的这个方法

interface ParentInterface {
    void method();
}

interface ChildInterface extends ParentInterface {
    @Override
    default void method() {
        System.out.println("ChildInterface method");
    }
}

class MyClass implements ChildInterface {
}

public class Main {
    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        myClass.method();  // 输出:ChildInterface method
        ParentInterface parentInterface = new MyClass();
        
        parentInterface.method();  // 输出:ChildInterface method
    }
}

interface

interface中默认是调用的他重写的方法,但是如果父类接口中写了默认的话,并且没被重写才调用的是interface中默认的内容

public interface interfaceTest {
    default void say(){
        System.out.println("hi");  //设置了默认方法,如果没被重写则会调用这个方法
    };
}
@Service
public class interfaceTests implements interfaceTest{
    @Override //将interface进行重写
    public void say() {
        System.out.println("interface Test");
    }
}
@RestController
public class interfaceTest {
    @Autowired
    interfaceTest interfaceTest;
    @GetMapping("interfaceTest")
    public void testinterface() {
        interfaceTest.say()  //调用方法则是执行重写的方法
    }
}

调用方法则是执行重写的方法,如果被多次重写的话,一般都调用指定的那个重写的子接口的,或者传入的是interface的集合类,在进行使用索引指定元素

@Autowired
AnimalInterface[] animalInterfaces; //将符合的类接口都放到这个数组里

@RequestMapping(value = "test",method = RequestMethod.GET)
@ResponseBody
String test(){
    cat01.say();
    System.out.println(Arrays.toString(animalInterfaces));
    animalInterfaces[0].say();
    animalInterfaces[1].say();
    return "test";
}

第二个用法就是,子接口可以调用父接口中默认的方法(如果父接口没写具体方法的话,则是需要子接口进行重写的)

public interface interfaceTest {
    default void say(){
        System.out.println("hi");  //设置了默认方法,如果没被重写则会调用这个方法
    };
}

如果接口只被重写了一次的话,用户调用这个父接口中的内容可以自动使用重写的那个方法,当然用户调用其他接口其他接口继承这个子接口的话也可以调用到重写的这个方法

interface ParentInterface {
    void method();
}

interface ChildInterface extends ParentInterface {
    @Override
    default void method() {
        System.out.println("ChildInterface method");
    }
}

class MyClass implements ChildInterface {
}

public class Main {
    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        myClass.method();  // 输出:ChildInterface method
        ParentInterface parentInterface = new MyClass();
        
        parentInterface.method();  // 输出:ChildInterface method
    }
}
最后更新 2023-08-16
评论 ( 0 )
OωO
隐私评论