好记性不如烂笔头-java接口只能声明对象,但不能实例化对象

来源:互联网 发布:词组贝多芬知乎 编辑:程序博客网 时间:2024/06/05 01:13

java接口只能声明对象,但不能实例化对象

interface Person {

    public void fun1();
}

class Student implements Person {
    public void fun1() {
        System.out.println("Studnt Class fun1()");
    }
}

public class TestInterface {
    public static void main(String[] args) {

        Person p = new Student();//接口对象实例化,对象的多态性

//如果是Person p = new Person()的话会报错

        p.fun1();
    }
}
0 0
原创粉丝点击