面向对象三大特性-多态详解
![]()
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
//new Person();
//可以指向的引用类型就不确定了:父类引用指向子类对象
//Student 能调用的方法都是自己的或者继承父类的!
Student s1 = new Student();
//Person 父类型,可以指向子类,但不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
//String s4 = new Student();这样不行,有父子关系才可以转换
//s2.run();//子类重写了父类的方法,执行子类的方法
//s1.run();
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大!
((Student)s2).eat();
s1.eat();
}
}
package com.oop.demo06;
public class Person {
public void run(){
System.out.println("run");
}
}
/*
多态注意事项:
1. 多态是方法的多态,属性没有多态
2. 父类和子类有联系 类型转换异常!ClassCastException!
3. 存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();
(方法需要重写:当父类和子类有一样的方法,需要对子类进行重写,调用时看右边,执行子类,不然调用的时候,不知道执行谁的,)
以下三个不能重写,更无法实现多态
1.static 方法,属于类,它不属于实例
2.final 常量
3.private方法
*/
package com.oop.demo06;
public class Student extends Person{
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
instanceof和类型转换
![]()
一
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;
public class Application {
public static void main(String[] args) {
//Object>String
//Object>Person>Teacher
//Object>Person>Student
Object object = new Student();
//System.out.println(X instanceof Y);//能不能编译通过!如果X和Y存在父子关系,就通过,如果不存在就不通过。
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teacher);//false Teacher是Person下的另一条线,和Student无关
System.out.println(object instanceof String);//false
System.out.println("=============================");
Person person = new Student();
System.out.println(person instanceof Student);//true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teacher);//false
//System.out.println(person instanceof String);//编译报错!
System.out.println("=============================");
Student student = new Student();
System.out.println(student instanceof Student);//true
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Object);//true
//System.out.println(student instanceof Teacher);//编译报错!
//System.out.println(student instanceof String);//编译报错!
}
}
二
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;
public class Application {
public static void main(String[] args) {
//类型之间的转换:基本类型的转换 高低64
// 父 子
//高 低
Person obj = new Student();
//obj 将这个对象转换为Student类型,我们就可以使用Student类型的方法了
Student student = (Student) obj;
student.go();
//((Student) obj).go();//写成一句话
//子类转换为父类,可能丢失自己本来的一些方法!
Person person = student;
}
}
/*
1. 父类引用指向子类对象
2. 把子类转换为父类,向上转型
3.把父类转换为子类,向下转型:强制转换
4.方便方法的调用,减少重复的代码,简洁
封装、继承、多态! 抽象类,接口
*/
package com.oop.demo06;
public class Person {
public void run(){
System.out.println("run");
}
}
package com.oop.demo06;
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
![]()