什么是多态
1.即同一方法可以根据发送对象的不同而采用多种不同的行为方式
2.一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多
3.多态存在的条件:
- 有继承关系
- 子类重写父类方法
- 父类引用指向子类对象
- 4.注意:多态是方法的多态,属性没有多态性
- 5.instanceof
理解:父类引用指向子类对象,父类可以调用自己的方法,和子类重写的方法,不可以调用子类独有的方法
//父类
public class Person {
public void run(){
System.out.println("run");
}
}
===================================分开===================================
//子类
public class Student extends Person {
@Override //重写父类方法
public void run() {
System.out.println("sou");
}
public void eat(){
System.out.println("eat");
}
}
===================================分开===================================
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();
s2.run(); //子类重写父类的方法,执行子类的方法 //sou
s1.run(); //sou
//对象能执行那些方法,主要看对象左边的类型,和右边关系不大
//s2.eat();
((Student)s2).eat(); //强制转换,高转底
s1.eat();
}
}
分析:其实就是子类重写父类的方法,然后父类引用指向子类,然后父类可以使用子类的方法
多态的注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系,类型转换异常 (ClassCastException 类型转换异常)
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();
instanceof和类型转换
1.instanceof,判断是否有继承关系
//父类
public class Person {
}
===================================分开===================================
//子类
public class Student extends Person {
}
===================================分开===================================
//子类
public class Teacher extends Person{
}
===================================分开===================================
//测试类
public class Application {
public static void main(String[] args) {
Object o = new Student();
System.out.println(o instanceof Student); //true
System.out.println(o instanceof Person);//true
System.out.println(o instanceof Object);//true
System.out.println(o instanceof Teacher);//false
System.out.println(o instanceof String);//false
System.out.println("===================================");
Person p = new Student();
System.out.println(p instanceof Student); //true
System.out.println(p instanceof Person);//true
System.out.println(p instanceof Object);//true
System.out.println(p instanceof Teacher);//true
//System.out.println(p instanceof String);//编译报错
System.out.println("===================================");
Student s = new Student();
System.out.println(s instanceof Student); //true
System.out.println(s instanceof Person);//true
System.out.println(s instanceof Object);//true
//System.out.println(s instanceof Teacher);//编译报错
//System.out.println(s instanceof String);//编译报错
}
}
使用instanceof判断是否有继承关系
System.out.println(对象 instanceof 类名);
2.多态的类型转换
//父类
public class Person {
public void run(){
System.out.println("run");
}
}
===================================分开===================================
//子类
public class Student extends Person {
public void go(){
System.out.println("go");
}
}
===================================分开===================================
//测试类
public class Application {
public static void main(String[] args) {
//类型之间的转换 : 基本类型转换 高转底需要强制转换 父 子
Person obj = new Student();
//student.go(); 需要强制转换
//将Person的对象student,转换为为Student类型,我们就可以使用Student类型的方法了
((Student)obj).go();
//子类转父类,可能丢失自己本来的一些方法
Student student = new Student();
Person person = student; //不能go方法
}
}
1.父类引用指向子类的对象
2.把子类转换为父类,向上转型 (子类转父类,可能丢失自己本来的一些方法)
3.把父类转换为子类,向下转型 :强制转换
4.方便方法的调用,减少重复的代码!简洁