Object-oriented polymorphism of JAVA entry

Polymorphism

What is polymorphism?

Polymorphism is the same thing, showing different states in different situations.

Why does JAVA use polymorphism?

Because many things in real life show different states under different circumstances.
For example, at the same teller machine, you take a bank card to repay or withdraw money.

Note: If the credit card you take is our bank card, you can both repay and withdraw money, and you can also check the balance, but the same teller machine, if you hold another bank card, Then I'm sorry, I can only withdraw money. This is a teller machine, showing different states under different circumstances.

Overview

Polymorphism means that an object has multiple forms.
Benefits: It improves the flexibility of the program. Reflects the versatility of the program. Make a unified call. Standard
versatility: In polymorphism, it does not care about the type of specific subclasses and shields the subclasses. The difference between the subclasses will be regarded as the parent class

Features

Prerequisite: Inheritance relationship + method rewriting phenomenon occurs

Mantra 1: The parent class reference points to the subclass object
Animal a = new Dog();

2 formulas: Compile look left, look right of the run
you want to save is successful, it can only be used on the left of the parent class
results subject to the right of the subclass

The meaning of polymorphism

JAVA polymorphism is manifested in two aspects:

The first aspect:

For a super-type variable, if the sub-type object it refers to is different, then the method implementation it executes at runtime can be different.
Insert picture description here

In the future, as long as the method in which subclass is executed at runtime, it will depend on which type of object new is, and there is no half a dime relationship with the variable on the left side of the equal sign.

Getting started case

package cn.tedu.oop;
		//测试 多态
		public class Test2_Mulity {
    
    
			public static void main(String[] args) {
    
    
				//2, 创建多态对象 -- 口诀1: 父类引用 指向 子类对象
				Animal a = new Dog() ;
				//3, 使用多态对象 -- 口诀2: 编译看左边, 运行看右边
				      //多态的目的是统一调用标准,,,标准就是父类 !!
				a.eat();//方法声明用了父类的, 方法体用了子类的
				//4, 父类中没有,是子类的特有的方法,非得用--多态对象不支持,只能创建子类对象使用
				//a.show();
			}
		}
		//1, 前提: 发生继承关系  +  发生方法重写现象
		class Animal{
    
    
			public void eat() {
    
    
				System.out.println("吃啥都行");
			}
		}
		class Dog extends Animal{
    
    
			public void show() {
    
    
				System.out.println("吃啥都行");
			}
			@Override
			public void eat() {
    
    
				System.out.println("狗吃肉");
			}
		}

Use of polymorphism

代码
		package cn.tedu.oop;
		//测试 多态的使用
		public class Test3_Mulity {
    
    
			public static void main(String[] args) {
    
    
				//创建多态对象测试--口诀1:父类引用 指向 子类对象
				Animal2 a = new Dog2();
				//使用多态对象--口诀2: 编译看左边,运行看右边
				a.eat();//1, 成员方法被重写了,所以用了子类的方法体 和 父类的方法声明
				System.out.println( a.name );//2, 成员变量用 父类的
				a.show();//3, 静态资源用 父类的
				Animal2.show();//show()是父类的
			}
		}
		//前提:继承+重写
		class Animal2{
    
    
			String name = "小动物" ;
			public void eat() {
    
    
				System.out.println("吃肉");
			}
			static public void show() {
    
    
				System.out.println("fu..show..");
			}
		}
		class Dog2 extends Animal2 {
    
    
			String name = "大黄" ;
			@Override
			public void eat() {
    
    
				System.out.println("狗吃肉");
			}
			//4, 静态资源可以被重写吗???--不能 !!!
			static public void show() {
    
    
				System.out.println("zi..show..");
			}
		}

Insert picture description here

The second aspect:

The second aspect is the reverse. If a sub-type object is "upwardly shaped" as a variable of a different parent type, the available functions are not the same.

What is upward modeling?

Upward modeling is the variable of the subtype type, which can be automatically converted into the variable of the parent type "upward".
For example:

ATMCBC cbc= new ATMCBC(); //cbc is a variable of the
subtype ATMCBC//The variable atm of the supertype IUnionPay can point to the object referenced by the subtype cbc.
IUnionPay atm=cbc; //Also called the variable cbc upward shape to super type IUnionPay type

Another example:

//Because the ATMCBC class also implements the IPolice interface,
the IPolice pol=cbc;//Compiled correctly: the variable cbc can also be shaped upward to the Ipolice type

The super type here can be an abstract class or an interface

The result of upward modeling

For variables of the parent type, only the methods of the parent type can be overridden by the child type. No other methods can be used.
When compiling, the compiler checks whether the call is correct according to the method definition in the parent type.
Insert picture description here


Insert picture description here

When compiling, method calls look at variables

Simple notation: In the future, as long as it is at compile time, to determine whether a variable of a parent type can call a method of a sub-type object, it depends on the variable type on the left side of the equal sign.

What should I do if I feel that the functions of the parent type are restricted at runtime and want to use more functions?

Forced conversion

The super-type variable can be converted into a sub-type variable through a forced conversion.
Prerequisite: The object referenced by the variable to be converted must be a sub-type of the target type or the target type itself.
E.g:

  • IPolice pol=new ATMCBC(); /The super type variable pol can only use the takePic method,
  • //Declare the subtype variable c, you can use all the subtype implementation methods
    ATMCBC c= (ATMCBC)pol;
    //Compile correctly. Because the object pointed to by the pol variable is the target type ATMCBC
    IUnionPay p= (IUnionPay)pol;
    //Compile correctly. Because the object type pointed to by pol is a subtype ATMABC of the target type IUnionPay
    aa=(ATMABC)pol;
    //Compiles correctly, but runs incorrectly: ClassCastException
    //Because the object type pointed to by pol is not a subtype of ATMABC.
    ATMABC aa= (ATMABC)pol;
    //The compilation is correct, but the operation error: ClassCastException
    //Because the object type pointed to by pol is not a subtype of ATMABC.
Force to look at the object! The target type must be the object type itself or the parent type of the object

instanceof keyword

· In the cast, in order to avoid ClassCastException, you can use the instanceof keyword to determine whether the object pointed to by a reference is of a specified type.
·Syntax: object instanceof target type pol instanceof ATMABC
·Return value:

  • True: indicates that the object is the target type or a subtype of the target type, which can be forced.

  • False: It means that the prerequisite of forced transfer is not met, and forced transfer is not allowed.

Guess you like

Origin blog.csdn.net/QQ1043051018/article/details/112503658