Advantages of Java polymorphism method rewrite

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right


1. The advantages of polymorphism

The cost of using the class by class callers is further reduced.

Encapsulation means that the caller of the class does not need to know the implementation details of the class.
Polymorphism allows the caller of the class to not even know what the type of the class is, only to know that the object has a certain method.
Therefore, polymorphism It can be understood as a further step in encapsulation, allowing class callers to further reduce the cost of using the class.

class Shape {
    
    
	public void draw() {
    
    
	// 啥都不用干
	}
}
class Cycle extends Shape {
    
    
	@Override
	public void draw() {
    
    
		System.out.println("○");
	}
}
class Rect extends Shape {
    
    
	@Override
	public void draw() {
    
    
		System.out.println("□");
	}
}
class Flower extends Shape {
    
    
	@Override
	public void draw() {
    
    
		System.out.println("♣");
	}
}
/我是分割线//
// Test.java
public class Test {
    
    
	public static void main(String[] args) {
    
    
		Shape shape1 = new Flower();
		Shape shape2 = new Cycle();
		Shape shape3 = new Rect();
		drawMap(shape1);
		drawMap(shape2);
		drawMap(shape3);
	}
// 打印单个图形
	public static void drawShape(Shape shape) {
    
    
	shape.draw();
	}
}

Can reduce the "cyclocomplexity" of the code, avoid using a lot of if-else

Unused

public static void drawShapes() {
    
    
Rect rect = new Rect();
Cycle cycle = new Cycle();
Flower flower = new Flower();
String[] shapes = {
    
    "cycle", "rect", "cycle", "rect", "flower"};
for (String shape : shapes) {
    
    
	if (shape.equals("cycle")) {
    
    
		cycle.draw();
	} else if (shape.equals("rect")) {
    
    
		rect.draw();
	} else if (shape.equals("flower")) {
    
    
		flower.draw();
	}
}
}

use

public static void drawShapes() {
    
    
// 我们创建了一个 Shape 对象的数组.
	Shape[] shapes = {
    
    new Cycle(), new Rect(), new Cycle(),
	new Rect(), new Flower()};
	for (Shape shape : shapes) {
    
    
	shape.draw();
	}
}

More scalable

If you want to add a new shape, the cost of code changes using polymorphism is relatively low.
For the caller of the class (drawShapes method), just create an instance of the new class, and the cost of modification is very low.
For the case where polymorphism is not used, the if-else in drawShapes must be modified to a certain extent, and the change cost is higher

2. Method rewriting

What is rewriting

The subclass implements the method of the same name of the parent class, and the type and number of parameters are exactly the same. This situation is called override/rewrite/override.
Precautions

  1. Rewriting is completely different from overloading. Don't get confused (think about it, what are the rules for overloading?)
  2. Ordinary methods can be overridden, static modified static methods cannot be overridden.
  3. The access rights of the methods of the overridden neutron class cannot be lower than the access rights of the parent class.
  4. The return value type of the overridden method is not necessarily the same as that of the parent class (but it is recommended to write the same, except for special cases. Assuming that the return values ​​of the parent class and subclass methods are completely irrelevant, compilation errors will occur; the parent class and the child The return value of the class has a parent-child relationship.)
public class student extends people{
    
    
    public String school;
    public int age=10;
    @Override
    public void look(){
    
    
        System.out.println("学生在看书");
    }
}

The look function here is rewritten

The difference between rewriting and overloading

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45070922/article/details/112971941