初学java随笔
发布时间:2020-12-15 08:25:17 所属栏目:Java 来源:网络整理
导读:概念:对象的多种形态? 1.引用多态 父类的引用可以指向本类的对象? 父类的引用可以指向子类的对象 2.方法多态 父类型的引用可以指向子类型的对象 Person person = new Student();Student student = new Student(); 思考:两个不同类型的引用person和student
父类型的引用可以指向子类型的对象 Person person = new Student(); Student student = new Student();
student01可以访问对象中所有的非私有的成员 person01只能访问对象中从父类继承下来的非私有成员
自动类型转换
Student student = new Student(); Person person = student; 强制类型转换
Person person = new Student(); Student student = (Student)person;
Person person = new Student(); Teacher teacher = (Teacher)person;
//检测person引用所指向的对象类型是否为Teacher boolean is01 = person instanceof Teacher; System.out.println("is01 = " + is01); // boolean is02 = person instanceof Student; System.out.println("is02 = " + is02);
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |