继承和重写
今天学习了继承与继承中的重写方法,并且做了小例子,主要是运用了JAVA的一些基本知识,主要是继承和方法的改写,还用到了之前的排序方法。这个小程序是描述了对于化妆品的进货(添加化妆品的种类或者和数量)以及展示现有的化妆品种类以及价格(价格按照从小到大排序)。
- 首先是建立一个化妆品类和一个化妆品管理类,在管理化妆品类中,如果空间不够就要进行空间的扩展
//化妆品类
class CosmeticType {
private String name;
private float price;
private String type;
public CosmeticType(String name, float price, String type) {
super();
this.name = name;
this.price = price;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//得到化妆品信息
public String getInfo(){
return "The name of Cosmetic is " + name + " price is " + price + " The type is " + type;
}
}
//化妆品管理类
class CosmeticManager{
//protected 才可以继承
protected CosmeticType[] cosmetics = new CosmeticType[4];
protected int count = 0;
//化妆品进货
public void add(CosmeticType cos){
//如果长度不够就扩展空间
if(count > cosmetics.length){
//将空间增加一半
int newLen = cosmetics.length*3/2+1;
//将原来的信息复制到新的空间中
cosmetics = Arrays.copyOf(cosmetics, newLen);
}
//将添加的cos(一个cosmetic对象)赋值给 最后一个位置
cosmetics[count] = cos;
count++;
}
//输出所有化妆品
public void printInfo(){
for(int i = 0; i < count; i++){
System.out.println(cosmetics[i].getInfo());
}
}
}
2 . 测试输出一下所有的化妆品
public class Cosmetic{
public static void main(String[] srgs){
CosmeticManager cm = new CosmeticManager();
cm.add(new CosmeticType("大宝", 8.0f, "China"));
cm.add(new CosmeticType("Olay", 260.0f, "America"));
cm.add(new CosmeticType("郁美净", 12.0f, "China"));
cm.add(new CosmeticType("Lancome", 450.0f, "France"));
cm.printInfo();
}
}
基本的类已经构造完成,也可以输出,接下来进行继承和改写部分,在继承时,要确保父类中的属性和方法是protected或者是public,只有这样才能被子类继承。
1.首先写一个继承化妆品管理类的子类,来实现将化妆品按价格从小到大输出
class SortCosmeticManager extends CosmeticManager{
//重写了父类的打印方法,实现了排序
public void printInfo(){
CosmeticType[] cs = new CosmeticType[super.count];
for(int i = 0; i < cs.length; i++){
cs[i] = super.cosmetics[i];
}
CosmeticType 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());
}
}
}
2.测试按价格输出化妆品
public class Cosmetic{
public static void main(String[] srgs){
SortCosmeticManager cm = new SortCosmeticManager();
cm.add(new CosmeticType("大宝", 8.0f, "China"));
cm.add(new CosmeticType("Olay", 260.0f, "America"));
cm.add(new CosmeticType("郁美净", 12.0f, "China"));
cm.add(new CosmeticType("Lancome", 450.0f, "France"));
cm.printInfo();
}
}
3.然后再写一个继承化妆品管理类的子类,实现只输出国产化妆品
//只输出国产化妆品
class ImportCosmeticManager extends CosmeticManager{
public void printInfo(){
for(int i = 0; i < super.count; i++){
if("China".equals(super.cosmetics[i].getType())){
System.out.println(cosmetics[i].getInfo());
}
}
}
}
4.测试输出国产化妆品
public class Cosmetic{
public static void main(String[] srgs){
ImportCosmeticManager cm = new ImportCosmeticManager();
cm.add(new CosmeticType("大宝", 8.0f, "China"));
cm.add(new CosmeticType("Olay", 260.0f, "America"));
cm.add(new CosmeticType("郁美净", 12.0f, "China"));
cm.add(new CosmeticType("Lancome", 450.0f, "France"));
cm.printInfo();
}
}
程序到此结束,所有的功能都已实现。