Introduction:
A company has ordinary employees and managers, they have a lot in common, but there are some differences, such as salary problems, ordinary employees only ordinary wages, the manager after the performance of a certain percentage of bonuses. At this point we can define two classes of employee and manager, obviously there is a clear "is-a" relationship between the two classes---manager is also a company employee, and the "is-a" relationship is a feature of the inheritance relationship, so there is an inheritance relationship between employee and manager.
Body:
The following are the employee classes:
/** * Employee Class */public class Employee { private int no; Employee number private String name; Employee name private double salary; Employee Pay Public employee () { } public employee (int no,string name, double salary) {this . No = no;< C11/>this. name = name; This. salary = salary; } Public double getsalary () { return salary; }}
It is obviously wrong to define the manager class as follows:
/** * Manager class * error code */class Manager extends employee{ private double reward; Bonus public void Manager (int no,string name, double salary,double reward) {this . no = no; this. Salary = salary; this. Name = name; this. Reward = Reward; }}
The manager class inherits the employee class, so it inherits the employee's member variable, but the manager itself does not have the No,salary and name attributes, and the three properties of the employee are private and cannot be called by any class other than itself. Even if it's a subclass, what to do? As follows:
Class Manager extends employee{ private double reward; Bonus public Manager (int no,string name, double salary,double reward) { super (no,name,salary); Must be placed in the first line of the construction method this . Reward = reward; }}
Super (No,name,salary) is a shorthand for calling the Employee class public employee (int no,string name, double salary).
It should be mentioned that if you write super (), the empty construction method of its superclass is called, but if the constructor of its superclass is not displayed in the subclass, the empty construction method of its superclass is called by default, and if there is no empty constructor method for the superclass, a compilation error is reported.
For the manager, the salary is Slalary+reward, so the manager needs to rewrite the Getsalary () method, first look at the following code:
Error code public double Getsalary () { return reward + salary; }
This piece of code is wrong, why? Because subclasses cannot call private domains of their superclass directly. If it is obtained through the Getsalary () method of the superclass
Error code
Public double getsalary () {
Return reward +getsalary ();
}
Although this code does not error, but it is also wrong, although the superclass has getsalary (), but the subclass manager also inherited the method, and the subclass of the member variables and member methods of high precedence, so will be the first to call the subclass of the method, is now writing the method, This will cause an infinite number of recursive calls.
The correct wording is to use the Super keyword, as follows:
Public double getsalary () {
Return reward + super.getsalary ();
}
The similarities and differences between super and this "text Source: http://blog.csdn.net/anmei2010/article/details/4093118":
1) Super (parameter): Call one of the constructors in the superclass (should be the first statement in the constructor)
2) This (parameter): Invokes another form of the constructor in this class (should be the first statement in the constructor)
3) Super: It refers to members in the immediate parent class of the current object (used to access member data or functions in the parent class that are hidden in the immediate parent class, when the base class has the same member definition as in the derived class, such as: Super. Variable name super. member function name (argument)
4) This: it represents the current object name (which is prone to two semantics in the program, should use this to indicate the current object, and if the function's shape participates in the class member data with the same name, this is required to indicate the member variable name)
5) call Super () must be written in the first row of the subclass construction method, otherwise the compilation does not pass. The first statement of each subclass construction method is implicitly called super (), and if the parent does not have this form of constructor, it will be error-free at compile time.
6) Super () is similar to this (), except that super () calls the constructor of the parent class from the subclass, and this () invokes other methods within the same class.
7) Super () and this () must be placed in the first line of the construction method.
8) Although you can call a constructor with this, you cannot call two.
9) This and super can not appear in a constructor at the same time, because this is bound to call other constructors, the other constructors will inevitably have a super statement exists, so in the same constructor has the same statement, it loses the meaning of the statement, the compiler will not pass.
This () and super () all refer to objects, so they cannot be used in a static environment. Includes: static variable, static method, static statement block.
11) In essence, this is a pointer to this object, but super is a Java keyword.
Consolidating Java (iv)----super and Super ()