对象数组增删改查和继承
来源:互联网 发布:前海开源人工智能 编辑:程序博客网 时间:2024/06/04 19:12
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(); }}
阅读全文
0 0
- 对象数组增删改查和继承
- java 对象数组(增删改查)
- java对象数组的增删改查
- javascript数组增删查改
- javascript数组增删改查
- 数组的增删改查
- 自定义数组的增删改查
- 多维数组的运用 增删改查
- 数组的增删改查操作
- 数据结构JAVA数组的增删改查
- javascript js 操作数组 增删改查
- Js 数组增删改查详解
- java创建数组,实现增删改查
- java实现数组的增删改查
- swit 数组用法,增删改查、合并
- Java数组的增删查改
- java 利用数组实现增删改查
- js中数组的增删改查
- 大数据概念
- Insert sort
- 数据库三范式
- 浅谈spring
- 【c语言】c语言实现顺序表(静态)
- 对象数组增删改查和继承
- SVN分支/合并原理及方式
- Meteor Shower (POJ 3669,广度优先搜索)
- Spring框架整合Java Web Token
- POJ 3233 Matrix Power Series
- [简单逻辑学]逻辑学的基本原理——建立一个论证
- C++日志
- 基于Unity3D的对象池的实现
- MapReduce代码示例