方法的重写或方法的覆盖( overriding )
1)子类根据需求对从父类继承的方法进行重新编写
2)重写时,可以用super.方法的方式来保留父类的方法
3)构造方法不能被重写
1)在子类里面对父类进行重写
2)方法名相同
3)参数列表相同
4)重写后的方法返回值类型与父类相同或者是其子类相同
5)访问权限不能严于父类 (父类的访问权限不能小于子类的访问权限)
-----------------------------(重点掌握1~5;了解注意6~9)-------------------------------
6)父类的静态方法不能被子类覆盖为非静态方法,父类的非静态方
法不能被子类覆盖为静态方法
7)子类可以定义与父类同名的静态方法,以便在子类中隐藏父类的
静态方法(注:静态方法中无法使用super)
8)父类的私有方法不能被子类覆盖
9)不能抛出比父类方法更多的异常
例如:
public class Pet extends Object {//Object是Pet的父类
……
}
toString() //返回当前对象本身的有关信息,按字符串对象返回
equals() //比较两个对象是否是同一个对象,是则返回true
比较两个对象是否是同一个对象,是则返回true
操作符==
●简单数据类型,直接比较值。如1==2
●弓|用类型,比较两者是否为同一对象
(1) Object类的equals()方法 与==没区别
(2)当有特殊需求,如认为属性相同即为同一对象时,需要重写equals()
(3) Java.lang.String重写 了equals()方法,把equals()方 法的判断变为了判断其值
toString() //返回当前对象本身的有关信息,按字符串对象返回
类package AnimalDemo03;
public class Animal {
//定义属性,使用private修饰符进行封装
private String name;//名字
private int health;//健康值
private int love;//亲密度
//无参构造方法
public Animal() {
super();//调用Animal类的父类Object类里的无参构造方法
}
//有参构造方法
public Animal(String name, int health, int love) {
super();
this.name = name;
this.health = health;
this.love = love;
}
//get、set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
//定义方法输出对象信息
@Override
public String toString() {//toString()重写方法
return "宠物昵称:" + name + ", 健康值:" + health + ", 亲密度:" + love;
}
}
快捷键快速生成toString重写方法:单击右键>Source>倒数第四个(末尾单词为 toString() 的)
对象
package AnimalDemo03;
import AnimalDemo01.Cat;
import AnimalDemo01.Dog;
import AnimalDemo01.Penguin;
public class Test {
public static void main(String[] args) {
//创建子类对象
//创建Animal类对象(toString()重写方法)
Animal dog2=new Animal("小黑",88,98);
System.out.println(dog2);
System.out.println(dog2.toString());
}
}
代码2:
equals() //比较两个对象是否是同一个对象,是则返回true
类
package AnimalDemo03;
public class Animal {
//定义属性,使用private修饰符进行封装
private String name;//名字
private int health;//健康值
private int love;//亲密度
//无参构造方法
public Animal() {
super();//调用Animal类的父类Object类里的无参构造方法
}
//有参构造方法
public Animal(String name, int health, int love) {
super();
this.name = name;
this.health = health;
this.love = love;
}
//get、set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
//定义方法输出对象信息
//比较你传递过来的dog对象与调用这个方法的对象里的昵称、健康值是否一致
public boolean equals(Animal dog){// equals()重写方法
if(dog.getName().equals(this.getName()) && dog.getHealth()==this.getHealth()){
return true;
}else{
return false;
}
}
}
对象
package AnimalDemo03;
import AnimalDemo01.Cat;
import AnimalDemo01.Dog;
import AnimalDemo01.Penguin;
public class Test {
public static void main(String[] args) {
//创建子类对象
//创建Animal类对象(toString()重写方法)
Animal dog2=new Animal("小黑",88,98);
System.out.println(dog2);
System.out.println(dog2.toString());
//创建Animal类对象(equals()重写方法)
Animal dog3=new Animal("小黑",88,98);//比较dog2和dog3
boolean dogs=dog3.equals(dog2);
System.out.println(dogs);//结果:true
System.out.println(dog3);
System.out.println(dog2);
String str1=new String("dw");
String str2=new String(" dw");
System.out.println(str1.equals(str2));
}
}



