Java-类的继承的初步运用

示例1

package com.leiheduixiang.a;

public class Pet {
	private String name;
	private int health;
	private int love;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHealth() {
		return health;
	}
	public void setHealth(int health) {
		this.health = health;
	}
	public int getLove() {
		return love;
	}
	public void setLove(int love) {
		this.love = love;
	}
	
}



package com.leiheduixiang.a;

public class Penguin extends Pet{
	public static final String SEX_MALE="雄";
	public static final String SEX_FEMALE="雌";
	private String sex="未知";

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	public void Print() {
		   System.out.println("企鹅的名字是:"+this.getName()+",健康值是:"+this.getHealth()+",和主人亲密度是:"
	                 +this.getLove()+",性别是:"+this.sex);
	   }
		
}



package com.leiheduixiang.a;

public class Dog extends Pet{
	private String strain="未知";

	public String getStrain() {
		return strain;
	}

	public void setStrain(String strain) {
		this.strain = strain;
	}
	   public void Print() {
		   System.out.println("狗的名字是:"+this.getName()+",健康值是:"+this.getHealth()+",和主人亲密度是:"+this.getLove()
		                  +",品种是:"+this.strain);
	   }
		
}



package com.leiheduixiang.a;

public class Test3 {
	public static void main(String[] args) {  
		Dog dog=new Dog();
		dog.setName("小白");
		dog.setLove(99);
		dog.setHealth(100);
		dog.setStrain("土狗");
		dog.Print();
		
		Penguin penguin=new Penguin();
		penguin.setName("小灰");
		penguin.setSex("雌");
		penguin.setHealth(100);
		penguin.setLove(98);
		penguin.Print();
	}
}

示例2

package com.leiheduixiang.a;

public class Pet {
	private String name;
	private int health;
	private int love;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHealth() {
		return health;
	}
	public void setHealth(int health) {
		this.health = health;
	}
	public int getLove() {
		return love;
	}
	public void setLove(int love) {
		this.love = love;
	}	
}



package com.leiheduixiang.a;

public class Penguin extends Pet{
	public static final String SEX_MALE="雄";
	public static final String SEX_FEMALE="雌";
	private String sex="未知";
	private String happy="未知";

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public String getHappy() {
		return happy;
	}

	public void setHappy(String happy) {
		this.happy = happy;
	}

	public void Print() {
		   System.out.println("企鹅的名字是:"+this.getName()+",健康值是:"+this.getHealth()+",和主人亲密度是:"
	                 +this.getLove()+",性别是:"+this.sex+",爱好是:"+this.happy);
	   }		
}



package com.leiheduixiang.a;

public class Dog extends Pet{
	private String strain="未知";
	private String jineng="未知";

	public String getStrain() {
		return strain;
	}
	public void setStrain(String strain) {
		this.strain = strain;
	}
	public String getJineng() {
		return jineng;
	}
	public void setJineng(String jineng) {
		this.jineng = jineng;
	}
	public void Print() {
		System.out.println("狗的名字是:"+this.getName()+",健康值是:"+this.getHealth()+",和主人亲密度是:"
	                 +this.getLove()+",品种是:"+this.strain+",技能是:"+this.jineng);
	}	
}



package com.leiheduixiang.a;

public class Test {
	public static void main(String[] args) {
		Dog dog=new Dog();
		dog.setName("啸天");
		dog.setLove(99);
		dog.setHealth(100);
		dog.setStrain("天狗");
		dog.setJineng("吞天噬地");
		dog.Print();
		
		Penguin penguin=new Penguin();
		penguin.setName("小槿");
		penguin.setSex("雌");
		penguin.setHealth(100);
		penguin.setLove(98);
		penguin.setHappy("吃鱼、跳水和泡澡");
		penguin.Print();
		
	}	
}

关于汽车继承的一个练习

package com.leiheduixiang.a;

public abstract class MotoVehicle {
	public int No;//车牌
	public String brand;//品牌
	public String color;//颜色
	public int Mileage;//里程
	
	public abstract int CalcRent(int days);//出租天数

}



package com.leiheduixiang.a;

public final class Car extends MotoVehicle{
	public static final String TYPE_CAR1="别克商务舱GL8";
	public static final String TYPE_CAR2="宝马550i";
	public static final String TYPE_CAR3="别克林荫大道";
	public String type;
	public int Days=0;	
	public Car(String type) {
		this.type=type;
	}
	public Car(int No,String type) {
		this.No=No;
		this.type=type;
	}
	public int CalcRent(int days) {
		int DayRent=CalcDayRent(this.type);
		int Rent=DayRent*days;
		System.out.println("车型是:"+this.type+",租期是:"+days+"天,租金是:"+Rent+"元");
		return Rent;
	}
	private int CalcDayRent(String type) {
		int DayRent=0;
		switch(type) {
		case Car.TYPE_CAR1:
			DayRent=600;
			break;
		case Car.TYPE_CAR2:
			DayRent=500;
			break;
		case Car.TYPE_CAR3:
			DayRent=300;
			break;
		}
		return DayRent;
	}
}



package com.leiheduixiang.a;

public final class Bus extends MotoVehicle{
	public int SeatCount;//座位数
	
	public Bus(final int SeatCount) {
		this.SeatCount=SeatCount;
	}
	public Bus(int No,int SeatCount) {
		this.No=No;
		this.SeatCount=SeatCount;
	}
	public int CalcRent(int days) {
		int DayRent=CalcDayRent(this.SeatCount);
		int Rent=DayRent*days;
		System.out.println("座位数是:"+this.SeatCount+",租期是:"+days+"天,租金是:"+Rent+"元");
		return Rent;
	}
	private int CalcDayRent(int SeatCount) {
		int DayRent=0;
		if(SeatCount<=16) {
			DayRent=800;
		}else {
			DayRent=1500;
		}
		return DayRent;
	}

}



package com.leiheduixiang.a;

public class Test {
	public static void main(String[] args) {
		Car car=new Car(Car.TYPE_CAR1);
		car.CalcRent(6);
		
		Bus bus=new Bus(12);
		bus.CalcRent(9);		
       }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我说、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值