JAVA继承 – 记忆角落

JAVA继承

/ 2评 / 0
多重继承:
练习2
   已知(1)有Father、Son、Garndson类,(2)Father有变量money,weight ,(3)Son有变量 hand并继承了Father(4)Grandson 本身有变量 foot并继承了Son

class Father {//父亲
	private  double money;
	private int weight;
	public double getMoney(){
		return money;
	}
	public void setMoney(double newMoney){
		money=newMoney;
	}
	public int getWeight(){
		return weight;
	}
	public void setWeight(int newWeight){
		 weight=newWeight;
	}
}
class Son extends Father{//儿子
	private String hand;

	public String getHand(){
		return hand;
	}
	public void setHand(String newHand){
		hand=newHand;
	}
}
class Grandson extends Son{//孙子
	private String foot;
	public String getFoot(){
		return foot;
	}
	public void setFoot(String newFoot){
		foot=newFoot;
	}
}
public class Test {//测试
	public static void main(String[] args) {
		Son son=new Son();
        Grandson grandson=new Grandson();
        son.setWeight(60);
        son.setHand("一双大手");
        grandson.setWeight(30);
        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());
	}
}

2条回应:“JAVA继承”

  1. themebetter说道:

    每天记录,不错,加油。

  2. jiom说道:

    @themebetter:你的模板也不错!

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注