2021-5-28,看了几天尚硅谷,实际上这里应该就是多态的体现吧,编译看左边,运行看右边。
通常会看到
List list = new ArrayList();
ArrayList list = new ArrayList();
一般都是上面那种写法,为什么呢
public interface interface1 {
public void function1();
}
public class test implements interface1{
public static void main(String[] args) {
interface1 interface1 = new test();
interface1.function1();
test test = new test();
test.function1();
test.function2();
}
@Override
public void function1() {
System.out.println("接口的方法");
}
public void function2(){
System.out.println("类自己的方法");
}
}
结果为
结论:实现类除了实现接口中所有的抽象方法,可以有自己独特的方法。
用接口接受对象,只能调用接口中的方法,不能调用实现类中特有的方法;
用实现类接受对象,可以调用实现类中特有的方法。