A question about the method override

Category: Java SE
 
win19890412
2011-08-01 03:04:53

Sponsored Links
Source:
import java.util. *;
Public class OverRide {
public static void main (String [] args)
{
Manager m = new Manager ("robbie", 2000,1986,11,8);
m.setBonus (500);

Employee [] staff = new Employee [3];
staff [0] = m;
staff [1] = new Employee ("harry", 20000,1984,12,3);
staff [2] = new Employee ("paul", 30000,1985,11,7);
for (Employee e: staff)
{
System.out.println ("name =" + e.getName () + "salary =" + e.getSalary ());
}
}

}

class Employee
{
private String name;
private double salary;
private Date hireDay;
public ; Employee (String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar g = new GregorianCalendar (year, month-1, day);
g.getTime ();
}

public String getName ()
{
return name;
}

public double getSalary ()
{
return salary;
}

public Date getHireDay ()
{
return (Date) hireDay.clone ();
}

public void raiseSalary ( double byPercent)
{
double raise = salary * byPercent/100;
salary + = raise;
}
}

class ; Manager extends Employee
{
private double bonus;
public Manager (String n, double s, int year, int ; month, int day)
{
super (n, s, year, month, day);
bonus = 0;
}

public void setBonus (double b)
{
bonus = b;
}

public double getSalary ()
{
double baseSalary = super.getSalary ();
return baseSalary + bonus;
}
}
Manager subclass which overrides the Employee class getSalary ( ) method. The book says the Employee class is referenced when e, e is called the Employee class getSalary () method, when e referenced Manager class, e invoked Manager class getSalary () method.

My question is a subclass override the parent class method, not that of the parent class to override it? Original parent class method does not it? But the book says how e can call the Employee class getSalary () method?

Sponsored Links

kl25868
2011-08-01 03:24:20
subclasses override superclass method, is indeed the parent class to cover, but you instantiate an Employee array, not its subclasses, call the Employee class getSalary () method has no question ...
senzhiren
2011-08-01 03:29:08
look at the polymorphic ~ ~ `

Manager m = new Manager (" robbie ", 2000,1986 , 11,8);
m.setBonus (500);

Employee [] staff = new Employee [3];
staff [0] = m;

you here is the Employee references pointing Manager object, so the call is to the Manager class methods, while other two points Employee object, so calling the method is Employee

another override the parent class method is not to say that the method does not exist, the objects produced by the parent class You can call, or using the super keyword in the subclass can call

yxjiayxjia
2011-08-01 03:48:12

subclasses override the parent class method, which means that a subclass object point The variables in the actual process will not be called by the calling parent class inherited getSalary () method, but a pointer to the parent class variable course, calling its own getSalary () method.
xgr_yzd
2011-08-01 04:04:01
looks lz We did not understand the meaning of ...... identification is completed
ABCDEF123456AAA
2011-08-01 04:06:31
import   java.util.*;
public   class   OverRide   {
public   static   void   main(String[]   args)
{
Manager   m=new   Manager("robbie",2000,1986,11,8);
m.setBonus(500);

Employee[]   staff=new   Employee[3];
staff[0]=m;
staff[1]=new   Employee("harry",20000,1984,12,3);
staff[2]=new   Employee("paul",30000,1985,11,7);
for(Employee   e:staff)
{
System.out.println("name="+e.getName()+"   salary="+e.getSalary());
}
}

}


class   Employee
{
private   String   name;
private   double   salary;
private   Date   hireDay;
public   Employee(String   n,double   s,int   year,int   month,int   day)
{
name=n;
salary=s;
GregorianCalendar   g=new   GregorianCalendar(year,month-1,day);
g.getTime();
}

public   String   getName()
{
return   name;
}

public   double   getSalary()
{
return   salary;
}

public   Date   getHireDay()
{
return   (Date)hireDay.clone();
}

public   void   raiseSalary(double   byPercent)
{
double   raise=salary*byPercent/100;
salary+=raise;
}
}


class   Manager   extends   Employee
{
    private   double   bonus;
public   Manager(String   n,double   s,int   year,int   month,int   day)
{
super(n,s,year,month,day);
bonus=0;
}

public   void   setBonus(double   b)
{
bonus=b;
}

public   double   getSalary()
{
double   baseSalary=super.getSalary();
System.out.println("this is manager method!");    //
return   baseSalary+bonus;
}


landlord can run it, I just subclass method you added the words, you look at the operation, Employee object which method is invoked in the end? Is rewritten before? Or rewritten?
jianjiuba
2011-08-01 04:12:23
Manager m = new Manager ("robbie", 2000,1986,11,8);
m.setBonus (500 );
Employee [] staff = new Employee [3];

staff [0] = m; this sentence corresponds
Employee employee_staff = new Manager ("robbie", 2000,1986,11,8);
so the back out of the way or the System Manager The method
subclass While covering superclass method but the parent class method in a subclass can still be invoked via display super.getSalary () to access
ooxueruanjianoo
2011-08-01 04:26:28
I cite a simple example

First: There is a father and son, now, my father has an apple "Daddy. getSalary ()", the son also has an apple "My son. getSalary ()"; total of two apples (apples likened methods, so Apple can not eat a)

followed: father let his son eating apples (Employee references pointing Manager object)
Employee [] staff = new Employee [3]; ;
staff [0] = m;
son, of course, to eat their own apple slightly! ! ! "Calling his son own getSalary ()"

again: Dad eat their own apples. "Call Dad own getSalary ()"
staff [1] = new Employee ("harry", 20000,1984,12,3);
staff [ 2] = new Employee ("paul", 30000,1985,11,7);

subclasses override (override) the parent class method, does not mean parent class method does not exist
personal point of view, to be correct!!!!!

mlxinyuan24
2011-08-01 04:35:03
LZ is "coverage" to confuse the word the.

so-called "coverage" is actually a function of the dynamic run-time binding.

simply, is to decide at run time which function to call.

PS: the parent class and subclass of the function information exist in the respective class space and objects irrelevant, does not produce so-called "coverage."
penguin1497
2011-08-01 04:50:28
polymorphism problem ...
dupei_0310
2011-08-01 04:54:45
so many people, so many text code Debating out, few people seriously look at the landlord, Khan

My question is subclasses override superclass method, not that of the parent class to override it? Original parent class method does not it? But the book says how e can call the Employee class getSalary () method?

Back to the landlord: Here's "coverage", but not with a new approach to sub-class replacement parent class method, they exist simultaneously;

answer is completed;

PS: As at runtime, which method calls which, it simply is who object whose method calls;
zhu27493663
2011-08-01 05:13:41
I mean rewriting getSalary () method after, Employee class getSalary () method has become a method of rewriting after that whoever calls are changed after the method call.
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.