练习题的代码
创建两个手机对象,比较他们的尺寸大小cpu和内存大小,球程序代码???
创建两个手机对象,比较他们的尺寸大小cpu和内存大小,球程序代码???
2016-11-02
public class Telephone { float Screen; float Cpu; float Mem; public Telephone(float Screen,float Cpu,float Mem){ this.Screen = Screen; this.Cpu = Cpu; this.Mem = Mem; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Telephone other = (Telephone) obj; if (Float.floatToIntBits(Cpu) != Float.floatToIntBits(other.Cpu)) return false; if (Float.floatToIntBits(Mem) != Float.floatToIntBits(other.Mem)) return false; if (Float.floatToIntBits(Screen) != Float.floatToIntBits(other.Screen)) return false; return true; } public static void main(String[] args){ Telephone t1 = new Telephone(3.5f,1.0f,1.5f); Telephone t2 = new Telephone(3.5f,1.0f,1.5f); if(t1.equals(t2)){ System.out.println("两个手机完全相同"); } else{ System.out.println("两个手机不完全相同"); } } }
举报