62,614
社区成员
发帖
与我相关
我的任务
分享
package com.xuz.csdn.july05;
public class Product {
private int id;
private String name;
public Product(int id,String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Product p){
if (this.id == p.getId()) {
return true;
} else {
return false;
}
}
}
package com.xuz.csdn.july05;
import java.util.ArrayList;
import java.util.List;
public class ProductApp {
public static void main(String[] args) {
Product p1 = new Product(1,"a");
Product p2 = new Product(2,"b");
Product p3 = new Product(3,"c");
Product p2_ = new Product(2,"b");
List<Product> list = new ArrayList<Product>();
list.add(p1);
list.add(p2);
list.add(p3);
for (int i = 0; i < list.size(); i++) {
System.out.println(p2_.equals(list.get(i)));
}
System.out.println(list.indexOf(p2_));
}
}
public boolean equals(Object o){
// if (this.id == p.getId()) {
// return true;
// } else {
// return false;
// }
Product p = (Product)o;
if (this.id == p.getId()) {
return true;
} else {
return false;
}
}