public abstract class Animal { private int age; public Animal(){ } public Animal(int age){ this.age = age; } public int getAge(){ return age; } public void setAge(int age){ this.age =age; } public abstract void introduce(); } class Fish extends Animal{ private int weight; public Fish(){ } 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 introduce() { System.out.println("鱼的年龄是:" + super.getAge() + ",重量是:" + weight + "kg"); } public void swim(){ System.out.println("我是一条鱼,我正在游泳呢... ..."); } } class Bird extends Animal{ private String color; public Bird(){ } 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 introduce() { System.out.println("鸟的年龄是:" + super.getAge() + ",颜色是:" + color); } public void fly(){ System.out.println("我是一只鸟,我正在飞呢... ..."); } } class AnimalText { public static void main(String[] args) { Fish fish = new Fish(17,2); fish.introduce(); fish.swim(); Bird bird = new Bird(16,"彩色"); bird.introduce(); bird.fly(); } }
public abstract class MotoVehical { private String no; private String brand; public MotoVehical(){ } public MotoVehical(String no, String brand) { this.no = no; this.brand = brand; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public abstract int calcRent(int days); } class Car extends MotoVehical { private String type; public Car() { } public Car(String no, String brand, String type) { super(no, brand); this.type = type; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public int calcRent(int days) { if (this.type.equals("0")) { return 600 * days; } else if (this.type.equals("1")) { return 500 * days; } else if (this.type.equals("2")) { return 300 * days; } else { System.out.println("您输入的车型不存在!"); return 0; } } } class Bus extends MotoVehical { private int seatCount; public Bus(){ } public Bus(String no,String brand,int seatCount){ super(no,brand); this.seatCount = seatCount; } public int getSeatCount() { return seatCount; } public void setSeatCount(int seatCount) { this.seatCount = seatCount; } @Override public int calcRent(int days) { if(this.seatCount > 16){ return 1500 * days; }else{ return 800 * days; } } } class Truck extends MotoVehical { private int weight; public Truck(String no,String brand,int weight){ super(no,brand); this.weight = weight; } @Override public int calcRent(int days) { return 50 * days * this.weight; } } class MotoVehicalTest { public static void main(String[] args) { MotoVehical[] motoArray = new MotoVehical[5]; motoArray[0] = new Car("豫A 00099999", "宝马550i", "1"); motoArray[1] = new Car("豫B 00088888", "宝马550i", "1"); motoArray[2] = new Car("豫C 00066666", "别克林荫大道", "2"); motoArray[3] = new Bus("豫D 00011111", "金龙", 34); motoArray[4] = new Truck("豫E 000333333", "东方红", 50); int totalRent = MotoVehicalTest.calcRent(motoArray, 5); System.out.println("租5天的总租金是:" + totalRent); } public static int calcRent(MotoVehical[] motoArray,int days){ int totalRent = 0; for(int i = 0; i<motoArray.length;i++){ totalRent += motoArray[i].calcRent(days); } return totalRent; } }
public class Pet { private String name; protected int health=100; protected int love = 0; public Pet() { super(); } public Pet(String name, int health, int love) { super(); this.name = name; this.health = health; this.love = 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; } } class Dog extends Pet { private String strain; public Dog() { super(); } public Dog(String strain) { super(); this.strain = strain; } public void catchingFlyDisc(){ System.out.println("小狗正在接飞盘!"); super.health =super.health-10; System.out.println("健康值:" + super.health); super.love = super.love +5; System.out.println("亲密度:" + super.love); } public String getStrain() { return strain; } public void setStrain(String strain) { this.strain = strain; } } class Penguin extends Pet { private String sex; public Penguin() { super(); } public Penguin(String sex) { super(); this.sex = sex; } public void swimming(){ System.out.println("企鹅正在接飞盘!"); super.health =super.health-10; System.out.println("健康值:" + super.health); super.love = super.love +5; System.out.println("亲密度:" + super.love); } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } class Master { private String name; private int age; public Master() { super(); } public Master(String name, int age) { super(); this.name = name; this.age = age; } public void play(Pet pet){ if(pet instanceof Dog){ Dog dog = (Dog)pet; dog.catchingFlyDisc(); }else if(pet instanceof Penguin){ Penguin p = (Penguin)pet; p.swimming(); } } public static Pet getPet(String typeId){ if (typeId.equals("1")){ return new Dog(); }else if (typeId.equals("2")){ return new Penguin(); }else { return null; } } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } class Test { public static void main(String[] args) { Master m = new Master(); Pet pet = Master.getPet("1"); if (pet instanceof Dog){ System.out.println("领养的是狗狗"); }else if (pet instanceof Penguin){ System.out.println("领养的是企鹅"); } m.play(new Dog()); m.play(new Penguin()); } }