0
点赞
收藏
分享

微信扫一扫

方法的重写和多态和类型转换

女侠展昭 2022-04-24 阅读 79
java

B类是父类,A类是子类,Application是主类。

package com.rgf.oop.Demo08;
//B类是父类,重写都是方法的重写,和属性无关
public class B {
    public static void test(){
        System.out.println("B=>test");
    }
}
package com.rgf.oop.Demo08;
//A类是子类,
public class A extends B{
    public static void test(){
        System.out.println("A=>test");
    }
}
package com.rgf.oop.Demo08;

public class Application {
    public static void main(String[] args) {
//方法的调用只和左边有关,定义的数据类型有关。
        A a=new A();//A类
        a.test();
  //父类的引用指向了子类
        B b=new A();
        b.test();//B类
    }
}

运行界面:

当我们在上述代码中去掉static之后,会出现箭头向上或者向下,代表重写。

我们在构造重写方法的时候,我们可以通过快捷键的使用,点击ALT+Insert,或者点击右键,选择Generate.

我们选择Override Methods,重写方法,进行选择所要重写的方法。

方法的重写:

package com.rgf.oop.Demo08;
//静态的方法和非静态的方法区别很大!
//静态方法://方法的调用只和左边,定义的数据类型有关
//静态方法在类一加载的时候就出来了
//非静态:重写
public class Application {
    public static void main(String[] args) {
        A a=new A();
        a.test();
        B b=new A();//子类重写了父类的方法,此方法只与非静态有关
        b.test();
    }
}
package com.rgf.oop.Demo08;
//B类是父类,重写都是方法的重写,和属性无关
public class B {
    public  void test(){
        System.out.println("B=>test");
    }
}
package com.rgf.oop.Demo08;
//A类是子类,
public class A extends B{
    //Override 重写
    @Override //注解:有功能的注释
    public void test() {
        System.out.println("A=>test");
    }
}

运行界面如下:

 重写的总结:

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

1.方法名必须相同

2.参数列表必须相同

3.修饰符:范围可以扩大。可以从本来的小范围继承变成后面的大范围。但不能缩小public>protected>default>private

4.抛出的异常:范围可以被缩小但不能扩大。classNotFoundException(小)---->Eexception(大)

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

为什么需要重写:

1.父类的功能,子类不一定需要,或者不一定满足!

面向对象的多态:

动态编译:类型:可扩展性

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

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

多态存在的条件:

有继承关系

子类重写父类方法

父类引用指向子类对象

注意:多态是方法的多态,属性没有多态性。

多态的代码示例如下:

主类:

package com.rgf.oop.Demo09;

public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的。
       // new Student();
       // new Person();
        //可以指向的引用类型就不确定了,父类的引用指向子类
        //Student 能调用的方法都是自己的或者继承父类的。
        Student stu1 = new Student();
        //Person 父类型,可以指向子类,但是不能调用子类独有的方法
        Person stu2 = new Student();
        Object stu3 = new Student();
        //对象能执行哪些方法主要取决于对象左边的类型,和右边关系不大。

        stu2.run();  //子类重写了父类的方法,执行子类的方法
        stu1.run();
       ((Student)stu2).eat();//强制转换类型,将Person类型转换为Student类型,然后进行调用方法


    }
}

父类:

package com.rgf.oop.Demo09;

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

子类:

package com.rgf.oop.Demo09;

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

运行界面如下:
 多态注意事项:

1.多态是方法的多态,属性没有多态

2.父类和子类,有联系,类型无法转换而强自转换会出现类型转换异常        ClassCastException!

3.存在条件:继承关系,方法需要重写,父类的引用指向的是子类对象!father f1=new Son();

无法被重写的方法:

(1)static,静态方法,属于类的,不属于实例

(2)final 常量

(3)private方法

intanceof关键字(类型转换 ) (引用类型),判断一个对象是什么类型

父类:

package com.rgf.oop.Demo09;

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

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

public class Student extends Person{

}
package com.rgf.oop.Demo09;

public class Application {
    public static void main(String[] args) {
        //Object>String
        //Object>Person>Student
        Object object = new Student();


        //System.out.println(X instanceof Y);看X和Y之间是否有父子关系,有则编译通过,没有则编译错误。
        System.out.println(object instanceof Student);//true
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Person);//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 Object);//true
        System.out.println(person instanceof Person);//true
        System.out.println(person instanceof Teacher);//false
        //System.out.println(person instanceof String);//编译报错
        System.out.println("===============================");
        Student student=new Student();
        System.out.println(student instanceof Student);//true
        System.out.println(student instanceof Object);//true
        System.out.println(student instanceof Person);//true
       // System.out.println(student instanceof Teacher);//编译错误
        //System.out.println(student instanceof String);//编译错误

    }
}

运行界面如下: 

类型转换:

父类:

package com.rgf.oop.Demo09;

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

子类:

package com.rgf.oop.Demo09;

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

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

主类:

package com.rgf.oop.Demo09;

public class Application {
    public static void main(String[] args) {

//类型之间的转化:基本类型转换   高低64  32  16 8,高转低需要强转,低转高则不需要。
        //父                   子
        //高                   低
        Person student = new Student();
        //student将这个对象转换为Student类型,我们就可以使用Student类型的方法了
        Student student1 = (Student) student;
        student1.go();
      //  ((Student)student).go();写到一起即为这个样子。
        Student student2=new Student();
        Person person=student2;
        //person.go();子类转换为父类的时候,可能丢失自己本来的一些方法。
    }
}

运行界面如下所示:

多态的总结:

1.父类引用指向子类的对象

2.把子类转换为父类,向上转型;

3.把父类转换为子类,向下转型;强制转换,

4.方便方法的调用,减少重复的代码:简洁

抽象:封装、继承、多态  。抽象类,接口(比抽象类还要抽象的抽象)

抽象:编程思想!持续的学习,茅塞顿开,多实践,多测试大脑中的想法,实践出真知。

举报

相关推荐

0 条评论