第四章 面向对象——继承 课后作业:

1.设计Bird,Fish类,都继承抽象类Animal,实现其抽象方法info( ),并输出他们的信息。

 
  
 
  
/**
 * 哺乳类
 * @author lenovo
 *
 */
public abstract class Animal {
	private int age; // 年龄

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
/**
 * 无参构造方法
 */
	public Animal() {
		super();
	}

	public Animal(int age) {
		this.age = age;
	}
/**
 * 抽象方法
 */
	public abstract void info();

}
/**
 * 鸟类
 * @author lenovo
 *
 */
public class Bird extends Animal{
    private String color;   //颜色
    /**
     *  有参构造方法
     * @param age  年龄
     * @param color  颜色
     */
    public Bird(int age,String color) {
    	super(age);
    	this.color = color;
    }

	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	/**
	 * 调用抽象方法
	 */
	@Override
	public void info() {
		// TODO Auto-generated method stub
		System.out.println("我是一只"+getColor()+"的鸟!");
		System.out.println("今年"+getAge()+"岁了!");
	}
}
/**
 * 
 * @author 鱼类
 *
 */
public class Fish extends Animal {
   private int weight;
/**
 *  有参构造方法
 * @param age
 * @param weight
 */
public Fish(int age,int weight) {
	super(age);
	this.weight = weight;
}

public int getWeight() {
	return weight;
}

public void setWeight(int weight) {
	this.weight = weight;
}
/**
 * 调用抽象方法打印
 */
@Override
public void info() {
	// TODO Auto-generated method stub
	System.out.println("我是一只"+getWeight()+"斤重的鱼!");
	System.out.println("今年"+getAge()+"岁了!");
    }
}
/**
 * 
 * @author测试类
 *
 */
public class Test {
  public static void main(String[] args) {
	Animal b = new Bird(4,"红色") ; //创建新的对象并赋值
	b.info();		
	System.out.println("");
	Animal c = new Fish(5,2);
	c.info();
     }
  }

2.兜兜家养了俩只家禽:一只鸡和一只鸭。请用面对思想的封装,继承的特性进行描述。

/**
 * 
 * @author 家禽类
 *
 */
public abstract class Fowl {
  private String name; //昵称
  private String likefavorite; //喜欢的食物
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getLikefavorite() {
	return likefavorite;
}
public void setLikefavorite(String likefavorite) {
	this.likefavorite = likefavorite;
}
/**
 * 无参构造方法
 */
public Fowl() {
	super();
	// TODO Auto-generated constructor stub
}
/**
 * 有参构造方法
 * @param name
 * @param likefavorite
 */
public Fowl(String name, String likefavorite) {
	super();
	this.name = name;
	this.likefavorite = likefavorite;
}
/**
 * 抽象方法
 */
  public abstract void print();
  
}
/**
 * 
 * @author 鸡类
 *
 */
public class Chicken extends Fowl {
  private String  strain; //品种
 private String hobby; //爱好
 /**
  * 有参构造方法
  * @param name         昵称
  * @param likefavorite  爱吃的食物
  * @param strain        品种
  * @param hobby         爱好
  */
 public Chicken(String name, String likefavorite ,String strain,String hobby) {
	super(name, likefavorite);
	this.strain = strain;
	this.hobby = hobby;
}

public String getStrain() {
	return strain;
}

public void setStrain(String strain) {
	this.strain = strain;
}

public String getHobby() {
	return hobby;
}

public void setHobby(String hobby) {
	this.hobby = hobby;
}
/**
 * 调用抽象方法打印输出
 */
public void print() {
	 System.out.println("我叫:"+getName()+",是一只"+getStrain()+"!");
	 System.out.println("我喜欢吃"+getLikefavorite()+"!");
	 System.out.println("我会"+getHobby()+"!");
 } 
}
/**
 * 
 * @author 鸭类
 *
 */
