<> Polymorphic array

The definition type of the array is the parent type , The actual element type saved inside is the subclass type
package com.hspedu.polyarr; public class Person { private String name; private
int age; public Person(String name, int age) { this.name = name; this.age = age;
} public String getName() { return name; } public void setName(String name) {
this.name = name; } public int getAge() { return age; } public void setAge(int
age) { this.age = age; } public String say() { return name + "\t" +age; } }
package com.hspedu.polyarr; public class Student extends Person{ private double
score; public Student(String name, int age, double score) { super(name, age);
this.score = score; } public String say() { return super.say()+"score=" + score;
} } package com.hspedu.polyarr; public class Teacher extends Person{ private
double salary; public Teacher(String name, int age, double salary) { super(name,
age); this.salary = salary; } public double getSalary() { return salary; }
public void setSalary(double salary) { this.salary = salary; } // Override method of parent class
@Override public String say() { return super.say() +"salary="+salary; } }
package com.hspedu.polyarr; public class ployArray { public static void main(
String[] args) { Person [] persons = new Person[5]; persons[0] = new Person(
"jack",20); persons[1]= new Student("tom",21,66); persons[2] = new Student(
"heihuj",26,98); persons[3]= new Teacher("semi",18,8734); persons[4] = new
Teacher("ligui",12,5849); // Traversal array for (int i = 0; i < persons.length ; i++) {
System.out.println( persons[i].say()); // Compilation type is persons , See the following for operation type // Dynamic binding } } }

Technology
©2019-2020 Toolsou All rights reserved,
evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possible Experiment 4 Automated test tools - software test mysql Export data sql sentence _mysql according to sql Query statement export data Create a thread ——— Javaweb (3) Data structure experiment ( three )—— Stacks and queues TS stay vue2 Writing in the project web Front end signature plug-in _signature_pad Plug in implements electronic signature function docker Where is the image stored Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control springboot How to get reality in ip address