方法重写&多态、instanceof&类型转换

方法重写

重写需要有继承关系,子类重写父类的方法!
1.方法名必须相同
2.参数列表列表必须相同
3.修饰符:范围可以扩大但不能缩小: public>Protected>Default>Private
4.抛出的异常:范围,可以被缩小,但不能扩大:ClassNotFoundException–>(大)

重写·子类的方法和父类的必须要一致:方法体不同!

为什么需要重写:
1.父类的功能,子类不一定需要,或者不一定满足!
Alt+Insert:override;
package com.oop.demo06;

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

package com.oop.demo06;

public class A extends B {
    public void test() {
        System.out.println("girl");
    }
}
/*
package com.oop;

import com.oop.demo06.A;
import com.oop.demo06.B;

public class Application {

    public static void main(String[] args) {
        A a = new A();
        a.test();
        B b = new A();
        b.test();//父类引用指向子类对象!
    }

}

 */

## 多态

package com.oop.demo07;

public class Person {
    public void run(){
        System.out.println("LOVE");
    }
}
package com.oop.demo07;

public class Student extends Person{
    @Override
    public void run() {
        System.out.println("girl");
    }
    public void eat(){
        System.out.println("吃饭");
    }
}
/*
多态注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系 类型转换异常!ClassCastException!
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();

    1.static 方法,属于类,它不属于实例
    2.final 常量; 不可以被重写
    3.private 方法:
package com.oop;


import com.oop.demo07.Person;
import com.oop.demo07.Student;

public class Application {

    public static void main(String[] args) {
       //一个对象的实际类型是确定的
       //Student能够调用的方法都是自己或者继承父类的!
        //父类不能调用子类独有的 方法!
        Student s1 = new Student();
        Person s2 = new Student(); //父类的引用指向子类
        Object s3 = new Student();
        //对象能够执行哪些方法,主要看左边的类型,和右边关系不大!
        s2.run();//子类重写了父类的方法,执行子类的方法
        ((Student) s2).eat();
    }

}

 */

instanceof & 类型转换

package com.oop.demo08;

public class Person {
}
package com.oop.demo08;

public class Teacher extends Person{
}
package com.oop.demo08;

public class Student extends Person{
    public void go(){
        System.out.println("LOVE");
    }
}

/*

        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        Object obj = new Student();

        //System.out.Println(X instanceof Y); 测试类与类之间是否存在继承的关系!

        System.out.println(obj instanceof  Student);
        System.out.println(obj instanceof Person);
        System.out.println(obj instanceof Object);
        System.out.println(obj instanceof Teacher);//false
        System.out.println(obj instanceof String);//false

        Person person = new Student();
        System.out.println(person instanceof Object);
        System.out.println(person instanceof Student);
        System.out.println(person instanceof Person);
        System.out.println(person instanceof Teacher);//false
        //System.out.println(person instanceof String); 编译器报错!

        Student student = new Student();
        System.out.println(student instanceof Student);
        System.out.println(student instanceof Person);
        System.out.println(student instanceof Object);
        //System.out.println(student instanceof Teacher); 编译器报错!
 */

package com.oop;


import com.oop.demo08.Person;
import com.oop.demo08.Student;

public class Application {

    public static void main(String[] args) {
        //类型之间的转换:
        //高             //低
        Person p1 = new Student(); //低转高自动转换!

        //将p1这个对象转换为Student类型,就可以调用Student类型的仿法了!

        ((Student) p1).go();//高转低,强制转换!

        Student student = new Student();

        Person person = student;//子类转化为父类会丢失一些都西!
        //person.go();
        ((Student) person).go();


    }

}

/*

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

 封装、继承、多态! 抽象类、接口!




 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值