子类通过方法的重写机制可以隐藏继承父类的方法, 把父类的状态和行为改变为子类自己的状态和行为. 假如父类中有一个方法myMethod(),一旦子类重写了超类的方法myMethod() ,就隐藏了继承的方法 . 子类对象在调用方法 myMethod()时,运行结果一定是重写了方法体的实现结果. 重写方法既可以操作继承的成员变量和继承的成员方法 ,也可以操作子类新增加的成员变量和新增加的成员方法 ,但无法操作被子类隐藏的成员变量和成员方法 . 如果子类想操作被隐藏的成员变量和成员方法 ,就必须在子类的方法中调用并且使用super.
public class Test3_18 {
public static void main(String[] args) {
// TODO Auto-generated method stub
AnotherEmployee first = new AnotherEmployee("John",177,73.8d,4000,2016,10,20) ;
System.out.println("员工信息\t姓名\t身高\t体重\t月薪\t入职时间");
System.out.println("员工信\t"+first.getName()+"\t"+first.getHeight()+"\t"+first.getWeight()+"\t"+first.getSalary()+"\t"+first.getHireDay());
AnotherManager mfirst =new AnotherManager("Jerry",187,80.8d,8000,2006,7,20) ;
mfirst.setBonus(10000) ;
System.out.println("经理信息\t姓名\t身高\t体重\t月薪\t入职时间\t奖金");
System.out.println("经理信息\t"+mfirst.getName()+"\t"+mfirst.getHeight()+"\t"+mfirst.getWeight()+"\t"+mfirst.getSalary()+"\t"+mfirst.getHireDay()+"\t"+mfirst.getBonus());
}
}
//
public class AnotherPeople {
private String name ;
private double height ,weight ;
public AnotherPeople() {
name = "" ;
height = 170 ;
weight =70 ;
}
public AnotherPeople(String name ,double height ,double weight ) {
this.name = name ;
this.height = height ;
this.weight = weight ;
}
public void setName(String name ) {
this.name = name ;
}
public void setHeight(double height) {
this.height = height ;
}
public void setWweight(double weight) {
this.weight = weight ;
}
public String getName() {
return this.name ;
}
public double getHeight() {
return this.height ;
}
public double getWeight() {
return this.weight ;
}
}
package cn.com.site2;
import java.util.*;
public class AnotherEmployee extends AnotherPeople{
private double salary ; // 子类增加的薪水
private Date hireDay ; // 增加了雇佣时间
public AnotherEmployee() {
// 无参无操作
}
public AnotherEmployee(String name ,double height , double weight ,double salary ,int year , int month ,int day ) {
super(name,height,weight) ;
this.salary = salary ;
GregorianCalendar calendar = new GregorianCalendar(year ,month-1,day) ;
hireDay = calendar.getTime();
}
public void setSalary(double salary) {
this.salary = salary ;
}
public void setHireDay(int year ,int month ,int day ) {
GregorianCalendar calendar = new GregorianCalendar(year ,month-1,day) ;
hireDay = calendar.getTime() ;
}
public double getSalary() {
return this.salary ;
}
public Date getHireDay() {
return this.hireDay ;
}
public void raiseSalary(double byPercent) { // 增加了加薪的方法
double raise = salary*byPercent /100 ;
salary +=raise ;
}
}
public class AnotherManager extends AnotherEmployee{
private double bonus ; // 增加了奖金
public AnotherManager() {
}
public AnotherManager(String name , double height , double weight ,double salary , int year , int month ,int day ) {
super(name ,height ,weight,salary,year,month,day) ;
bonus = 0 ;
}
public void setBonus(double bonus) {
this.bonus = bonus ;
}
public double getBonus() {
double baseSalary = super.getSalary();
return bonus+baseSalary ;
}
}
输出 :
员工信息 姓名 身高 体重 月薪 入职时间
员工信 John 177.0 73.8 4000.0 Thu Oct 20 00:00:00 CST 2016
经理信息 姓名 身高 体重 月薪 入职时间 奖金
经理信息 Jerry 187.0 80.8 8000.0 Thu Jul 20 00:00:00 CST 2006 18000.0
标签:salary,JAVA,name,weight,double,笔记,height,学习,public From: https://blog.51cto.com/u_15970235/6063949