java_抽象类举例(以数码产品的销售信息为例)

/*
有三个类Product、Pad和Mobile,
其中Product是抽象类,成员变量有商品名name, 日期data,销售价sale和标价price,
成员方法有sell()(抽象方法)打印商品销售时间,
output()(抽象方法)打印商品信息,
如:huaiwei pad,6999元
销售的日期和时间:2019-09-14 22:03:41.054 sales:7980¥。

Pad和Mobile继承Product,并实现sell()和output()方法。输出格式如下:
生成5件商品放入数组Prodcts中,如:

Mobile mobile1 = new Mobile("2019-09-14 22:03:41.054", "106800", "iphone 11 pro max", 107780);
。。。
Product[] products = {mobile1, mobile2, pad1, pad2, pad3};


 */
abstract class Product {
    String name;
    String date;
    int sale;//销售价(可能打过折)
    int price;//标价(为未打过折)

    abstract void sell();

    abstract void output();
}

class Mobile extends Product {
    //先从Products类中继承过所有成员;
    void output() {
        System.out.print(name + "," + price + "元 ");
    }

    void sell()//打印商品销售时间,
    {
        System.out.println("销售的日期和时间:" + date+" " + "sales:" + sale + "¥");
    }

    //constructor_of_Mobile()
    Mobile(String date, int price, String name, int sale) {
        this.date = date;
        this.price = price;
        this.name = name;
        this.sale = sale;
    }
}

class Pad extends Product {
    //先从Products类中继承过所有成员;
    void output() {
        System.out.print(name + "," + price + "元 ");
    }

    void sell()//打印商品销售时间,
    {
        System.out.println("销售的日期和时间:" +date+" " + "sales:" + sale + "¥");
    }

    Pad(String date, int price, String name, int sale) {
        this.date = date;
        this.price = price;
        this.name = name;
        this.sale = sale;
    }

}

public class Products {

    public static void main(String [] args) {
        Mobile mobile1 = new Mobile("2019-09-14 22:03:41.054", 107780, "iphone 11 pro max",106800 );
        Mobile mobile2 = new Mobile("2019-09-14 22:03:41.054", 5450, "huaiwei p30", 6000);
        Pad pad1 = new Pad("2019-09-14 22:03:41.054", 6999, "huaiwei pad", 7980);
        Pad pad2 = new Pad("2019-09-14 22:03:41.054", 1380, "xiaomi pad", 1400);
        Pad pad3 = new Pad("2019-09-14 22:03:41.054", 9500, "iphone pro", 9500);
        Product[] products_ = {mobile1, mobile2, pad1, pad2, pad3};

        for (int i = 0; i < 5; i++) {
            products_[i].output();
            products_[i].sell();

        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值