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();
}
}