day15 多态练习

现有一台自动贩卖机,贩卖机中有如下商品,所有商品都有商品名称,价格,自动贩卖机有出售商品的方法,用户可以输入想要购买的商品的编号,根据商品编号售卖不同的商品

奶茶:

红牛

冰红茶

package oracleday15;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.Random;
import java.util.Scanner;

/**
 *
 */
public class Traffic {
    public static void main(String[] args) {
        VendMechine vmechine = new VendMechine();//创建贩卖机
        vmechine.foundgoods();//生成商品
        Colo colo = new Colo();
        RedBull redBull = new RedBull();
        IceRedTea iceRedTea = new IceRedTea();

        Scanner scan = new Scanner(System.in);
        System.out.println("请输入想要的商品编号");
        System.out.println("1.colo  2.红牛  3.茶  4.退出");

        OUT:
      while (true){
          int systemIn = scan.nextInt();
        if (systemIn == colo.goodsnum){

            for (int i = 0; i < vmechine.commditys.length; i++) {
                if  (vmechine.commditys[i] instanceof Colo){
                    System.out.println(vmechine.commditys[i].name+"价格是"+vmechine.commditys[i].price);
                    break;}
            }

        }else if (systemIn == redBull.goodsnum) {
            for (int i = 0; i < vmechine.commditys.length; i++) {
                if (vmechine.commditys[i] instanceof RedBull){
                    System.out.println(vmechine.commditys[i].name+"价格是"+vmechine.commditys[i].price);
                    break;}
            }
        }else if (systemIn == iceRedTea.goodsnum){
                for (int i = 0; i < vmechine.commditys.length; i++) {
                    if  (vmechine.commditys[i] instanceof IceRedTea){
                        System.out.println(vmechine.commditys[i].name+"价格是"+vmechine.commditys[i].price);
                        break;}
                }
        }else if (systemIn == 4){
            System.out.println("成功退出");
            break OUT;
        }
        else {
            System.out.println("fault");
        }

        }


      }
    }

class VendMechine {
    public VendMechine() {
    }

    //    public VendMechine(Colo colo){
//        System.out.println();
//    }
    //创建多个商品对象
    Commditys commditys[] = new Commditys[15];

    Random random = new Random();



    public Commditys[] foundgoods() {
        for (int i = 0; i < commditys.length; i++) {
            int a = random.nextInt(15);
            if (a < 5) {
                commditys[i] = new Colo();
            }else if(a < 10){
                commditys[i] = new RedBull();
            }else if (a < 15){
                commditys[i] = new IceRedTea();
            }

        }
        //生成随机的所有商品对象
    return commditys;
    }//贩卖机的生成商品方法




}

class Commditys {
  protected   String name;
  protected   double price;
  protected   int goodsnum;

    public Commditys() {
    }

    public Commditys(String name, double price, int goodsnum) {
        this.name = name;
        this.price = price;
        this.goodsnum = goodsnum;
    }
}

class Colo extends Commditys {

    public Colo() {
        name = "可乐";
        price = 3;
        goodsnum = 1;
    }
}

class RedBull extends Commditys {
    public RedBull() {
        name = "红牛";
        price = 6;
        goodsnum = 2;
    }
}

class IceRedTea extends Commditys {
    public IceRedTea() {
        name = "冰红茶";
        price = 3;
        goodsnum = 3;
    }
}


定时器定时输出Pet的子类对象

package com.poloymorphic.extendsx;



import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 多态
 * 向上造型
 */
public class ExtendsDemo {

    private Pet pets[] = {};     //自定义数组长度

//        Pet pets[] = new Pet[20];
    public Pet[] creatPets(){
        pets = Arrays.copyOf(pets,pets.length+1);
        //每次调用creatPet数组  数组长度加1   创造一个新的pet对象
        Random random = new Random();
        for (int i = 0; i < pets.length; i++) {
            int n = random.nextInt(20);
            if (n <= 4){
                pets[i] = new Dog();
            }else if( n<= 12){
                pets[i] = new Rabbits();
            }else if (n <= 19){
                pets[i] = new Cat();
            }
        }
        //遍历for循环给声明的pet类型创建一个子类对象

        return pets;
        //creatPets给定一个返回值
    }

    /**
     *   遍历循环输出数组
     */
        public void printPet(Pet petss[]){
            for (int i = 0; i < petss.length; i++) {
                System.out.println(petss);
            }
    }




    public static void main(String[] args) {
            ExtendsDemo exdemo = new ExtendsDemo();


        Timer timer = new Timer();//创建定时器对象

        timer.schedule(new TimerTask(){
            @Override
            public void run(){
                exdemo.creatPets();
                exdemo.printPet(exdemo.pets);
            }
        },10,1000);
    }


}























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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值