Java learning summary: 12

Polymorphism

1. Method polymorphism: overloading and overwriting
Overloading: the same method name, according to different parameter types and number can complete different functions;
overwriting: the same method, according to different instantiated subclasses Different objects have different functions.
2. Object polymorphism: conversion of parent and child class objects
Upward transformation: child class object becomes parent class object, format: parent class parent class object = child class instance, automatic conversion;
downward transformation: parent class object becomes child Class object, format: subclass subclass object = (subclass) parent class instance, forced conversion;

Example: The object is transformed downward:

class A4{
    public void print(){
        System.out.println("父类");
    }
}
class B4 extends A4{
    public void print(){
        System.out.println("子类");
    }
}
public class Test1_1_3_7 {
    public static void main(String args[]){
        A4 a=new B4();
        a.print();
    }
}
//结果:
//子类

The final execution is determined based on whether the class in which the instantiated object resides overrides the method in the parent class. This program instantiates the subclass object (new B4 ()), and the print () method is overwritten by the subclass, Then the final call must be the overwritten method ( don't look at the class name, but depends on the class of the instantiated object ).

Example: Objects are transformed downward

class A4{
    public void print(){
        System.out.println("父类");
    }
}
class B4 extends A4{
    public void print(){
        System.out.println("子类");
    }
}
public class Test1_1_3_7 {
    public static void main(String args[]){
        A4 a=new B4();	//实例化的是子类对象,对象向上转型
        B4 b=(B4)a;	//对象需要强制性地向下转型
        b.print();	//调用被子类覆写过的方法
    }
}
//结果:
//子类

Because there is a forced conversion operation, the downward transformation operation itself has a prerequisite, that is, the downward transformation must occur after the upward transformation occurs .

Example: Wrong down-conversion operation

class A4{
    public void print(){
        System.out.println("父类");
    }
}
class B4 extends A4{
    public void print(){
        System.out.println("子类");
    }
}
public class Test1_1_3_7 {
    public static void main(String args[]){
        A4 a=new A4();	//直接实例化子类对象,没有进行向上转型
        //此时并没有发生子类对象向上转型的操作,所以强制转型会带来安全隐患
        B4 b=(B4)a;	//强制向下转型,产生异常
        b.print();	//此语句无法执行
    }
}

Features:
upward transformation: its purpose is the unification of parameters, but in upward transformation, the methods that can be called by the parent object instantiated by the subclass can only be methods defined in the parent class;
downward transformation: the purpose is The parent class object should use the special method in the subclass that instantiates it, but the downward transformation is to be forced conversion, such operation is easy to bring security risks.

In order to ensure the smooth transition, Java provides a keyword: instanceof, which can be used to determine whether an object is an instance of a specified class.
The use format is as follows:

对象 instanceof 类  返回boolean

If an object is an instance of a class, return true, otherwise return false.

Example: Use instanceof to judge

class A6{
    public void print(){
        System.out.println("父类");
    }
}
class B6 extends A6{
    public void print(){    //覆写
        System.out.println("子类");
    }
    public void funB(){
        System.out.println("子类扩充的方法");
    }
}
public class Test1_1_3_9 {
    public static void main(String args[]){
        fun(new B6());  //对象向上转型
    }
    public static void fun(A6 a){
        a.print();
        if(a instanceof B6){    //如果a对象是B6类的实例
            B6 b=(B6)a; //向下转型
            b.funB();   //调用子类扩充的方法
        }
    }
}
//结果
//子类
//子类扩充的方法
49 original articles published · Liked 25 · Visits 1537

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/104280272