A simple example of polymorphism in java: First there is an Animal class, which has two subclasses: Cat and Dog; then there is a say method in Animal. When Cat calls this method, the output is "Kitten" "Meow meow meow", when Dog calls this method, the output is "puppy bark woof woof".
[Related learning recommendations: java basic tutorial】
Polymorphism:
Polymorphism
refers to the same entity having multiple forms at the same time. It is an important feature of object-oriented programming (OOP). If a language only supports classes but not polymorphism, it can only mean that it is object-based rather than object-oriented.
Polymorphism occurs between objects of two classes with an inheritance relationship, so it is not like method overloading (which occurs in a class) that occurs during compilation (that is, it is determined), but Occurs (determines) during runtime.
A reference of a parent class type can point to an object of any of its subclasses
[Different] objects in the [same] class domain will have [different] results when executing the [same] method Performance
There is a relatively classic example of polymorphism:
There is an Animal class, which has two subclasses: Cat and Dog. There is a say method in Animal , when Cat calls this method, the output is "kitten meow meow", when Dog calls this method, the output is "puppy woof woof", this is the implementation of Java polymorphism.
Here we write another one:
—Parent class: Person.java
import java.util.Scanner; public class Person { public int salary; public int allSalary(){ return 0; } public static void main(String[] args) { Person p = null; for (int n = 0; n < 3; n++) { @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); System.out.println("请输入一个1-3的数字:\n1 is Teacher\n2 is Scientis\n3 is Waiter"); int i = sc.nextInt(); //分别指向了不同的子类,输出的结果是不一样的 if (i == 1) { p = new Teacher(5000); System.out.println(p.allSalary()); } else if (i == 2) { p = new Scientist(5000); System.out.println(p.allSalary()); } else if (i == 3) { p = new Waiter(5000); System.out.println(p.allSalary()); } else { System.out.println("?\n请输入1-3"); } } } }
—Subclass: Scientist .java
public class Scientist extends Person{ public Scientist(int salary){ this.salary = salary; } public int allSalary(){ int aa = salary*12+36500; System.out.print("五五开一年的工资:"); return aa; } }
Waiter.java
public class Waiter extends Person{ public Waiter(int salary){ this.salary = salary; } public int allSalary(){ int aa = salary*12; System.out.print("服务员一年的工资:"); return aa; } }
Teacher .java
public class Teacher extends Person{ public Teacher(int salary){ this.salary = salary; } public int allSalary(){ int aa = salary*12+3650; System.out.print("教师一年的工资:"); return aa; } }
If you want to know more about programming learning, please pay attention to the php training column!
The above is the detailed content of Detailed explanation of simple examples of java polymorphism. For more information, please follow other related articles on the PHP Chinese website!