方法重载
重写:需要有继承关系,子类重写父类的方法
1、方法名必须相同
2、参数列表必须相同
3、修饰符:范围可以扩大,但不能缩小 public》protected》Default》private
4、抛出的异常:范围可以被缩小,但不能扩大 ClassNotFoundException《Exception(大)
重写,子类的方法和父类必须一致,方法体不同
为什么需要重写:
1、父类的功能,子类不一定需要,或者不一定满足
Alt+Insert : Override
package java_oop.demo05;
/**
* @author IT_Jay
* @date 2022/1/25 19:44
*/
public class A extends B{
// public static void test(){
// System.out.println("A=>test()");
// }
//Override 重写
@Override //注释:有功能的注释
public void test() {
// super.test();
System.out.println("A=>test()");
}
}
/**
* //静态方法输出结果
* A=>test()
* B=>test()
*
* //@Override
* //super.test();输出结果
* B=>test()
* B=>test()
*
* //@Override
* //System.out.println("A=>test()");输出结果
* A=>test()
* A=>test()
*/
/**
* //main方法
* //静态方法和非静态方法区别很大
* //静态方法:方法的调用只和左边,定义的数据类型有关
* //非静态方法:重写
* public static void main(String[] args) {
*
* A a = new A();
* a.test();
*
* //父类的引用指向了子类
* B b = new A(); //子类重写了父类的方法
* b.test();
*
* }
*/
package java_oop.demo05;
/**
* @author IT_Jay
* @date 2022/1/25 19:44
*/
//重写都是 方法的重写,与属性无关
public class B {
// public static void test(){
// System.out.println("B=>test()");
// }
public void test(){
System.out.println("B=>test()");
}
}
多态
- 即同一个方法可以根据发送对象的不同而采用多种不同行为方式
- 一个对象的实际类型是确定的,但可以指定对象的引用的类型有很多(父类,有关系的类)
- 多态存在的条件
- 有继承关系
- 子类重写父类方法
- 父类引用指向子类对象
- 注意:多态是方法的多态,属性没有多态
- instanceof (类型转换) 引用类型 判断一个对象是什么类型
package java_oop.demo06;
/**
* @author IT_Jay
* @date 2022/1/25 20:16
*/
public class Person {
public void run(){
System.out.println("run");
}
}
/**
* 多态注意事项:
* 1、多态是方法的多态,属性无多态
* 2、父类和子类,有联系 类型转换异常ClassCastException
* 3、存在条件:继承关系,方法需要重写,父类的引用指向子类对象 Father f1 = new Son();
*
* 不能重写
* 1、static方法,属于类,不属于实例
* 2、final 常量
* 3、private 方法;
*/
/**
* //main方法
*
* 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(); //重写之前输出run;Override后,子类重写父类的方法,执行子类的方法
* s1.run();
*
* s1.eat();
* // s2.eat(); //报错,
* }
*/
package java_oop.demo06;
/**
* @author IT_Jay
* @date 2022/1/25 20:16
*/
public class Student extends Person{
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
- instanceof
- main
public static void main(String[] args) {
//类型之间的转换: 父类 子类
//高 低
Person student =new Student();
//将student(此时为Person类型)这个对象转换为Student类型,我们就可以直接使用Student类的方法
// Student s = (Student)student;
// s.go();
((Student)student).go(); //两句合为一句
//子类转父类,可能丢失自己本来的方法
Student student1 = new Student();
student1.go();
Person person = student;
}
- Person
package java_oop.demo07;
/**
* @author IT_Jay
* @date 2022/1/25 20:55
*/
public class Person {
public void run(){
System.out.println("run");
}
}
- Teacher
package java_oop.demo07;
/**
* @author IT_Jay
* @date 2022/1/25 20:56
*/
public class Teacher extends Person{
}
- Student
package java_oop.demo07;
/**
* @author IT_Jay
* @date 2022/1/25 20:55
*/
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
/**
* public static void main(String[] args) {
* //Object >> Person >> Student
* //Object >> Person >> Teacher
* //Object >> String
*
* // System.out.println(X instanceof Y); //能否编译取决于X、Y是否有父子关系
*
* Object object = new Student();
* 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(person instanceof String); //false 编译错误
*
* 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); //false 编译错误
* }
*
*/