父类
public class Father {
private int money;
int weight;
int getWeight() {
return weight;
}
protected void setWeight(int w) {
weight = w;
}
}
private int money;
int weight;
int getWeight() {
return weight;
}
protected void setWeight(int w) {
weight = w;
}
}
子类
public class Son extends Father {
String hand;
public void setHand(String hand) {
this.hand = hand;
}
String getHand() {
return hand;
}
String hand;
public void setHand(String hand) {
this.hand = hand;
}
String getHand() {
return hand;
}
}
public class Grandson extends Son{
String foot;
public void setFoot(String foot) {
this.foot= foot;
}
String getfoot() {
return foot;
}
String foot;
public void setFoot(String foot) {
this.foot= foot;
}
String getfoot() {
return foot;
}
}
public class Example5_1 {
public static void main(String[] args) {
Son son = new Son();
Grandson grandson = new Grandson();
Father father = new Father();
son.setWeight(62);
son.setHand("一双大手");
grandson.setWeight(29);
grandson.setHand("一双小手");
grandson.setFoot("一双小脚");
System.out.println("儿子的重量:" + son.getWeight());
System.out.println("孙子的重量:" + grandson.getWeight());
System.out.println("儿子的手:" + son.getHand());
System.out.println("孙子的手:" + grandson.getHand());
System.out.println("孙子的脚:" + grandson.getfoot());
}
Son son = new Son();
Grandson grandson = new Grandson();
Father father = new Father();
son.setWeight(62);
son.setHand("一双大手");
grandson.setWeight(29);
grandson.setHand("一双小手");
grandson.setFoot("一双小脚");
System.out.println("儿子的重量:" + son.getWeight());
System.out.println("孙子的重量:" + grandson.getWeight());
System.out.println("儿子的手:" + son.getHand());
System.out.println("孙子的手:" + grandson.getHand());
System.out.println("孙子的脚:" + grandson.getfoot());
}
}
输出的结果是:
儿子的重量:62
孙子的重量:29
儿子的手:一双大手
孙子的手:一双小手
孙子的脚:一双小脚
孙子的重量:29
儿子的手:一双大手
孙子的手:一双小手
孙子的脚:一双小脚