Upward transformation of Java polymorphism

Upward transformation of Java polymorphism

Polymorphism is the third feature of object-oriented.

Advantages of polymorphism

To tell the truth, as Xiaobai, I don't quite understand the above three advantages. With in-depth study, understanding should be deeper and deeper. Encourage each other.

Upward transformation

concept

Java allows the subclass object to be assigned to the reference variable of the parent class without any coercion, and the system completes it automatically. The upward transformation comes from the bottom-up inheritance relationship. The subclass inherits the parent class. The subclass is a special parent class, so the operation of upward transformation is reasonable.

Let's try to understand the concept and benefits of upward transformation according to the simple code.

package com.my.pac14;

/**
 * @auther Summerday
 */
public class DynamicBinding {
    //Object是所有类的超类,根据向上转型,该方法可以接受任何类型的对象
    public static void test(Object x) {
        System.out.println(x.toString());
    }

    public static void main(String[] args) {
        test(new PrimaryStudent());//Student
        test(new Student());//Student
        test(new Person());//Person
        test(new Object());//java.lang.Object@1b6d3586
    }
}

class Person extends Object {
    @Override
    public String toString() {
        return "Person";
    }
}

class Student extends Person {
    @Override
    public String toString() {
        return "Student";
    }
}

class PrimaryStudent extends Student {
}
 public static void test(Object x) {
        System.out.println(x.toString());
    }
public static void main(String[] args) {
    test(new PrimaryStudent());//Student
    test(new Student());//Student
    test(new Person());//Person
    test(new Object());//java.lang.Object@1b6d3586
}
Object x = new Student();

What's good about upward transformation

If there is no upward transformation mechanism, we need to add many overloaded test methods to achieve the original effect, which is too cumbersome. If you want to add a method like test () or add a new class exported from object, you will also do more complex operations, which is not conducive to expansion and is not desirable.

// 原来的情况:需要创建很多很多的测试方法。
    public static void test(Object x) {
        System.out.println(x.toString());
    }
    public static void test(Person x) {
        System.out.println(x.toString());
    }
    public static void test(Student x) {
        System.out.println(x.toString());
    }
    public static void test(PrimaryStudent x) {
        System.out.println(x.toString());
    }

The existence of polymorphism just solves this thorny problem. In order to facilitate expansion, you only need to write a simple method that only receives the base class as parameters. No matter how the exported class is, it is really comfortable to automatically select and call the method of the corresponding exported class at runtime.

Reference book: Thinking in Java

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>