标签:手机app idt 子类 return 链接 main name 图片 gets
java文件分享地址为:
链接:https://pan.baidu.com/s/1dRL2d0p_wFmQoVhryXzukQ
提取码:r8ky
复制这段内容后打开百度网盘手机App,操作更方便哦
EQUALS重写
编写代码如下:
创建Pet的父类
package com.chongxie;
public class Pet {
private String name;
private int age;
private String sex;
public Pet() {
}
public Pet(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public boolean equals(Object obj){
if(obj instanceof Pet){
Pet pet = (Pet)obj;
if(pet.getName().equals(this.getName())&&pet.getAge()==this.getAge()&&pet.getSex().equals(this.getSex())){
return true;
}
}
return false;
}
}
创建Dog子类:
package com.chongxie;
public class Dog extends Pet{
private String strain;
public Dog(String name, int age, String sex, String strain) {
super(name, age, sex);
this.strain = strain;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
}
测试类:
package com.chongxie; public class Test { public static void main(String[] args) { Pet dog1 = new Dog("狗狗",10,"公","拉布拉多犬"); Pet dog2 = new Dog("狗狗",10,"公","拉布拉多犬"); System.out.println(dog1==dog2); System.out.println(dog1.equals(dog2)); } }
输出结果为:
标签:手机app idt 子类 return 链接 main name 图片 gets
原文地址:https://www.cnblogs.com/bozhu-liu/p/10298263.html