子类根据需求对从父类继承的方法进行重新编写
重写时,可以用super.方法的方式来保留父类的方法
构造方法不能被重写
就像之前写的代码:
- public class Pet {
- private String name;
- private int health;
- private int love;
-
- // 添加构造方法
- public Pet() {
- }
-
- public Pet(String name, int health, int love) {
- this.name = name;
- this.health = health;
- this.love = love;
- }
-
- 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;
- }
-
- public void print() {
- System.out.println(this.getName() + " " + this.getLove() + " "
- + this.getHealth());
- }
- }
- //Dog
- public class Dog extends Pet {
- //strain属性是Dog类里独有的属性,其他类没有,所以要在Dog类里定义
-
- private String strain;
-
- public Dog(){
- }
-
- public Dog(String name, int health, int love, String strain) {
- super(name, health, love);
- this.strain = strain;
- }
-
- public String getStrain() {
- return strain;
- }
-
- public void setStrain(String strain) {
- this.strain = strain;
- }
-
- }
- //测试类
- Dog dog1=new Dog("大黄", 66, 66, "柯基");
- //在dog类中没有setName方法,但是Dog类对象dog1是可以调用,因为Dog类继承了pet类
- dog1.setName("小黄");
- dog1.print();

这里只能输出父类Pet的print()的方法, 并不能把Dog类中的strain属性输出,所以需要使用到方法重写
父类的方法无法满足子类的需求,就需要使用到方法重写
子类根据需求对从父类继承的方法进行重新编写
重写时,可以用super.方法的方式来保留父类的方法
构造方法不能被重写
- //父类Pet
- public class Pet {
- private String name;
- private int health;
- private int love;
-
- // 添加构造方法
- public Pet() {
- //调用Pet类的父类Object类里的无参构造方法
- super();
- }
-
- public Pet(String name, int health, int love) {
- this.name = name;
- this.health = health;
- this.love = love;
- }
-
- 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;
- }
-
- public void print() {
- System.out.print("昵称:"+this.getName() + " 亲密度:" + this.getLove() + " 健康值"
- + this.getHealth());
- }
- }
- //Dog类
- public class Dog extends Pet {
- //strain属性是Dog类里独有的属性,其他类没有,所以要在Dog类里定义
-
- private String strain;
-
- public Dog(){
- }
-
- public Dog(String name, int health, int love, String strain) {
- super(name, health, love);
- this.strain = strain;
- }
-
- public String getStrain() {
- return strain;
- }
-
- public void setStrain(String strain) {
- this.strain = strain;
- }
- @Override
- public void print() {
- super.print();
- System.out.println(" 品种:"+this.getStrain());
- }
- }
- //penguin类
- public class Penguin extends Pet {
- //定义Penguin类里独有的属性
-
- private String sex;
-
- public Penguin(){
- super();//这个地方表示调用父类的无参构造方法
- }
-
- public Penguin(String name, int health, int love, String sex) {
- super(name, health, love);
- this.sex = sex;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
- public void print() {
- super.print();
- System.out.println(" 性别:"+this.getSex());
- }
- }
- //测试类
- Dog dog = new Dog("旺旺", 5, 8, "恶霸犬");
- dog.print();
-
- Penguin penguin = new Penguin("QQ", 0, 1, "雄");
- penguin.print();
输出结果

需要是该类的子类
方法名相同
参数列表相同
返回值类型相同或者是其子类
访问权限不能严于父类
| 位置 | 方法名 | 参数表 | 返回值 | 访问权限修饰符 | |
| 方法重写 | 子类 | 相同 | 相同 | 相同或者是子类 | 不能比父类更严格 |
| 方法重载 | 同类 | 相同 | 不同 | 无关 | 无关 |
Object类是所有类的父类
| toString() | 返回当前对象本身的有关信息 |
| equals() | 比较两个对象是否是同一个对象,是则返回true |
| hashCode() | 返回改对象的哈希代码值 |
| getClass() | 获取当前对象所属的类信息,返回Class对象 |
若不重写toString,输出的则是:包名.类名.@.hash码值,是地址;若重写之后则输出对象本身的值。
- public static void main(String[] args) {
- //创建学生类对象
- Student stu = new Student("马力", 66);
- System.out.println(stu);//cn.bdqn.demo02.Student@40671416
- String str=stu.toString();//按住ctrl点击toString可以查看
- System.out.println(str);//cn.bdqn.demo02.Student@40671416
- System.out.println(stu.getClass().getName()+'@'+Integer.toHexString(stu.hashCode()));//cn.bdqn.demo02.Student@40671416
- }

可以按住ctrl,点击toString

- @Override
- public String toString() {
- return "姓名:"+this.getName()+",年龄:"+this.getAge();
- }
重写之后输出的代码是

还可以右击鼠标,——>Source——>Generate toString


这里可以选择在toString中重写哪些方法
为什么要重写equals()方法:
object类中的equals()方法比较是的两个对象的地址值,不能满足子类的需求
“==”表示比较两个基本数据类型,“equals”表示比较两个对象。若不引入object类的equals方法,则输出为对象的地址,而引入以后则输出对象的值。
- public static void main(String[] args) {
- Dog dog3 = new Dog("马力", 78, 56);
- System.out.println(dog3);
- System.out.println(dog3.toString());
-
- Dog dog2 = new Dog("马力", 78, 56);
- /*
- * object类中的equals()方法比较是的两个对象的地址值,不能满足子类的需求:
- * string类里只要两个字符串对象的内容相同,就认为两个对象是同一个对象,所以String类重写了0bject类里的equals()方法
- * Dog类里只要两个Dog对象的昵称、品种和健康值一致,就认为两个Dog对象是同一个对象,所以也需要在Dog类里重写Object里的equals()方法
- */
-
- boolean result = dog2.equals(dog3);
- System.out.println(result);//false
-
- System.out.println(dog3);
- System.out.println(dog2);
-
- String str1 = new String("dsad");
- String str2 = new String("dsad");
- System.out.println(str1.equals(str2));
- }