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();
}
}
}
posted @   xuchaoxin1375  阅读(7)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-04-03 二叉查找树_删除节点
2021-04-03 datastructre_查找_顺序查找/折半查找(二分查找)/分块查找性能_二叉查找树_查找前驱节点
点击右上角即可分享
微信分享提示