【面对对象基础练习(多态应用)——去宠物店买猫买狗】

       去宠物店去买猫和狗,
       * 从从控制台输入1,购买一只猫,
       * 输入2,购买一只狗,
       * 输入其他则提示重新输入
       * 当狗和猫的只数总共为5只的时候去结算,
       * 假设狗的价格为每只100元,猫的价格为每只200元,
       * 计算最后的总价格

import java.util.Scanner;

class Animal{
	public void say() {
		System.out.println("动物叫……");
	}	
	public double getPrice() {
		return 0;
	}
}

class Dog extends Animal{
	/**
	 * 重写动物叫的方法
	 */
	@Override
	public void say() {
		System.out.println("汪汪汪……");
	}	
	public void lookHouse() {
		System.out.println("狗看家……");
	}
	public double getPrice() {
		return 200;
	}
}

class Cat extends Animal{
	@Override
	public void say() {
		System.out.println("喵喵喵……");
	}	
	public void catchMouse() {
		System.out.println("猫抓老鼠……");
	}
	public double getPrice() {
		return 100;
	}
}
public class Test1 {
	public static void main(String[] args) {

		Animal a1=new Animal();
		
		Animal  d1=new Dog();
		
		Animal  c1=new Cat();
		//父类型的引用可以指向子类型的对象
		//在编译期间,引用是属于父类型
		//在运行期间,会调用子类型中重写的方法
		a1.say();
		d1.say();
		c1.say();
		//在编译期间,d1和c1两个引用都是属于animal类型
		//只能调用到在父类中已经定义过的方法
		//而不能调用到子类中所特有的方法
//		d1.lookHouse();
//		c1.catchMouse();
		
		System.out.println("-------------------------");
		
		//类型转换
		/**
		 * 
		 * 1、向上类型转换
		 *    子类型转父类型,自动类型转换,隐式类型转换
		 */
		Dog d2=new Dog();
		
		d2.lookHouse();
		d2.say();
		
		Animal a2=d2;
		System.out.println(d2==a2);
		//多态,运行时候调用子类重写方法
		a2.say();
		//转成父类型后,只能调用父类中已经定义过的方法
		//a2.lookHouse();
		
		/*
		 * 2、向下类型转换
		 *     父类型转子类型,强制类型转换,显示类型转换
		 * 
		 */
		Animal a3=new Dog();
		Dog d3=(Dog)a3;
		System.out.println(a3==d3);
		//强转子类型后就可以调用子类中定义的特有方法
		d3.lookHouse();
		
		Animal a4=new Cat();
		
		/*
		 * instanceof关键字
		 * 主要用于判断a4引用所指向的对象
		 * 是否属于Cat类型,而不是判断
		 * a4引用本身的类型
		 */
		boolean f=a4 instanceof Cat;
		if(f) {
			System.out.println("-----可以进行类型转换----");
		
		Cat c4=(Cat)a4;
		c4.catchMouse();
		c4.say();
		}else {
			System.out.println("------不可以进行类型转换------");
		}
		
		
		System.out.println("---------------------------------");
		//调用买狗
		Dog d5=(Dog)buyDog();
		d5.lookHouse();
		d5.say();
		//调用买猫
		Cat c5=(Cat)buyCat();
		c5.catchMouse();
		c5.say();
		
		//购买动物
		Animal a5 =buyAnimal(1);
		a5.say();
		Animal a6=buyAnimal(2);
		a6.say();
				
		System.out.println("---------------------------------------");
		//创建狗
		Dog d6=new Dog();
		//调用狗叫
		dogSay(d6);
		//创建猫
		Cat c6=new Cat();
		//调用猫叫
		catSay(c6);
		
		
	    animalSay(d6);
	    animalSay(c6);
	    
	    System.out.println("----------------------");
		//购买5只动物(猫或狗)
		Animal[] animals = buyAnimal();
		//计算总价格
		double price = 0;
		for(int i=0;i<animals.length;i++) {
			Animal animal = animals[i];
			//根据多态应用,如果是狗,就获取狗的价格
			//如果是猫,就获取猫的价格,不用去判断
			price += animal.getPrice();
		}
		System.out.println("总价格:"+price);
	
	}
      public static Animal buyDog() {
    	  Dog dog =new Dog();
    	  return dog;
      }
      public static Animal buyCat() {
    	  Cat cat =new Cat();
    	  return cat;
      }
      public static Animal buyAnimal(int type) {
 	  Dog dog =new Dog();
//    	  return dog;
   	  Cat cat =new Cat();
//    	  return cat;
 	  if(type==1) {
    		  return dog;
    	  }else {
    		  return cat;
    	  }
      }
      
      //传入狗,调用狗叫
      public static void dogSay(Dog dog) {
    	  dog.say();
      }
      public static void catSay(Cat cat) {
    	  cat.say();
      }
      
      public static void animalSay(Animal animal) {
    	  animal.say();
      }      
     
      public static Animal[] buyAnimal() {
  		Animal[] animas = new Animal[5];
  		for(int i=0;i<5;i++) {
  			System.out.println("请选择1>猫2>狗");
  			int type = new Scanner(System.in).nextInt();
  			switch (type) {
  				case 1://购买猫
  					Cat cat = new Cat();
  					System.out.println("你买了一只猫");
  					animas[i] = cat;
  					break;
  				case 2://购买狗
  					Dog dog = new Dog();
  					System.out.println("你买了一只狗");
  					animas[i] = dog;
  					break;
  				default://输入错误
  					System.out.println("输入错误,请重新输入");
  					i--;
  					break;
  			}
  		}
  		return animas;
      }
      
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

God Zhang

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

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

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

打赏作者

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

抵扣说明:

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

余额充值