面向对象03

什么是多态

1.多态是方法的多态
2.父类和子类有联系,如person和student类
一个对象的实际类型是确定的,但是他指向的引用类型就不确定了。
可以通过父类去new子类

可以实现动态编译,类型实现可扩展性

public class Person {

}

public class Student extends Person{

}

import OOP.duotai.Person;
import OOP.duotai.Student;

public class Application {
    public static void main(String[] args) {
        //一个类new出来的对象的实际类型是确定的
        //如new Person();new成的一定是Person类

        //但类new的对象可以指向的引用类型就不一定确定
        Student s1 = new Student();
        Person s2 = new Student();
        //即:父类的引用Person s2,指向子类Student的类型
        //存在继承关系时才可
        Object s3 = new Student();
    }
}

一个类new出来的对象的实际类型是确定的,如new Person();

new成的一定是Person类。但类new的对象可以指向的引用类型不一定确定:即:父类的引用Person s2,指向子类Student的类型(存在继承关系时才可)

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

public class Student extends Person{

}

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

        Student s1 = new Student();
        Person s2 = new Student();

        s2.run();//s2为Person类(声明为最终的实际类,此时声明的是Person类,即s2前面声明的类),Person类中本身就有run方法,直接调用
        s1.run();//s1为Student类,尽管Student类没有run方法,但Student类继承了父类Person的run方法,因此s1也可以run,此时方法为父类Person的run方法
    }
}

此时输出:
Father
Father

若子类Student中对方法重写:

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

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

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

        Student s1 = new Student();
        Person s2 = new Student();

        s2.run();//s2是Person类,但指向的是Student子类,当子类重写了父类的方法后,会执行子类重写的方法
        s1.run();//s1是Student类,当子类重写了父类的方法后,走重写后的方法,即son方法
        
        
        Person s3 = new Person();//此时s3为纯粹的Person类,未指向子类,因此其方法仍是用父类原有的方法
        s3.run();
    }
}

此时输出:
son
son
Father

即:当父类的引用(Person s2)指向子类(new Student)时,若子类重写了父类的方法,那么该对象调用的是重写后子类中的方法,若未重写还是调用父类中的方法

当父类未指向子类时,若子类重写了父类的方法,对该对象方法的调用没有影响,仍是直接调用父类本来的方法。

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();

        s2.eat();//这样直接写此行会报错,因为s2为Person类,而Person类中本身没有eat方法,eat方法是Student子类中的方法
        s1.eat();
    }
}

即:对象能执行哪些方法,主要看生成对象时其左侧声明的类型,如Person s2 = new Student();中左侧的Person,Person作为父类本身无eat方法时,无法调用该方法,(与右侧关系不大)

即:子类可以调用的方法都是自己本身已有的或继承父类的

父类(声明)虽然可以指向子类,但不能调用子类独有的方法;且若子类对继承的方法进行了重写,父类指向子类的生成对象调用的也将是重写后的方法

上述例子若.eat处不报错,应对s2进行强制转换,将其从Person类转为Student类:

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

        Student s1 = new Student();
        Person s2 = new Student();

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

多态的注意事项:

多态是方法的多态,属性没有多态
多态的前提:只存在于父类与子类之间,两类需要有父子联系,否则会出现类型转换异常:ClassCastException
多态存在的条件:继承关系;方法需要重写;父类引用指向子类对象:Father f1 = new Son();
static方法属于类,不属于实例,因此不可重写
final常量修饰的方法不可重写
private修饰的方法不可重写
多态即:同一个方法(重写后)可以根据发送对象的不同(父类是否指向子类)而采用不同的方法

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

关键词:instanceof、类型转换(引用类型之间的转换)强制转换/自动转换

instanceof和类型转换

通过instanceof判断对象是什么类型

public class Person {

}

public class Student extends Person{

}

public class Teacher extends Person{

}

import OOP.duotai.Person;
import OOP.duotai.Student;
import OOP.duotai.Teacher;

public class Application {
    public static void main(String[] args) {
        Object object = new Student();
        
        System.out.println(object instanceof Student);
        System.out.println(object instanceof Person);
        System.out.println(object instanceof Object);
        System.out.println(object instanceof Teacher);
        System.out.println(object instanceof String);

    }
}
输出:
true
true
true
false
false

即:此时object即是Student类,又是Person类,又是Object类。

Object > Person > Student(判断/定义类型时可向上转型)
Object > Person > Teacher
Object > String

类型关键看new什么,倘若是声明父类但new子类来生成的对象,在判断类型时可自动向上转型,即它既是子类类型,又是父类类型;但若new父类生成的对象,则其在判断类型时,不是子类类型。

且有上下继承关系时才会互相转/判断为true:

public class Application {
    public static void main(String[] args) {
        Object object = new Student();
        Person person = new Student();
        System.out.println(person instanceof Student);
        System.out.println(person instanceof Person);
        System.out.println(person instanceof Object);
        System.out.println(person instanceof Teacher);
        System.out.println(person instanceof String);//此行会编译报错,因为本身Student与Person两个类与String并没有直接关系,因此不可以直接用instanceof来连接

    }
}
输出:
true
true
true
false

两者之间若没有直接继承关系,则不可比较;

(子类是父类的一种,而父类不一定是子类的一种,如猫科动物(父类)不一定是老虎(子类),而老虎(子类)一定是猫科动物(父类))

声明的类型是用来开内存的模板,实际上生成什么类型的对象要看new,能装入模板的属性与方法就装,否则就用模板自带的

类型转换

之前的类型转换:64位,32位等,高转低需要强制转换,低转高可自动

现在是父子类型的转换,父为高,子为低:

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

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

public class Teacher extends Person{

}

public class Application {
    public static void main(String[] args) {
        Person person = new Student();
        person.go(); //此行会编译报错:
    }
}



// 高                    低
Person person = new Student();
//Student类中才有go方法,而此时Person声明为person类,不可用子类的方法,应强制转换person对象为Student类才能用go方法

即:
Student student = (Student) person;//强制转换
student.go();



将两句汇总成一句:
((Student)person).go();
输出:
    go

本质:子类Student占用1.5个内存,而父类Person占用1.0个内存,子类多的0.5个内存封装了子类独有的方法,即便student覆盖了person,但person内存不够大装不下go方法,所以调用不了go

向下强制转换语法:
( ( 目标子类类型 ) 当前待转换的对象名) . 子类方法();

( ( Student ) person ) . go( );

而子类转父类:低转高可默认,但是转换后可能会丢失自己本身原有的方法

Student student = new Student();
Person person = student ;//低转高
//此时转换后person不能再调用Student类的go方法

多态总结:

存在条件:父类引用指向子类的对象(反之不成立)
子类转换为父类:向上转型不需要强制转换,instanceof也会true
父类转换为子类:需要强制转换

无标签
打赏
评论区
头像