第22天学习打卡
即同一方法可以根据发送对象的不同而采用多种不同的行为方式。
一个对象的实际类型是确定的,但可以指向对象的引用类型有很多。(引用一般指:父类,有关系的类)
多态存在的条件
子类重写父类方法
父类引用指向子类对象
注意 :多态是方法的多态,属性没有多态性。
instanceof: 类型转换, 引用类型转换:判断一个对象是什么类型
package com.oop.demo05;
//继承
public class A extends B{
public static void test(){
System.out.println("A=>test()");
}
}
package com.oop.demo05;
//重写都是方法的重写与属性无关
public class B {
public static void test(){
System.out.println("B=>test()");
}
}
package com.oop;
import com.oop.demo05.A;
import com.oop.demo05.B;
public class Application {
public static void main(String[] args) {
//方法的调用只和左边,定义的数据类型有关
A a = new A();
a.test();//走的是A类调用
//父类的引用指向了子类
B b = new A();
b.test();//走的是B类调用
}
}
重写
package com.oop.demo05;
//继承
public class A extends B{
//override:重写
package com.oop.demo05;
//重写都是方法的重写与属性无关
public class B {
public void test(){
System.out.println("B=>test()");
}
}
package com.oop;
import com.oop.demo05.A;
import com.oop.demo05.B;
public class Application {
//静态的方法和非静态的方法区别很大
public static void main(String[] args) {
//静态方法:方法的调用只和左边,定义的数据类型有关
//重写只和非静态方法有关
A a = new A();
a.test();//走的是A类调用
//父类的引用指向了子类
B b = new A();//子类重写了父类的方法
b.test();//走的是B类调用
}
}
重写:需要有继承关系,子类重写父类的方法
1.方法名必须相同
2.参数列表必须相同
3.修饰符:范围可以扩大但不能缩小; public >Protected>default>private
4.抛出的异常:范围,可以被缩小,但不能扩大;ClassNotFoundException(类找不到异常)-->Exception(大)
重写,子类的方法和父类必须一致:方法体不同!
为什么要重写:
1.父类的功能,子类不一定需要,或者不一定满足!
快捷键 :Alt + Insert :override;
package com.oop.demo06;
public class Student extends Person{
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;
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();
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
((Student)s2).eat();//子类重写了父类的方法,执行子类的方法
s1.eat();
}
}
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
public class Application {
public static void main(String[] args) {
//最外面都是Object 其次是String类
//最外面都是Object类 其次是Person类 再其次是Studnet类
//最外面都是Object类 其次是Person类 再其次是Teacher类
Object object = new Student();
//System.out.println(x instanceof y);//能不能编译成功看x与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
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(object instanceof String);//person和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);//编译报错
System.out.println("============================");
}
}
true true true false
false
true true true
false
true true
true
Process finished with exit code 0
强制转换
package com.oop.demo06;
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
package com.oop.demo06;
public class Person {
public void run(){
System.out.println("run");
}
}
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
public class Application {
public static void main(String[] args) {
//类型之间的转化:父 子,低转高不需要强制转换,高转低需要强制转换
//Person高 Stydent低
Person obj = new Student();
//student 将这个上面的obj对象转换为student类型(即将person类型转换为student类型),我们就可以使用Student类型的方法
Student student = (Student)obj;
student.go();
//还可以写成((Student)obj).go();
}
}
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
public class Application {
public static void main(String[] args) {
//类型之间的转化:父 子,低转高不需要强制转换,高转低需要强制转换
//子类转换为为父类会丢失一些方法
Student student = new Student();
student.go();
Person person = student;
((Student)person).go();
}
}
/*
1.父类引用指向子类的对象
2.把子类转换为父类,向上转型;
3.把父类转换为子类,向下转型;需要强制转换。可能会丢失一些方法
4.强制转换方便方法的调用,减少重复的代码;可以使代码简洁
抽象: 封装、继承、多态!
*/

浙公网安备 33010602011771号