Java Fundamentals 12 Object Oriented ~ Polymorphism

Like you who love programming!
Learn SpringBoot actual combat course https://edu.csdn.net/course/detail/31433
Learn SpringCloud introductory course https://edu.csdn.net/course/detail/31451


Preface

In this article, we begin to explore the third important feature of object-oriented-polymorphism. You will learn what polymorphism is, the role of polymorphism, how to achieve polymorphism in programs, and the instanceof operator and up-down transformation. point.

Polymorphism related concepts

What is polymorphism

Polymorphism means that things have multiple states. For example, water changes between three states: liquid, solid, and gas as the temperature changes. People in different countries will change into Chinese, American, Indian, etc.

The role of polymorphism

Polymorphism improves the flexibility and scalability of the program.
Polymorphism in life: People in different countries can have their own favorite ways of eating, and solve problems flexibly.
Insert picture description here
Polymorphism in the program: Use objects of different subclasses as parent classes, which can shield the differences between objects of different subclasses. When programming, you can write more general and flexible code to adapt to different business needs.

Implementation of polymorphism

The premise of polymorphism

Polymorphism is based on encapsulation and inheritance mechanisms, and the following conditions must be met

  1. The child class inherits the parent class
  2. The child class overrides the method of the parent class
/**
 * 人类
 */
abstract class Human {
	public abstract void eat();
}
/**
 * 中国人
 */
class Chinese extends Human{
	public void eat(){
		System.out.println("中国人拿筷子吃");
	}
}
/**
 * 美国人
 */
class American extends Human{
	public void eat(){
		System.out.println("美国人拿刀叉吃");
	}
}
/**
 * 印度人
 */
class Indian extends Human{
	public void eat(){
		System.out.println("印度人拿手吃");
	}
}

Polymorphism

There are several ways to achieve polymorphism:

  1. Assign the object of the child class to the reference
    object of the parent class to call the method of the parent class
public class HumanTest{
	public static void main(String[] args) {
		Human man1 = new Chinese();
		man1.eat();
		Human man2 = new American();
		man2.eat();
		Human man3 = new Indian();
		man3.eat();
	}
}
  1. Define the parameters of the method as the parent type, and pass in the child object
public static void service(Human man){
	man.eat();
}

public static void main(String[] args) {
	service(new Chinese());
	service(new American());
	service(new Indian());
}
  1. Define the return type of the method as the parent type and return the child object
public static Human getHuman(int type){
	Human man = null;
	switch(type){
	case 1:
		man = new Chinese();
		break;
	case 2:
		man = new American();
		break;
	case 3:
		man = new Indian();
		break;
	}
	return man;
}

public static void main(String[] args) {
	Human man1 = getHuman(1);
	Human man2 = getHuman(2);
	Human man3 = getHuman(3);
	man1.eat();
	man2.eat();
	man3.eat();
}

Insert picture description here

Up-cast and down-cast

Upcasting
Subclass objects are converted to parent types

Human person = new Chinese();

Downcasting the
parent class object to the subclass type

如:子类类型 对象= (子类类型)父类对象;

The premise of downward transformation is: first upward transformation

//向上转型
Human  person = new Chinese();
//向下转型
Chinese ch = (Chinese)(person);
ch.eat();

Note: Downcasting has the risk of type conversion errors

Human  person = new Chinese();
//出现 ClassCastException 类型转换异常
Indian ch = (Indian)(person);

instanceof operator

instanceof role: to determine whether an object is of a certain type

对象 instanceof 类型
返回boolean类型结果

Suppose you add mahjong methods to the Chinese, war methods to the Americans, and yoga methods to the Indians. If you want to call this specific method of the human object, you need to use instanceof to determine the object type, and then perform a downward transformation.

//创建对象
Human person = new Indian();
ch.eat();
//判断人的类型
if(person instanceof Chinese){
	//向下转型
	Chinese ch = (Chinese)(person);
	ch.majiang();
}else if(person instanceof American){
	American ch = (American)(person);
	ch.fight();
}else if(person instanceof Indian){
	Indian ch = (Indian)(person);
	ch.yoja();
}else{
	System.out.println("我不认识你!");
}

End

Finally, leave a homework to check everyone's understanding:

  • Define the car category, with license plate, price and brand attributes.
  • Define the method of starting and calculating the rent for the car (car rental = price/365 + 100, truck = price/365 + 150, bus = price/365 + 200)
  • Cars, trucks, and buses inherit cars and rewrite the method of calculating rent according to the above algorithm.
  • Trucks have separate loading methods, and buses have separate stop reporting methods.
  • Create several car objects and test all methods.

If you need to learn other Java knowledge, poke here ultra-detailed knowledge of Java Summary

Guess you like

Origin blog.csdn.net/u013343114/article/details/112506137