Java基础之⽅法重写详解
⼀、java⽅法重写
⽅法的重写是⼦类根据需求对⽗类继承的⽅法进⾏重新的编写,在重写时,可以使⽤super⽅法的⽅式来保留⽗类中的⽅法,注意:构造⽅法不可以被重写。
创建⼀个⼈类,属性包括姓名性别年龄⾏为⽅法是输出信息
⼆、super关键字
⽅法重写时要在⼦类中定义⼀个和⽗类相同名称的⽅法,并采⽤super关键字super.⽅法名();,这样就实现了⽅法的重写
package cn.zhz.Kind.dh;
public class Person1 {
public String sex;
public String name;
public int age;
public void showInfo() {
System.out.println("姓名:" + name + "年龄:" + age + "性别:" + sex);
}
}
创建⼀个学⽣类,属性包括学号学科,⾏为⽅法是输出信息
package cn.zhz.Kind.dh;
public class Student1 extends Person1{
public int stuId;
public String stuSubject;
public void showInfo(){
super.showInfo();
System.out.println("学号:"+ stuId+ "学科:"+ stuSubject);
}
}
创建学⽣类的⼀个实例,进⾏赋值操作,赋值后输出
package cn.zhz.Case.dh;
import cn.zhz.Kind.dh.Student1;
public class Student1Test {
public static void main(String[] args) {
Student1 student1 = new Student1();
student1.stuId = 1;
student1.name = "张三";
student1.age = 18;
student1.sex = "男";
student1.stuSubject = "物理";
student1.showInfo();
}
}
2.1 super关键字的介绍
super可以应⽤在⼦类访问⽗类成员中,⽐如说:
访问⽗类的属性super.print();注意是⾮private的⽅法
访问⽗类的⽅法super.name;
访问⽗类的构造⽅法super();
package cn.zhz.Inherit.dh;
public class Pet {
private String name = "⽆名⽒";
private int health = 100;
private int love = 0;
public int age = 1;
public Pet() {
System.out.println("⽗类⽆参构造⽅法");
}
public Pet(String name) {
this.name = name;
}
public Pet(String name, int health, int love) {
//this可以调⽤本类的构造⽅法,且在第⼀⾏
//this(name);
this.name = name;
this.health = health;
this.love = love;
System.out.println("⽗类带参构造⽅法");
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setHealth(int health) {
if (health < 0 || health > 100) {
System.out.println("宠物的健康值在0-100之间");
this.health = 60;
return;
}
this.health = health;
}
public int getHealth() {
return this.health;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setLove(int love) {
if (love < 0 || love > 100) {
System.out.println("宠物的亲密度只能在0-100之间");
this.love = 60;
return;
}
this.love = love;
}
public int getLove() {
return this.love;
}
/
/输出宠物的信息
public void print() {
System.out.println("宠物的⾃⽩:\n我的名字叫" + this.name + "健康值是" + this.health + "和主⼈的亲密度是:" + this.love);    }
}
package cn.zhz.Inherit.dh;
public class Dog extends Pet {
private String strain = "聪明的拉布拉多⽝";
public Dog() {
System.out.println("⼦类狗狗的⽆参构造⽅法");
}
public Dog(String name, int health, int love, String strain) {
//通过super调⽤⽗类的构造⽅法,必须是第⼀句
//super();
super(name, health, love);
this.strain = strain;
System.out.println("狗狗的带参构造⽅法");
}
public void setStrain(String strain) {
this.strain = strain;
}
public String getStrain() {
return this.strain;
}
public void print() {
//调⽤⽗类的⾮private的print()⽅法
super.print();
System.out.println("我是⼀只" + this.strain);
}
public void m1() {
//super不可以调⽤⽗类的private属性
//        System.out.println(super.name);
System.out.println(super.age);
}
}
在使⽤super关键字来访问⽗类的成员时,使⽤super关键字,super代表的是⽗类的对象,super只可以出现在⼦类的⽅法和构造⽅法中,使⽤super调⽤构造⽅法时,只可以是第⼀句,super不可以访问⽗类的private成员。
2.2 super关键字的使⽤
当⼦类中的⽅法重写了⽗类的⽅法,或者是在⼦类中定义了和⽗类的同名称的成员变量,使⽤super关键字可以使被屏蔽的成员可见。.
package cn.zhz.Inherit.dh;
public class Pet {
private String name = "⽆名⽒";
private int health = 100;
private int love = 0;
public int age = 1;
public Pet() {
System.out.println("⽗类⽆参构造⽅法");
}
public Pet(String name) {
this.name = name;
}
public Pet(String name, int health, int love) {
//this可以调⽤本类的构造⽅法,且在第⼀⾏
//this(name);
this.name = name;
this.health = health;
this.love = love;
System.out.println("⽗类带参构造⽅法");
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setHealth(int health) {
if (health < 0 || health > 100) {
System.out.println("宠物的健康值在0-100之间");
this.health = 60;
return;
}
this.health = health;
}
public int getHealth() {
return this.health;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setLove(int love) {
if (love < 0 || love > 100) {
System.out.println("宠物的亲密度只能在0-100之间");
this.love = 60;
return;
}
this.love = love;
}
public int getLove() {
return this.love;
}
//输出宠物的信息
public void print() {
System.out.println("宠物的⾃⽩:\n我的名字叫" + this.name + "健康值是" + this.health + "和主⼈的亲密度是:" + this.love);
}
}
⼦类和⽗类中同时定义了⼀个age属性,⼦类调⽤这个age属性时先会在本类中,这时可以使⽤super关键字代表⽗类,⽤super.属性的⽅式调⽤⽗类中的age属性
package cn.zhz.Inherit.dh;
public class Dog extends Pet {
private String strain = "聪明的拉布拉多⽝";
private int age = 10;
java重写和重载的区别public Dog() {
System.out.println("⼦类狗狗的⽆参构造⽅法");
}
public Dog(String name, int health, int love, String strain) {
//通过super调⽤⽗类的构造⽅法,必须是第⼀句
//super();
super(name, health, love);
this.strain = strain;
System.out.println("狗狗的带参构造⽅法");
}
public void setStrain(String strain) {
this.strain = strain;
}
public String getStrain() {
return this.strain;
}
public void print() {
/
/调⽤⽗类的⾮private的print()⽅法
super.print();
System.out.println("我是⼀只" + this.strain);
}
public void m1() {
//super不可以调⽤⽗类的private属性
//        System.out.println(super.name);
System.out.println(super.age);
}
public void m2() {
//⼦类会覆盖服了诶的同名成员
System.out.println(this.age);
//可以使⽤super关键字调⽤⽗类被⼦类覆盖的成员
System.out.println(super.age);
}
}
super和this的区别
区别this super
访问属性访问本类的属性,如果没有则从⽗类中访问⽗类中的属性
访问⽅法访问本类⽅法,如果没有从⽗类中访问⽗类的⽅法
访问构造⽅法调⽤本类的构造⽅法,⽅法构造⽅法的⾸⾏调⽤⽗类的构造⽅法,放在⼦类的构造⽅法的⾸⾏
总结的来说是this代表的是本类,⽽super代表的是⽗类
因为super代表的是⽗类,那么如果声明了多个类时,⼦类中如果要访问⽗类的⽗类的成员,难道要使⽤的是super.super.的⽅式吗?
package cn.zhz.Kind;
//爷爷类
public class Animal {
private int age;
private String sex;
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 void print() {
System.out.println("爷爷类下的⽅法");
}
}
package cn.zhz.Kind;
public class Person extends Animal {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package cn.zhz.Kind;
/
/孙⼦类
public class Student extends Person {
private String sid;
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。