JavaSE面向对象03 2021.05.13-14

本文详细探讨了Java中的方法重写和多态性。讲解了静态方法与非静态方法的区别,强调了重写仅适用于非静态方法,并给出了重写规则。通过示例展示了多态的概念,指出其条件包括继承、方法重写和父类引用指向子类对象。还讨论了`instanceof`关键字的使用和类型转换,总结了多态的关键要点和应用场景。
摘要由CSDN通过智能技术生成

最近跑回学校两趟,然后外婆外公移山,时间都比较零碎。同时还要解决毕业论文答辩,相关材料的准备;
但同时也有小期待小盼头啦,毕业照,又是爷青结。加油!

在这里插入图片描述

1 方法重写

  • 静态方法和非静态方法区别很大!
  • 重写都是方法的重写,和属性无关

静态方法static:方法的调用只和左边,即定义的数据类型有关

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

}

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

public class Application {
    public static void main(String[] args) {
        A a = new A();
        a.test();//A>=test()

        B b = new A();//父类的引用指向了子类
        b.test();//B>=test()
    }
}


重写只跟非静态方法有关

快捷键 alt + insert
得到:
在这里插入图片描述
注意点:父类的static的方法在下面的这个窗口不显示
在这里插入图片描述
//Override 重写

    @Override //注解:有功能的注释!
    public void test() {
        super.test();
    }

非静态:重写

子类重写了父类的方法,执行子类的方法
在这里插入图片描述在这里插入图片描述

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

}

public class A extends B{
    //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();

        B b = new A();//结果A>=test()   , 子类重写了父类的方法,执行子类的方法
        b.test();
    }
}

重写小结:

  • 需要有继承关系,子类重写父类的方法!

    1. 方法名必须相同
    2. 参数列表必须相同
    3. 修饰符:范围可以扩大但不能缩小: public> protected>default>private
    4. 抛出的异常:范围可以被缩小,但不能扩大;ClassNotFoundException --> Exception(范围更大,不可以) ;
  • 重写的子类的方法和父类必须一致,方法体不同

  • 为什么需要重写:
    父类的功能,子类不一定需要,或者不一定满足!所以在子类中才需要重新写。

  • 重写快捷键 Alt + Insert : override

2 多态

  • 动态编译:类型,可扩展性变强

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

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

  • 多态存在的条件

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


一个对象的实际类型是确定的,但是可以指向这个对象的引用类型可以是任意父类的。

比如:new一个子类Student,这时会默认new了一个父类Person,所以可以引用父类Person

所以有 父类的引用指向子类:Person p = new Student();


对象能执行哪些方法,主要看对象左边的类型,和右边关系不大

Student 能调用的方法都是自己的或者继承父类的

Person 父类型,可以指向子类,但是不能调用子类独有的方法;子类重写了父类的方法,执行子类的方法

public class Person {
    public void run(){
        System.out.println("father");
    }
}


public class Student extends Person{
    @Override
    public void run() {
        System.out.println("son");
    }

    public void eat(){
        System.out.println("eat");
    }
}

public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        Person s2 = new Student();
        Object s3 = new Student();

       s1.run();//son
       s2.run();//son

        s1.eat();//eat
        ((Student)s2).eat();//eat  ,强制转换
    }
}

多态注意事项:

  1. 多态是方法的多态,属性没有多态
  2. 父类和子类,有联系;不然类型转换异常!:ClassCastException!
  3. 存在的条件:继承关系,方法需要重写,父类引用指向子类对象 Father f1 = new Son();
    以下情况不能重写,也就不能实现多态:
    ①static方法,属于类。不属于实例
    ②final常量,在常量池
    ③priva方法

instanceof

System.out.println(X instanceof Y);

能不能编译通过,取决于X的引用类型与Y之间是否存在父子关系,否则编译报错
结果true还是false,X所指向的实际类型是否Y的子类型。

        //Object>Person>Teacher
        //Object>Person>Student
        //Object>String

        //System.out.println(X instanceof Y);

        Object object = new Student();
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Person);//true
        System.out.println(object instanceof Student);//true
        System.out.println(object instanceof Teacher);//false object所指向的实际类型为student类,不是Teacher的子类型
        System.out.println(object instanceof String);//false object所指向的实际类型为student类,不是Teacher的子类型

        Person person = new Student();
        System.out.println(person instanceof Object);//true
        System.out.println(person instanceof Person);//true
        System.out.println(person instanceof Student);//true
        System.out.println(person instanceof Teacher);//false object所指向的实际类型为student类,不是Teacher的子类型
        //System.out.println(person instanceof String); //报错,不存在父子关系

        Student student = new Student();
        System.out.println(student instanceof Object);//true
        System.out.println(student instanceof Person);//true
        System.out.println(student instanceof Student);//true
        //System.out.println(student instanceof Teacher); //报错,不存在父子关系
        //System.out.println(student instanceof String); //报错,不存在父子关系

类型转换

类型之间的转换:父类----->子类

//子类转换为父类,可能丢失自己本来的一些方法!

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 person = new Student();//student一开始从低的Student类转化成高的Person类
        /*
        Student student = new Student();
        Person person = student;
         */
        //person.go;报错
        
        //将这个对象转换为Student类,就可以使用Student类的方法了
        ((Student)person).go(); //go
    }
}

小结:

  1. 父类引用指向子类的对象
  2. 把子类转换为父类,向上转型
  3. 把父类转换成子类,向下转型;强制转换
  4. 方便方法调用,减少重复的代码,简洁

核心思想:抽象
还有抽象类,接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值