public class Duck extends Fowl {
   private String kind; //种类
   private String hobby; //爱好
   /**
    * 有参构造方法
    * @param name          昵称
    * @param likefavorite  爱吃的
    * @param kind          种类
    * @param hobby         爱好
    */
public Duck(String name, String likefavorite,String kind,String hobby) {
	super(name, likefavorite);
	 this.kind = kind;
	 this.hobby = hobby;
}

public String getKind() {
	return kind;
}

public void setKind(String kind) {
	this.kind = kind;
}

public String getHobby() {
	return hobby;
}

public void setHobby1(String hobby) {
	this.hobby = hobby;
}
/**
 *调用抽象方法打印并输出
 */
public void print() {
	 System.out.println("我叫"+getName()+",是一只"+getKind()+"!");
	 System.out.println("我喜欢吃"+getLikefavorite()+"!");
	 System.out.println("我会"+getHobby()+"!");
}
/**
 * 
 * @author 测试类
 *
 */
public class Test1 {

	public static void main(String[] args) {
		Chicken chi = new Chicken("喔喔","虫子","芦花鸡","打鸣");
		chi.print();
		System.out.println("");
		Duck duck = new Duck("嘎嘎","小鱼虾","斑嘴鸭","游泳");
		duck.print();
	}
}

3.练习题:

 
 
/**
 * 
 *汽车类
 *
 */
public  abstract class MotoVehcle {
   private String no; //车牌号
   private int  brand; //品牌
public String getNo() {
	return no;
}
public void setNo(String no) {
	this.no = no;
}
public int  getBrand() {
	return brand;
}
public void setBrand(int brand) {
	this.brand = brand;
}
/**
 * 无参构造方法
 */
 public MotoVehcle() {
	 
 }
public MotoVehcle(String no, int  brand) {
	super();
	this.no = no;
	this.brand = brand;
}
/**
 * 抽象方法
 * @param days
 */
   public abstract int caiRent(int days);
}
/**
 * 
 * @author 小汽车类
 *
 */
public final class Car extends MotoVehcle {
   private int type; //型号

public int getType() {
	return type;
}

public void setType(int type) {
	this.type = type;
}


public Car(String no,int  brand,int type) {
	super(no, brand);
	this.type = type;
		// TODO Auto-generated constructor stub
	}

public int caiRent(int days) {
	if (type == 1) {           //宝马
	    return 500 * days;
	} else {
		if (type == 2) {       //别克
	    return 600 * days;

		} else {
	    return 300 * days;
		}
	}
}

}
/*
 * 大巴车类
 */
public final class Bus extends MotoVehcle {
   private int seatCount; //座位数

public int getSeatCount() {
	return seatCount;
}

public void setSeatCount(int seatCount) {
	this.seatCount = seatCount;
}


public Bus(int seatCount) {
	super();
	this.seatCount = seatCount;
}

public int caiRent(int days) {
	if(seatCount<=16) {  //金杯
		return 800*days;
	}else {              //金龙
		return 1500*days;
	}
}
   
}
/**
 * 
 * @author测试类
 *
 */
public class Test {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("欢迎您你来到汽车租赁公司!");
		System.out.print("请输入要租赁的天数:");
		int days = input.nextInt();
		System.out.print("请输入租赁的汽车类型(1.轿车       2.客车):");
		int tepy = input.nextInt();
		if (tepy == 1) {
			System.out.print("请输入要租赁的汽车品牌(1.宝马    2别克):");
			int brand = input.nextInt();
			if (tepy == 1) {
				System.out.println("1.宝马 550i");
			} else if (tepy == 2) {
				System.out.print("请输入要租赁的汽车品牌(2.商务舱GL8  3.林荫大道):");
				tepy = input.nextInt();
			}
			String no = "京BK5543";
			System.out.println("分配给你的车牌是:" + no);
			Car car = new Car(no, days, brand);
			System.out.println("\n\n您好,您需要支付的租赁费是:" + car.caiRent(days));
		} else {
			if (tepy == 1) {
				System.out.print("请输入要租赁的汽车品牌(1.金杯    2金龙):");
				int brand = input.nextInt();
			}
			System.out.print("请输入客车座位数:");
			int seatCount = input.nextInt();
			System.out.println("分配给你的号牌是:京AU8769");
			Bus bus = new Bus(seatCount);
			System.out.println("\n\n您好,您需要支付的租赁费是:" + bus.caiRent(days));
		}
	}
}


猜你喜欢

转载自blog.csdn.net/gz98411/article/details/80089439