java object oriented 17_ polymorphism (Polymorphism)

1. Overview of polymorphic

Polymorphism refers to the same method call, due to the different objects may have different behavior. In real life, the same method, specific implementation will be completely different.

Polymorphic necessary conditions:

  1. Inheritance is a prerequisite of polymorphism

  2. Subclasses override inherited methods

  3. References to parent child class object

Polymorphic Use:

  1. Made using the parent class method parameter, arguments may be any type of a subclass of

  2. Returns the parent class's methods do value type, the return value may be the subject of any subclass

2. achieve polymorphism

Multi-state mainly in the parent class and the parent class inherits one or more sub-classes override certain methods, a plurality of sub-classes override the same methods may exhibit different behavior.

[Example] based on multiple inheritance to achieve - feed the animals administrators

class Animal {
	public void eat() {
		System.out.println("动物 eat");
	}
}
class Dog extends Animal {
	@Override
	public void eat() {
		System.out.println("dog 吃骨头");
	}
}
class Pig extends Animal {
	@Override
	public void eat() {
		System.out.println("Pig 吃饲料");
	}
}
class Cat extends Animal {
	@Override
	public void eat() {
		System.out.println("cat 吃鱼儿");
	}
}
// 管理员
class Admin {
	/*
	 * 多态满足条件
	 * 1.Cat、Dag和Pig继承Animal
	 * 2.Cat、Dag和Pig重写了Animal的eat()方法
	 * 3.父类引用指向子类对象(实参赋值给形参)
	 * 此处如果没有使用多态,管理员需要分别给每一个动物添加一个喂食的方法!!!
	 */
	public void feedAnimal(Animal a) {
		a.eat(); // 动态绑定
	}
}
public class PolymorphismDemo {
	public static void main(String[] args) {
		// 初始化一个管理员对象
		Admin admin = new Admin();
		// 管理员给动物喂食
		admin.feedAnimal(new Dog());
		admin.feedAnimal(new Pig());
		admin.feedAnimal(new Cat());
	}
}

3. The reference data type conversion

Recalling the basic data type conversion:

[Example] The basic data type conversion Case

// 整数(int)转换成小数(double)
int x = 10;
double y = x;
// 小数(double)转换成整数(int)
double n = 2.2;
int m = (int)n;

And includes reference data type conversion upward transition and the downward transition.

Upcast: references to the parent class subclass object, are automatic type conversion.

Format: parent class type variable name = subclass object;

Downcast: subclass references to parent class object, belonging to the cast.

Format: subclass type variable name = (subclass type) parent class object;

[Example] reference type conversion case

// 父类
class Person {
	public void eat() {
		System.out.println("person eat ...");
	}
}
// 子类
public class Student extends Person {
	public void study() {
		System.out.println("Student study ...");
	}
}
// 测试类
public class PolymorphismDemo {
	public static void main(String[] args) {
		// 向上转型:父类引用指向子类对象
		Person p = new Student();
		p.eat(); // 调用Person的eat()方法
		// p.study(); // 编译失败,不能调用Student的study()方法
		
		// 向下转型:子类引用指向父类对象
		Student stu = (Student)p;
		stu.eat(); // 调用Person的eat()方法
		stu.study(); // 调用Student的study()方法
	}
}
  • Upcast:

    Advantages: hidden type subclass, improved scalability of the code, the process itself is polymorphic transformation upward.

    Cons: Only use the contents of the common parent class, subclass-specific method can not be called!

  • Downcast:

    Pros: After downcast, you can call the subclass-specific methods

    Cons: But upward transition risk, prone to abnormal ClassCastException!

ps: For the latest free documentation and instructional videos, please add QQ group (627,407,545) receive.

Published 55 original articles · won praise 0 · Views 790

Guess you like

Origin blog.csdn.net/zhoujunfeng121/article/details/104600464