构造方法

public class Phone {
    public double width;
    public double high;
    public int weight;
    public String color;

    //构造方法,用于在内存中创建对象
    public Phone() {
        System.out.println("我被构造了");
    }

    /*//构造方法
    public Phone(double kuan,double gao,int zhong,String yanse){
        width=kuan;
        high=gao;
        weight=zhong;
        color=yanse;
    }*/
    public Phone(double kuan, double gao, int zhong, String yanse) {
        this.width = width;
        this.high = high;
        this.weight = weight;
        this.color = color;
    }

    public Phone(double kuan, double gao, int zhong) {
        width = kuan;
        high = gao;
        weight = zhong;
    }

    public void startUp() {
        System.out.println("手机在开机");
    }

    public void lockScreen() {
        System.out.println("手机在锁屏");
    }

    //
    public String toString() {
        return "{" + this.width + this.high + this.weight + this.color + "}";
    }

    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (object instanceof Phone) {
            Phone temp = (Phone) object;
            if (temp.width == this.width && temp.high == this.high && temp.weight == this.weight && temp.color.equals(this.color)) {
                return true;
            }
        }else {
            return false;
        }
        return false;
    }
}

public class Demo {
    public static void main(String[] args) {

        Phone iphoneSE3=new Phone();
        iphoneSE3.width=25;
        iphoneSE3.high=55;
        iphoneSE3.weight=155;
        iphoneSE3.color="玫瑰金";

        Phone onePlus=new Phone(26,56,156,"琥珀蓝");

        System.out.println(iphoneSE3);//edu.xcdq.Phone@1b6d3586
        System.out.println(onePlus);//edu.xcdq.Phone@4554617c

        Phone xiaomi12=new Phone();
        Phone xiaomi13=new Phone();
        System.out.println(xiaomi12==xiaomi13);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。