This chapter will be discussed by an example JAVA Polymorphism of —— Object type conversion

<> Experiment topic : Total salary paid by the company :

<> requirement :

*
There is one abstract class , Class name is Employee.Employee Subclasses of are YearWorker,MonthWorker,WeekWorker.YearWorker Object receives salary by year ,MonthWorker Monthly salary ,WeekWorker Weekly salary .
* Employee Class has one abstract method :public abstract
earnings(); Subclasses must override the earnings() method , Give specific ways to receive remuneration .
*
There is one Company class , For this class Employee Object array as a member ,Employee The cells of an object array can be YearWorker Object's upper transformation object ,MonthWorker Object or WeekWorker Object's upper transformation object . Program output Company Total salary to be paid by the object in one year .
Source code : package salaries; abstract class Employee { public abstract double
earnings(); } //YearWorker Object receives salary by year , class YearWorker extends Employee { int n;
double ns; // Working years and annual salary YearWorker(int a, double b) // Constructor { n = a; ns = b; }
public double earnings() { return n * ns; } } //MonthWorker Monthly salary , class
MonthWorker extends Employee { int m; double ms; // Number of working months and monthly salary MonthWorker(int a,
int b) { m = a; ms = b; } public double earnings() { return m * ms; } } //
WeekWorker Weekly salary class WeekWorker extends Employee { int w; double ws; //
Number of working weeks and weekly salary WeekWorker(int a, double b) { w = a; ws = b; } public double earnings()
{ return w * ws; } } class Company { double sum; Employee[] employee; Company(
Employee[] employee) { this.employee = employee; } double pay() { for (int i = 0
; i < 30; i++) { sum += employee[i].earnings(); } return sum; } } public class
CompanySalary { public static void main(String[] args) { Employee[] employee =
new Employee[30]; // Co owned by the company 30 Employees // Divide employees into annual salary , a monthly salary , Weekly paid employee for (int i = 0; i < employee.
length; i++) { if (i % 3 == 0) employee[i] = new WeekWorker(1, 1000); // Upper transformation object
if (i % 3 == 1) employee[i] = new MonthWorker(1, 10000); if (i % 3 == 2)
employee[i] = new YearWorker(1, 200000); } Company company = new Company(
employee); System.out.println(" The total salary of the company is " + company.pay() + " element "); } }
<>JAVA Object transformation types of are divided into upward transformation and downward transformation

* Upward transformation
Use a subclass object as a parent type , Call defined in parent class , Methods implemented in subclasses ;
format : Parent class Object name 1 = new Subclass ()
example :Animal an1 = new Dog();

Example above :

employee[i] = new WeekWorker(1, 1000);

among employee[i] Is an array of objects defined in the parent class ,WeekWorker Is a subclass ,
WeekWorker(1, 1000) Is a subclass WeekWorker Constructor in ,
This is called in the parent class , Methods implemented in subclasses ,
That is, the so-called upward transformation .

* Downward transformation
If you want to call a method specific to a subclass , You need to cast an object of the parent type to a child type .
format : Subclass Object name 2 = ( Subclass ) Object name 1
notes : Object name 1 Object name of the parent class when pointing to the up transformation , Object name 2 Refers to the subclass object name defined at this time
example :
Animal an1 = new Dog();
Dog an2 = (Dog) an1;

<> Possible errors

1.The method Method name ()is undefined for the type Parent class name
If this error occurs , That is, in the downward transition , You are not defined in the parent class ××× method

resolvent : Define in the parent class

2. If you first A Subclass objects are used as parent types , However, the parent type at this time is cast to B Subclass type , Call subclass specific methods , This is wrong !

Simply put : You and your son A Identity exchange , You are A,A Eat with his wife , This is normal , But you want to be with your brother B My wife eats , What do you want? ?? Remember that you are A!! I want to eat , You have to A My brother's identity changed to B Brother can !

resolvent : keyword instanceof You can determine whether an object is a ( Class or interface ) Instance or subclass instance of .

Syntax format :
Object name A instanceof Class name A // Judge object name A Is it a class name A type
You can use one if Statement to determine the output
example :
if(an1 instanceof Dog) { Dog an2 = (Dog) an1; an2.shout(); } else {Systrm.out.
println(" Object of this type is not Dog type !");}

Technology