Home > Back-end >  Subclasses override superclass method after why call or the parent class method?
Subclasses override superclass method after why call or the parent class method?

Time:09-25

Subclasses override superclass method, why boss. GetSalary (); Call is the parent class getSalary, rather than a Manager class? Inquire how to change can be called a subclass getSalary method?


Public class ManagerTest
{
Public static void main (String [] args)
{
Manager boss=new Manager (" Carl Cracker ", 80000198, 7,12,15);
Boss. SetBonus (5000);
Boss. The getSalary ();
The Employee staff []=new Employee [3].
The staff [0]=boss;
The staff [1]=new Employee (" Harry Hacker ", 50000198, 9,10,1);
The staff [2]=new Employee (" Tommy Tester ", 40000199 0,3,15);
For (the Employee e: staff)
System. The out. Println (" name="+ um participant etName () +", Salary="+ um participant etSalary ());
}
}

The class Employee
{
Public Employee (String n, double s, int year, int expressions using, int day
{
Name=n;
Salary=s;
GregorianCalendar calendar=new GregorianCalendar (year, expressions using 1, day);
HireDay=calendar. GetTime ();
}
Public String getName ()
{
return name;
}
Public double getSalary ()
{
Return the salary;
}
Public Date getHireDay ()
{
Return hireDay;
}
Public void raiseSalary (double byPercent)
{
Double raise=salary * byPercent/100;
Salary +=raise;
}
private String name;
private double salary;
Private Date hireDay;

}

The class Manager extends the Employee
{
Public Manager (String n, double s, int year, int the month, int day
{
Super (n, s, year, month, day);
Bonus=0;
}
Public double gerSalary ()
{
System. The out. Println (" * * * * * * * * * * * ");
Double baseSalary=super. GetSalary ();
Return baseSalary + bonus;
}
Public void setBonus (double) b
{
Bonus=b;
}
Private double bonus;
}

CodePudding user response:

Well, rewrite the grammar I believe that you already know, you is a subclass of gerSalary (), the parent class is getSalary (); Spelling errors, natural not rewritten

CodePudding user response:

LS vision is good

CodePudding user response:

Recently in the static block, every brother please answer, is that the static block in the subclass, whether can only call a static method of the parent, I tried it on found unable to call the parent class of ordinary method,

CodePudding user response:

reference 4 floor weixin_44540213 response:
recently in the static block, every brother please answer, is that the static block in the subclass, whether can only call a static method of the parent, I tried it on found unable to call the superclass method of ordinary


You want to understand first class load sequence, when static block loading, ordinary methods haven't loaded, so don't call,

CodePudding user response:

This kind of low-level mistakes can by add @ Override annotations to avoid completely

CodePudding user response:

reference 4 floor weixin_44540213 response:
recently in the static block, every brother please answer, is that the static block in the subclass, whether can only call a static method of the parent, I tried it on found unable to call the superclass method of ordinary

The static method as the class loading and loading,
  • Related