概述
父类
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
儿子的手:一双大手
孙子的手:一双小手
孙子的脚:一双小脚
最后
以上就是怕孤独中心为你收集整理的继承接口的全部内容,希望文章能够帮你解决继承接口所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复