java Object重写

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("存在");
            }

        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值