java基础——方法的重写、多态+instanceof

方法的重写

  • 前提:需要有继承关系:子类 重写 父类 的 方法。

  • 注意点:

    1. 方法名必须相同
    2. 参数列表必须相同
    3. 修饰符:方位可以扩大,但是不能缩小: Public>Protected>Default>Private
    4. 抛出的异常:范围,可以被缩小,但不能扩大;classNotFoundException
  • 重写,子类的方法和父类必须要一致,但是方法体不同!

为什么要重写:

  1. 父类的功能 子类不一定需要,或者不一定满足
  2. 快捷键:Alt+Insert ----->override
public class B {

    public B() {
    }

    public void test() {
        System.out.println("B ===> test");
    }
}
=======================
public class A  extends B{

    public A() {
        super();
    }

    // override 表示重写
    @Override // 相当于注释,但是有意义的注释
    public void test() {
        System.out.println("A ===>test ");
    }
}
=======================
public class Application {
    public static void main(String[] args) {
		
        //子类,创造的子类的对象, 其对象可以调用 子类的 方法
        A a = new A();
        a.test();
        System.out.println("============");
		// 子类创造的父类对象。由于被限制在父类,只能用父类可以继承的东西,、
        // 所以,其调用的方法,必须是父类可以继承的(即父类的方法)
        B b = new A();
        b.test();
    }
}

多态

  • 即同一种方法可以根据发送对象的不同二采用多种不同的行为方式。

  • 一个对象的实际类型是确定的,但是可以指向对象的引用的类型有很多(父类 或 有关系的类)

  • 多态存在的条件:

    • 有继承关系
    • 子类引用父类的方法
    • 父类引用指向子类的对象
  • 注意:多态是方法的多态,属性没有多态性

  • instanceof (类型转换) 引用类型,判断一个对象是什么类型

多态的注意事项:
1. 多态是方法的多态,其属性没有多态
2. 父类和子类,有联系   类型转换异常! ClassCastException!
3. 存在条件:继承关系 ,方法需要被重写,父类的引用指向子类对象!Father f1 = new son();

不能被重写东西:
1. static 方法,属于类,它不属于实例
2. final 常量;
3. private 方法
public class Person {
    public void run(){
        System.out.println("Person Run");
    }
}
==================================
public class Student  extends Person {
    @Override
    public void run() {
        System.out.println("Son run");;
    }
    public void eat(){
        System.out.println("Son 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();

        s1.run();
        s1.eat();

        //对象能执行那些方法,主要看对象左边的类型和右边关系不大
        //子类重写了父类的方法,执行子类的方法
        s2.run();
        ((Student) s2).eat();
    }
}

instanceof

  • 判断两个类之间是否存在父子关系
public class Student  extends Person {

}
====================================
public class Teacher extends Person{
    
}
====================================
public class Student extends Person{
    
}
====================================
public class Application {
    public static void main(String[] args) {

        //总结:用 System.out.println(x instanceof y);  来判断有咩有父子关系

        // Object > String
        // Object > person > teacher
        // Object > Person > Student
        Object xxxx = new Student();

        System.out.println(xxxx instanceof Object);
        System.out.println(xxxx instanceof Person);
        System.out.println(xxxx instanceof Student);
//        System.out.println(xxxx instanceof Teacher);  //编译前报错
        System.out.println(xxxx instanceof String);

        System.out.println("=====================");

        Person yyyy = new Student();

        System.out.println(yyyy instanceof Object);
        System.out.println(yyyy instanceof Person);
        System.out.println(yyyy instanceof Student);
//        System.out.println(yyyy instanceof Tercher);  //编译前直接报错
//        System.out.println(yyyy instanceof String);  //编译时报错,因为时平行关系,不能转换

        Student zzzz = new Student();
        System.out.println(zzzz instanceof Object);
        System.out.println(zzzz instanceof Person);
        System.out.println(zzzz instanceof Student);
//        System.out.println(zzzz instanceof Teacher); //编译前直接报错
//        System.out.println(zzzz instanceof String);  //编译前直接报错

    }
}
父 子 类之间的强制转换:
1. 父类引用指向了子类的对象(产生关系)
2. 把子类装欢为父类,时向上转型。(因为父类的提炼的 通性 更高,只需削特殊性)
3. 把父类转换为子类,向下转型。(往 通性 更低转,需增加其特殊性。)(强制)
4. 为什么进行前置转换:方便方法的调用,减少重复的代码
public class Person {
    public void run(){
        System.out.println("父 person run");
    }
}
==============================================
public class Student extends Person{
    public void go(){
        System.out.println("子 students go");
    }
}
==============================================
public class Application {
    public static void main(String[] args) {
        //基本类型的转换: 64 -> 32 -> 16 -> 8
        //父子之间类型的转换: 父  ->  子
        //父的优先级高,子的优先级低
        // 父转子类型,需要强制,恢复其特殊性,丧失其通用性。
        // 子类转父类,丧失其特殊性(特有方法),得到其通用性。可以自动转。

        Person student = new Student();

        // 如果上面Person类型的student指引,想要使用Student类的所有方法,
        // 必须要将student,强制转换为Student类型
        student.run();
        //student.go(); //直接调用Student类中的go()方法,不被允许
        ((Student)student).go(); // 强制转换成Student类型后,则可以直接调用
    }
}
posted @ 2021-05-27 00:48  放慢_脚步  阅读(216)  评论(0编辑  收藏  举报