类型的检测——向上转型 向下转型
向上转型:父类对象的引用指向子类对象,向下转型:向上转型的基础上再次指向子类的对象
1.向上转型

1package text5; 2 3public class Father { 4public void say(){ 5 System.out.println("father say()"); 6} 7public static void main(String[] args) { 8 Father son=new Son(); 9 son.say(); 10} 11} 12 13class Son extends Father{ 14 public void say(){ 15 System.out.println("son say()"); 16 } 17}
View Code
有时候使用向上转型会丢掉子类特有的方法,

1package text5; 2 3public class Father { 4public void say(){ 5 System.out.println("father say()"); 6} 7public static void main(String[] args) { 8 Father son=new Son(); 9 son.sayMe(); 10} 11} 12 13class Son extends Father{ 14 public void say(){ 15 System.out.println("son say()"); 16 } 17 public void sayMe(){ 18 System.out.println("son sayMe"); 19 } 20}
View Code
2.向下转型(强转)注意强转的方法

1package text5; 2 3public class Father { 4 public void say() { 5 System.out.println("father say()"); 6 } 7 8 public static void main(String[] args) { 9 // 向上转型 10 Father son = new Son(); 11 son.say(); 12 13 // 向下转型 14 Son son2 = (Son) son; 15 son2.sayMe(); 16 } 17} 18 19class Son extends Father { 20 public void say() { 21 System.out.println("son say()"); 22 } 23 24 public void sayMe() { 25 System.out.println("son sayMe"); 26 } 27}
View Code
动态绑定;

1package text5; 2 3public class Father { 4 public void say() { 5 System.out.println("father say()"); 6 } 7 8 public static void main(String[] args) { 9 // 向上转型 10 Father son = new Son(); 11 son.say(); 12 13 } 14} 15 16class Son extends Father { 17 public void say() { 18 System.out.println("son say()"); 19 } 20 21 public void sayMe() { 22 System.out.println("son sayMe"); 23 } 24}
View Code
静态绑定;!!!!

1package text5; 2 3public class Father { 4 private String name = "father"; 5 6 public static void say() { 7 System.out.println("father say()"); 8 } 9 10 public void say1() { 11 System.out.println("father say1()"); 12 } 13 14 /// 主运行程序 15 public static void main(String[] args) { 16 // 向上转型 17 Father son = new Son(); 18 System.out.println(son.name); 19 son.say1(); 20 son.say(); 21 22 Son f = (Son) son; 23 f.say(); 24 f.say1(); 25 } 26} 27 28class Son extends Father { 29 String name = "Son"; 30 31 public static void say() { 32 System.out.println("son say()"); 33 } 34 35 public void say1() { 36 System.out.println("son say1"); 37 } 38}
View Code
1.使用多态实现主人给宠物喂食:
2.使用多态实现主人领养宠物并与宠物玩耍
3.使用多态计算汽车租赁总租金
4.使用多态完善汽车租赁系统计价
父亲不能调用儿子的方法,需要强制转换
A.实现主人给宠物喂食:
Pet:

1package Animal2; 2 3public abstract class Pet { 4 private String name; 5 private int health; 6 private int love; 7 8 public Pet() { 9 } 10 11 public Pet(String name, int health, int love) { 12 this.name = name; 13 this.health = health; 14 this.love = love; 15 } 16 17 public void print() { 18 System.out.println("宠物的自白:"); 19 System.out.print("我的名字叫" + getName() + ",健康值是" + getHealth() + ",和主人的亲密度是" + getLove()); 20 } 21 22 public String getName() { 23 return name; 24 } 25 26 public void setName(String name) { 27 this.name = name; 28 } 29 30 public int getHealth() { 31 return health; 32 } 33 34 public void setHealth(int health) { 35 // 设置默认 36 if (health < 0 || health > 100) 37 health = 60; 38 this.health = health; 39 } 40 41 public int getLove() { 42 return love; 43 } 44 45 public void setLove(int love) { 46 if (love < 0 || love > 100) 47 love = 60; 48 this.love = love; 49 } 50 51 public abstract void eat(); 52}
View Code
Dog

1package Animal2; 2 3public class Dog extends Pet { 4 private String strain; 5 6 public Dog(String name, int health, int love) { 7 super(name, health, love); 8 // this.strain = strain; 9 } 10 11 public Dog(String name, int health, int love, String strain) { 12 super(name, health, love); 13 this.strain = strain; 14 } 15 16 // 接飞盘 17 public void getFrise() { 18 System.out.println("接飞盘啊"); 19 } 20 21 public Dog() { 22 // TODO Auto-generated constructor stub 23 } 24 25 public void print() { 26 System.out.println("宠物的自白"); 27 System.out.println("我的名字叫" + this.getName() + ",健康值是" + this.getHealth() + "和主人的亲密度是" + this.getLove() 28 + ",我是一只酷酷的" + strain); 29 } 30 31 public String getStrain() { 32 return strain; 33 } 34 35 public void setStrain(int strain) { 36 if(strain==1){ 37 this.strain = "拉布拉多"; 38 }else this.strain="博美"; 39 } 40 41 @Override 42 public void eat() { 43 // TODO Auto-generated method stub 44 if(getHealth()==100){ 45 System.out.println("狗狗已经饱了,不需要吃东西了。"); 46 }else { 47 System.out.println("带狗狗去吃骨头......"); 48 setHealth(getHealth() + 3); 49 System.out.println("狗狗的健康值为:"+getHealth()); 50 } 51 } 52 53}
View Code
Penguin

1package Animal2; 2 3public class Penguin extends Pet { 4 String sex; 5 6 public Penguin(String name, int health, int love, String sex) { 7 super(name, health, love); 8 this.sex = sex; 9 } 10 11 public Penguin(String name, int health, int love) { 12 super(name, health, love); 13 // this.sex=sex; 14 } 15 16 public Penguin() { 17 // TODO Auto-generated constructor stub 18 } 19 20 public void swim() { 21 System.out.println("会游泳啊"); 22 } 23 24 public void print() { 25 System.out.println("宠物的自白"); 26 System.out.println( 27 "我的名字叫" + this.getName() + ",健康值是" + this.getHealth() + "和主人的亲密度是" + this.getLove() + ",性别是" + sex); 28 } 29 30 public void setSex(int sex) { 31 if (sex == 1) { 32 this.sex = "Q仔"; 33 return; 34 } 35 this.sex = "Q妹"; 36 } 37 38 public void setSex(String sex) { 39 this.sex = sex; 40 } 41 42 @Override 43 public void eat() { 44 // TODO Auto-generated method stub 45 if (getHealth() == 100) { 46 System.out.println("企鹅已经饱了,不需要吃东西了。"); 47 } else { 48 System.out.println("带企鹅去吃骨头......"); 49 setHealth(getHealth() + 5); 50 System.out.println("qie的健康值为:" + getHealth()); 51 } 52 } 53 54}
View Code
Master:

1package Animal2; 2 3//主人与宠物玩 4public class Master { 5 public void play(Pet pet) { 6 if (pet instanceof Dog) { 7 Dog dog = (Dog) pet; 8 dog.getFrise(); 9 } else if (pet instanceof Penguin) { 10 Penguin pen = (Penguin) pet; 11 pen.swim(); 12 } 13 } 14 15 // 喂食物 16 public void feed(Pet pet) { 17 if (pet instanceof Dog) { 18 Dog dog = (Dog) pet; 19 dog.eat(); 20 } else if (pet instanceof Penguin) { 21 Penguin penguin = (Penguin) pet; 22 penguin.eat(); 23 } 24 } 25}
View Code
Test:

1package Animal2; 2 3import java.util.Scanner; 4 5public class Text { 6 public static void main(String[] args) { 7 Master master=new Master(); 8 Pet dog=new Dog(); 9 dog.setName("黑比"); 10 dog.setLove(68); 11 dog.setHealth(0); 12 ((Dog) dog).setStrain(1);//儿子的调用强转 13 master.feed(dog); 14 dog.print(); 15 System.out.println("*********************"); 16 17 Pet penguin=new Penguin(); 18 penguin.setName("QQ"); 19 penguin.setHealth(100); 20 penguin.setLove(76); 21 ((Penguin) penguin).setSex(1);//儿子的调用 22 penguin.print(); 23 24 } 25}
View Code
B.使用多态实现汽车总租金
首先定义一个机动车抽象类,
Vehicle:

1package Car; 2 3/** 4 * 汽车抽象类 5 * 6 * @author Administrator 7 * 8 */ 9public abstract class Vehicle { 10 private String no; 11 private String brand; 12 13 // 有参构造 14 public Vehicle(String no, String brand) { 15 this.brand = brand; 16 this.no = no; 17 } 18 19 // 抽象方法,计算汽车租赁价格 20 public abstract int cale(int days); 21 22 // 自动获取的方法 23 public String getNo() { 24 return no; 25 } 26 27 public void setNo(String no) { 28 this.no = no; 29 } 30 31 public String getBrand() { 32 return brand; 33 } 34 35 public void setBrand(String brand) { 36 this.brand = brand; 37 } 38 39}
View Code
Bus;

1package Car; 2 3public class Bus extends Vehicle { 4 private int seat;// 座位数 5 6 // 构造不要忘了 7 public Bus(String no, String brand, int seat) { 8 super(no, brand);// 自动 9 this.seat = seat; 10 // TODO Auto-generated constructor stub 11 } 12 13 @Override 14 public int cale(int days) { 15 if (seat < 16) 16 return days * 800; 17 else 18 return days * 1500; 19 } 20 21 public int getSeat() { 22 return seat; 23 } 24 25 public void setSeat(int seat) { 26 this.seat = seat; 27 } 28 29}
View Code
Car:

1package Car; 2 3/** 4 * 轿车类 5 * 6 * @author Administrator 7 * 8 */ 9public class Car extends Vehicle { 10 private String type;// 汽车型号 11 12 public Car(String no, String brand, String type) { 13 super(no, brand); 14 this.type = type; 15 } 16 17 @Override 18 public int cale(int days) { 19 if ("1".equals(type)) {// 代表550i 20 return days * 500; 21 } else if ("2".equals(type)) { 22 return days * 600; 23 } else 24 return 300 * days; 25 } 26 27 public String getType() { 28 return type; 29 } 30 31 public void setType(String type) { 32 this.type = type; 33 } 34 35}
View Code
Truck:

1package Car; 2 3/** 4 * 计算总的租价 5 * 6 * @author Administrator 7 * 8 */ 9public final class Truck extends Vehicle { 10 int ton;// 吨位 11 12 public Truck(String no, String brand, int ton) { 13 super(no, brand); 14 this.ton = ton; 15 } 16 17 @Override 18 public int cale(int days) { 19 // TODO Auto-generated method stub 20 return days * ton; 21 } 22}
View Code
Customer:

1package Car; 2 3 4public class Customer { 5private String id; 6private String name; 7public Customer(String id,String name){ 8 this.name=name; 9 this.id=id; 10} 11public int calcTotalRent(Vehicle motos[],int days){ 12 int sum=0; 13 for(int i=0;i<motos.length;i++) 14 sum+=motos[i].cale(days); 15 return sum; 16 } 17public String getId() { 18 return id; 19} 20public void setId(String id) { 21 this.id = id; 22} 23public String getName() { 24 return name; 25} 26public void setName(String name) { 27 this.name = name; 28} 29 30}
View Code
Test:

1package Car; 2 3import java.util.Scanner; 4 5public class Test { 6 public static void main(String[] args) { 7 int days;// 租赁天数 8 int totalRent;// 总租赁费用 9 Vehicle motos[] = new Vehicle[5]; 10 motos[0] = new Car("宝马x5", "豫DX56432", "1"); 11 motos[1] = new Car("宝马x6", "豫DX51112", "2"); 12 motos[2] = new Car("金龙", "豫DX99832", "3"); 13 motos[3] = new Bus("别克林荫大道", "豫DX99832", 34); 14 motos[4] = new Truck("比亚迪", "豫DX99832", 8); 15 // 控制台输入 16 // 1、客户租赁的多辆汽车信息及租赁天数 17 Customer customer = new Customer("1", "万方"); 18 // 2、计算总租赁费用 19 System.out.println("总租金为:" + customer.calcTotalRent(motos, 5)); 20 } 21}
View Code
案例一:
Animal:

1package text2; 2 3public class Animal { 4private String name; 5private int age; 6 7public String getName() { 8 return name; 9} 10public void setName(String name) { 11 this.name = name; 12} 13public int getAge() { 14 return age; 15} 16public void setAge(int age) { 17 this.age = age; 18} 19public void cry(){ 20 System.out.println("不知道怎么叫"); 21 22} 23}
View Code
Cat:

1package text2; 2 3public class Cat extends Animal{ 4 5public void cry(){ 6 System.out.println("喵喵叫"); 7} 8}
View Code
Dog;

1package text2; 2 3public class Dog extends Animal{ 4public void cry(){ 5 System.out.println("汪汪叫"); 6} 7}
View Code
Test:

1package text2; 2 3public class Tset { 4public static void main(String[] args){ 5 Cat cat=new Cat(); 6 cat.cry(); 7 Dog dog=new Dog(); 8 dog.cry(); 9 Animal an=new Cat(); 10 an.cry(); 11 an=new Dog(); 12 an.cry(); 13} 14}
View Code
案例二;

Animal:

1package text2; 2 3public class Animal { 4private String name; 5private int age; 6 7public String getName() { 8 return name; 9} 10public void setName(String name) { 11 this.name = name; 12} 13public int getAge() { 14 return age; 15} 16public void setAge(int age) { 17 this.age = age; 18} 19public void cry(){ 20 System.out.println("不知道怎么叫"); 21 22} 23public void eat(){ 24 25} 26}
View Code
Cat;

1package text2; 2 3public class Cat extends Animal{ 4 5public void cry(){ 6 System.out.println("喵喵叫"); 7} 8public void eat(){ 9 System.out.println("猫喜欢吃鱼"); 10} 11}
View Code
Dog:

1package text2; 2 3public class Dog extends Animal{ 4public void cry(){ 5 System.out.println("汪汪叫"); 6} 7public void eat(){ 8 System.out.println("狗喜欢吃骨头"); 9} 10}
View Code
Food;

1package text2; 2 3public class Food { 4String name; 5public void showname(){ 6 7} 8 9}
View Code
Fish:
Bone;

1package text2; 2 3public class Bone extends Food{ 4public void showname(){ 5System.out.println("我是骨头"); 6} 7 8}
View Code
MAster:

1package text2; 2 3public class Master { 4//给动物喂食物 5 public void feed(Animal an,Food f)//灵活 6 { 7 an.eat(); 8 f.showname(); 9 } 10 11}
View Code
Test;

1package text2; 2 3public class Tset { 4public static void main(String[] args){ 5 Master master=new Master(); 6 master.feed(new Dog(), new Bone()); 7 8} 9}
View Code