没问题,可以参考啊

来源:9-9 Java 中的 Object 类 Ⅱ

weibo_yx阳光宅男xy_0

2017-10-18 11:31

package com.super2;

public class IPhone {
    public String name;
    public double cpu;
    public double screen;
    public double member;
    
    public void camera() {
        System.out.println("拍照美美哒");
    }
}



package com.super2;

public class Oppo extends IPhone{
    
//    public String name = "Oppo";
    public double cpu;
    public double screen;
    public double member;
    
    public Oppo() {
        // TODO Auto-generated constructor stub
        System.out.println("Oppo类执行了!!!");
        
    }

    public void Advertisment() {
        System.out.println("前后两千万柔光双摄,照亮你的美");
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Oppo other = (Oppo) obj;
        if (Double.doubleToLongBits(cpu) != Double.doubleToLongBits(other.cpu))
            return false;
        if (Double.doubleToLongBits(member) != Double.doubleToLongBits(other.member))
            return false;
        if (Double.doubleToLongBits(screen) != Double.doubleToLongBits(other.screen))
            return false;
        return true;
    }
}


package com.super2;

public class Initial {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Oppo oppoR9  = new Oppo();
        Oppo oppoR11 = new Oppo();
        oppoR9.cpu = 2.0;
        oppoR11.cpu= 2.0;
        oppoR9.screen = 5.5;
        oppoR11.screen= 5.5;
        oppoR9.member = 1024;
        oppoR11.member = 1024;
        oppoR9.Advertisment();
//        System.out.println(oppoR9);
//        System.out.println(oppoR11);
        if (oppoR9.equals(oppoR11)) {
            System.out.println("两个对象一样");
        }else {
            System.out.println("俩对象不一样");
        }
    }
}


写回答 关注

2回答

  • qq_Ambition_21
    2017-10-19 10:10:39

    自己写了个equals比较

    weibo_...

    对,就是这样

    2017-10-26 16:32:17

    共 1 条回复 >

  • 学习从零开始
    2017-10-18 14:30:26

    这有什么问题吗?

    weibo_...

    没有啊。很好啊

    2017-10-26 16:31:27

    共 3 条回复 >

Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530559 学习 · 6091 问题

查看课程

相似问题