继承接口

父类
public class Father {
 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;
 }
}

public class Grandson extends Son{
 
 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());
 }

}

输出的结果是:
儿子的重量:62
孙子的重量:29
儿子的手:一双大手
孙子的手:一双小手
孙子的脚:一双小脚
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值