java11 OOP继承实例 2022-02-07

//继承实例
import java.util.Arrays;
public class Test1 {
    public static void main(String[] args) {
        CosmeticManager cm1 = new CosmeticManager();
        cm1.add(new Cosmetic("shizendo","import",1000));
        cm1.add(new Cosmetic("香奈儿","import",900));
        cm1.add(new Cosmetic("sk2","import",1500));
        cm1.add(new Cosmetic("dabao","domestic",100));
        cm1.printAll();
        System.out.println("====================");
        SortCosmeticManager cm = new SortCosmeticManager();
        cm.add(new Cosmetic("shizendo","import",1000));
        cm.add(new Cosmetic("香奈儿","import",900));
        cm.add(new Cosmetic("sk2","import",1500));
        cm.add(new Cosmetic("dabao","domestic",100));
        cm.printAll();
        System.out.println("====================");
        ImportCosmeticManager cm2 = new ImportCosmeticManager();
        cm2.add(new Cosmetic("shizendo","import",1000));
        cm2.add(new Cosmetic("香奈儿","import",900));
        cm2.add(new Cosmetic("sk2","import",1500));
        cm2.add(new Cosmetic("dabao","domestic",100));
        cm2.printAll();
    }
}
//化妆品类
class Cosmetic{
    private String name;
    private String type;
    private int price;
    public Cosmetic() {}
    public Cosmetic(String name, String type, int price) {
        this.name = name;
        this.type = type;
        this.price = price;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getInfo(){
        return "name="+name+"type="+type+"price="+price;
    }
}
//化妆品管理类
class CosmeticManager{
    protected Cosmetic[] cs=new Cosmetic[4];//子类需要所以用protected,如果还用private需要Getter Setter方法,代码量多
    protected int count=0;//子类需要所以用protected,如果还用private需要Getter Setter方法,代码量多
    //进货功能
    public void add(Cosmetic c){
        int size=cs.length;
        if(count>=size){
            int newLen=size*2;
            cs=Arrays.copyOf(cs,newLen);
        }
        cs[count]=c;
        count++;
    }
    //输出所有产品
    public void printAll(){
        for (int i = 0; i < count; i++) {
            System.out.println(cs[i].getInfo());
        }
    }
}
//可按单价排序的化妆品管理类
class SortCosmeticManager extends CosmeticManager{
    //排序输出所有产品(重写父类方法)
    public void printAll(){
        Cosmetic[] temp=Arrays.copyOf(cs,count);
        //冒泡排序
        Cosmetic c=null;
        for (int i = 0; i < temp.length; i++) {
            for (int j = 0; j < temp.length-i-1; j++) {
                if(temp[j].getPrice()>temp[j+1].getPrice()){
                    c=temp[j];
                    temp[j]=temp[j+1];
                    temp[j+1]=c;
                }
            }
        }
        //foreach循环输出数组
        for(Cosmetic cosmetic:temp){
            System.out.println(cosmetic.getInfo());
        }
    }
}
//筛选出进口化妆品
class ImportCosmeticManager extends CosmeticManager{
    //输出所有产品
    public void printAll(){
        //比较两个字符串的值不能使用==
        for (int i = 0; i < count; i++) {
            if("import".equals(cs[i].getType())){
                System.out.println(cs[i].getInfo());
            }
        }
    }
}

你可能感兴趣的:(java11 OOP继承实例 2022-02-07)