文章目录
  1. 1. 继承应用

继承应用

实现一个化妆品商城中的化妆品管理

  1. 定义一个化妆品类(Cosmetic)
  2. 定义一个化妆品管理类(CosmeticManager)
    • 实现进货功能
    • 可以输出所有化妆品信息功能
  3. 使用继承实现一个可按单价排序输出所有化妆品的功能
  4. 使用继承实现一个只输出进口化妆品的功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//化妆品管理应用(继承)
import java.util.Arrays;
public class Test2{
public static void main(String[] args){
/*
CosmeticManager cm = new CosmeticManager();
cm.add("new Cosmetic("香奈儿",800.0f));
cm.add("new Cosmetic("大宝",10.0f));
cm.add("new Cosmetic("郁美净",8.0f));
cm.add("new Cosmetic("玉兰油",100.0f));
cm.printInfo();
*/
/*
CosmeticManager cm = new CosmeticManager();
cm.add("new Cosmetic("香奈儿",800.0f));
cm.add("new Cosmetic("大宝",10.0f));
cm.add("new Cosmetic("郁美净",8.0f));
cm.add("new Cosmetic("玉兰油",100.0f));
cm.printInfo();
*/
ImportCosmeticManager cm = new CosmeticManager();
cm.add("new Cosmetic("香奈儿",800.0f,"进口"));
cm.add("new Cosmetic("大宝",10.0f,"国产"));
cm.add("new Cosmetic("郁美净",8.0f,"国产"));
cm.add("new Cosmetic("玉兰油",100.0f,"进口"));
cm.printInfo();
}
}
//只输出进口化妆品
class ImportCosmeticManager extends CosmeticManager{
public void printInfo(){
String type = null;
for(int i=0;i<super.count;i++){
super.cosmetics[i].getType();
if("进口".equals(type)){
System.out.println(cosmetics[i].getInfo());
}
}
}
}
//可按单价排序
class SortCosmeticManager extends CosmeticManager{
//重写了父类的打印方法,实现了排序功能
public void println(){
Cosmetic[] cs = new Cosmetic[super.count];
for(int i=0;i<cs.length;i++){
cs[i] = super.cosmetics[i];
}
Cosmetic temp=null;
//冒泡排序
for(int i =0;i<cs.length-1;i++){
for(int j=0;j<cs.length-1-i;j++){
if(cs[j].getPrice()>cs[j+1].getPrice()){
temp = cs[j];
cs[j] = cs[j+1];
cs[j+1] = temp;
}
}
}
for(int i=0;i<count;i++){
System.out.println()cs[i].getInfo();
}
}
}
//化妆品管理类
class CosmeticManager{
private Cosmetic[] cosmetics = new Cosmetic[3];
prot int count = 0;
//化妆品进货
public void add(Cosmetic c){
if(count>=cosmetics.length){
int newLen = cosmetics.length*3/2+1;
cosmetics = Arrays.copyOf(cosmetics,newLen);
}
cosmetics[count]=c;
count++;
}
//输出所有化妆品
public void printInfo(){
for(int i=0;i<cosmetics;i++){
System.out.println(cosmetics[i].getInfo());
}
}
}
//化妆品类
class Cosmetic{
private String name;
private float price;
private String type; //国产或进口
public Cosmetic(){};
publi Cosmetic(String name,float price,String type){
this.name = name;
this.price = price;
this.type = type;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setPrice(){
this.price;
}
public void setName(String type){
this.type= type;
}
public String getType(){
return Type;
}
public float getPrice(){
return price;
}
public void getInfo(){
return "化妆品名称:"+name+"单价为:"+price+"类型为"+type;
}
}
文章目录
  1. 1. 继承应用