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);		
       }
}

 

你可能感兴趣的:(java-高级)