package com.data.entity;
public class dog extends Anlimal {
public String name;
public int age;
public double score;
public mm mao;
public dog(String name,int age,double score){
this.name=name;
this.age=age;
this.score=score;
}
@Override
public String toString() {
return "dog{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
", mao=" + mao +
'}';
}
@Override
public int hashCode() { //重构 hash
return name.hashCode()+age+(int)score;
}
@Override
public boolean equals(Object o) { //重构内容比较
if (this == o) { //自反性
return true;//自己和自己比,一定相同
}
if (o == null || this.getClass() != o.getClass()) { //非空判断 和 类型比较
return false;//与null值比或者实际对象类型不一致,一定不同
}
//比较属性值
dog d=(dog) o;
return this.age==d.age && this.score==d.score && this.name.equals(d.name) && this.mao.equals(d.mao);
}
}
import com.data.entity.Anlimal;
import com.data.entity.commodity;
import com.data.entity.dog;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Anlimal a1=new dog("111",22,33.3);
// Anlimal a2=new dog("22",22,33.3);
// System.out.println(a1.getClass());
// System.out.println(a2.getClass());
//
// System.out.println(a1.hashCode());
commodity []comm=new commodity[]{
new commodity("11111",333,5),
new commodity("22222",222,5),
new commodity("33333",111,5),
};
for (int i = 0; i < comm.length; i++) {
System.out.println(comm[i]);
}
Scanner in = new Scanner(System.in);//定义scanner,等待输入
System.out.println("\n请输入名称:");
String name = in.nextLine();
System.out.println("\n请输入价格:");
double price = in.nextDouble();
System.out.println("\n请输入数量:");
int reg = in.nextInt();
commodity nx=new commodity(name,price,reg);
for (int i = 0; i < comm.length; i++) {
if(nx.equals(comm[i])){
System.out.println("存在");
}
}
}
}