对象数组增删改查和继承

Clothes.java

/**
 * 定义一个衣服类(名字,类型(男士或女士),价格)
 * 重写equal、toString等方法
 *
 */

public class Clothes {
    private String name;
    private String sex;
    private double price;

    public Clothes() {
        super();
    }

    public Clothes(String name, String sex, double price) {
        super();
        this.name = name;
        this.sex = sex;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        long temp;
        temp = Double.doubleToLongBits(price);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        result = prime * result + ((sex == null) ? 0 : sex.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Clothes other = (Clothes) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
            return false;
        if (sex == null) {
            if (other.sex != null)
                return false;
        } else if (!sex.equals(other.sex))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Clothes [name=" + name + ", sex=" + sex + ", price=" + price + "]";
    }

}

ClothesManage.java

/**
 * 对象数组的增删改查和继承
 */

import java.util.Arrays;

public class ClothesManage {
    protected Clothes[] clothes = new Clothes[3];
    protected int count = 0;

    //增加一个对象
    public void add(Clothes c) {
        if (count >= clothes.length) {
            int newLen = (clothes.length * 3) / 2 + 1; //重新赋予数组长度
            clothes = Arrays.copyOf(clothes, newLen);
        }
        clothes[count] = c;
        count++;
    }

    //查找对象是否在数组中
    public int find(Clothes c) {
        for (int i = 0; i < count; i++) {
            if (clothes[i].equals(c)) {
                return i;
            }
        }
        return -1;
    }

    //删除数组中的对象
    public void delete(Clothes c) {
        for (int i = 0; i < count; i++) {
            if (clothes[i].equals(c)) {
                for (int j = i; j < count - 1; j++) {
                    clothes[j] = clothes[j + 1];
                }
                clothes[count - 1] = null;
                count--;
                break;
            }
        }
    }

    //修改数组中的对象
    public void update(Clothes c1, Clothes c2) {
        int i = find(c1);
        clothes[i] = c2;
    }

    public void list() {
        for (int i = 0; i < count; i++) {
            System.out.println(clothes[i].toString());
        }
    }
}

//继承并重写list()方法,实现冒泡排序
class SortClothesManage extends ClothesManage {
    public void list() {
        Clothes temp = null;
        for (int i = 0; i < clothes.length - 1; i++) {
            for (int j = 0; j < clothes.length - 1 - i; j++) {
                if (clothes[j].getPrice() < clothes[j + 1].getPrice()) {
                    temp = clothes[j];
                    clothes[j] = clothes[j + 1];
                    clothes[j + 1] = temp;
                }
            }
        }
        super.list();
    }
}

//继承并重写list()方法,实现只输出女士服装
class SexClothesManage extends ClothesManage{
    public void list(){
        ClothesManage cm=new ClothesManage();
        for(int i=0;i<count;i++){
            if("女士".equals(clothes[i].getSex())){
                cm.add(clothes[i]);
            }
        }
        cm.list();
    }
}

TestClothes


public class TestClothes {
    public static void main(String[] args) {
        Clothes c1=new Clothes("内衣1","男士",121);
        Clothes c2=new Clothes("内衣2","男士",122);
        Clothes c3=new Clothes("内衣3","男士",123);
        Clothes c4=new Clothes("内衣4","女士",124);
        Clothes c5=new Clothes("内衣5","女士",125);
        ClothesManage cm=new ClothesManage();
        cm.add(c1);
        cm.add(c2);
        cm.add(c3);
        cm.add(c4);
        cm.add(c5);
        cm.list();
        System.out.println("---------------实现按价格排序-----------------");
        SortClothesManage sm=new SortClothesManage();
        sm.add(c1);
        sm.add(c2);
        sm.add(c3);
        sm.add(c4);
        sm.add(c5);
        sm.list();
        System.out.println("---------------实现只输出女士服装--------------------");
        SexClothesManage ssm=new SexClothesManage();
        ssm.add(c1);
        ssm.add(c2);
        ssm.add(c3);
        ssm.add(c4);
        ssm.add(c5);
        ssm.list();
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值