鱼C论坛

 找回密码
 立即注册
查看: 2960|回复: 0

[学习笔记] JavaDemo_0909_2334

[复制链接]
发表于 2018-9-9 23:36:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
//equals()方法覆写
class Person
{
        private String name;
        private int age;
        public Person(String name,int age)
        {
                this.name=name;
                this.age=age;
        }
        public void setName(String name)
        {
                this.name=name;
        }
        public void setAge(int age)
        {
                this.age=age;
        }
        public String getName()
        {
                return this.name;
        }
        public int getAge()
        {
                return this.age;
        }
        public String toString()
        {
                return "姓名-->"+this.name+"年龄-->"+this.age;
        }
        public boolean equals(Object obj)
        {
                if(!(obj instanceof Person))
                {
                        return false;
                }
                if(obj==null)
                {
                        return false;
                }
                if(this==obj)
                {
                        return true;
                }
                Person per=(Person) obj;
                return this.name.equals(per.name) && this.age==per.age;                // 在equals()方法中再调用equals方法?
        }
}
public class JavaDemo_0909_2334
{
        public static void main(String[] args)
        {
                Person perA=new Person("张三",22);
                Person perB=new Person("张三",22);
                System.out.println(perA.equals(perB));
        }
}


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 05:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表