导读
1.继承的简单实现
2.重载与重写
3.继承的初始化顺序
4.super关键字
继承的简单实现
Java只支持单继承,即一个子类只能有一个父类
animal父类
package com.hala.animal;
public class Animal {
private String name;
private int math;
private String species;
public Animal(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
//吃东西
public void eat(){
System.out.println(this.getName()+"正在吃东西。");
}
}
Cat子类
package com.hala.animal;
public class Cat extends Animal {
//子类可以继承父类所有的非私有成员,没有选择性
private double weight;
public Cat(){
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
//跑动方法
public void run(){
System.out.println(this.getName()+"正在跑。");
}
}
Dog子类
package com.hala.animal;
public class Dog extends Animal {
private String sex;
public Dog(){
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void sleep(){
System.out.println(this.getName()+"正在睡觉。");
}
}
测试类
package com.hala.test;
import com.hala.animal.Cat;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cat one=new Cat();
one.setName("花花");
one.eat();
}
}
输出结果
花花正在吃东西。
重载与重写
访问修饰符的范围
这里同包包括同包子类和同包非子类
子类则指跨包子类
其他指的是跨包非子类
animal父类
package com.hala.animal;
public class Animal {
private String name;
private int math;
private String species;
public Animal(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
//吃东西
public void eat(){
System.out.println(this.getName()+"正在吃东西。");
}
}
Dog子类
package com.hala.animal;
public class Dog extends Animal {
private String sex;
public Dog(){
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void sleep(){
System.out.println(this.getName()+"正在睡觉。");
}
//重写eat()方法
public void eat(){
System.out.println(this.getName()+"最近没有食欲~");
}
}
/*方法的重载:
* 1.在同一个类中
* 2.方法名相同,参数列表不同(包括参数个数,顺序,类型)
* 3.方法返回值,访问修饰符任意
* 4.如果两个方法仅仅是参数名称不同,其他都相同,不构成重载
* 如:public void run(int age,String name)和public void run(int name,String age)
*/
/*方法的重写:
* 1.在有继承关系的子类中
* 2.方法名相同,参数列表相同(包括参数个数,顺序,类型)
* 3.访问修饰符限制:子类访问修饰符范围要>=父类访问修饰符范围
* 4.子类中参数名可以与父类中不同
* 5.重写不仅可以用于方法上还可以用于属性上
* 6.返回值可以相同也可以是父类返回值的派生类
* 7.static方法和final方法都不能被重写
*/
测试类
package com.hala.test;
import com.hala.animal.Dog;
public class Test {
public static void main(String[] args) {
Dog two =new Dog();
two.setName("二哈");
two.eat();
}
}
输出结果
二哈最近没有食欲~
⚠️重写快捷键:写父类的函数名,点alt+/,自动补全
继承的初始化顺序
父类静态属性成员||父类静态代码块->子类静态属性成员||子类静态代码块->父类其他属性成员||父类构造代码块->父类构造方法->子类其他属性成员||子类构造代码块->子类构造方法
⚠️这里||的含义是两者的加载顺序没有先后,与代码在程序中的相对位置有关
animal父类
package com.hala.animal;
public class Animal {
protected String name;
private int math;
private String species;
private static int st1=1;
public static int st2=2;
static{
System.out.println("我是父类的静态代码块");
}
{
System.out.println("我是父类的构造代码块");
}
//父类构造方法不能被继承,不能被重写
public Animal(){
System.out.println("我是父类的无参构造方法");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
//吃东西
public void eat(){
System.out.println(this.getName()+"正在吃东西。");
}
}
Cat子类
package com.hala.animal;
public class Cat extends Animal {
//子类可以继承父类所有的非私有成员,没有选择性
private double weight;
public static int st3=3;
static{
System.out.println("我是子类的静态代码块");
}
{
System.out.println("我是子类的构造代码块");
}
public Cat(){
System.out.println("我是子类的无参构造方法");
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
//跑动方法
public void run(){
System.out.println(this.getName()+"正在跑。");
}
}
测试类
package com.hala.test;
import com.hala.animal.Cat;
import com.hala.animal.Dog;
public class Test {
public static void main(String[] args) {
Cat one =new Cat();
System.out.println(one.st2);
}
}
输出结果
我是父类的静态代码块
我是子类的静态代码块
我是父类的构造代码块
我是父类的无参构造方法
我是子类的构造代码块
我是子类的无参构造方法
2
super关键字
animal父类
package com.hala.animal;
public class Animal {
protected String name;
private int math;
private String species;
private static int st1=1;
public static int st2=2;
//父类构造方法不能被继承,不能被重写
public Animal(){
System.out.println("我是父类的无参构造方法");
}
public Animal(String name,int math){
System.out.println("我是父类的代参构造");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
//吃东西
public void eat(){
System.out.println(this.getName()+"正在吃东西。");
}
}
Dog子类
package com.hala.animal;
public class Dog extends Animal {
private String sex;
public Dog(){
System.out.println("我是子类的无参构造");
}
//如果子类带参构造方法中没有说明,会默认调取父类无参构造方法
//正是由于它会默认调用无参构造方法,凸显无参构造方法的重要性,所以要养成写无参构造的好习惯
public Dog(String name,int math){
System.out.println("我是子类的代参构造方法");
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//睡觉方法
public void sleep(){
eat();
super.eat();//用super访问父类中的eat()
System.out.println(super.name);
//用super还可以访问父类中的 允许 子类访问的属性
System.out.println(this.getName()+"正在睡觉。");
}
//重写eat()方法
public void eat(){
System.out.println(this.getName()+"最近没有食欲~");
}
}
测试类
package com.hala.test;
import com.hala.animal.Dog;
public class Test {
public static void main(String[] args) {
Dog two =new Dog();
two.setName("二哈");
two.sleep();
System.out.println("+++++++++++++++++++++++++");
Dog one=new Dog("二哈",2);
System.out.print(one.st2);
}
}
输出结果
我是父类的无参构造方法
我是子类的无参构造
二哈最近没有食欲~
二哈正在吃东西。
二哈
二哈正在睡觉。
+++++++++++++++++++++++++++++++
我是父类的无参构造方法
我是子类的代参构造方法
2
animal父类
package com.hala.animal;
public class Animal {
protected String name;
private int math;
private String species;
private static int st1=1;
public static int st2=2;
//父类构造方法不能被继承,不能被重写
public Animal(){
System.out.println("我是父类的无参构造方法");
}
public Animal(String name,int math){
System.out.println("我是父类的代参构造");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
//吃东西
public void eat(){
System.out.println(this.getName()+"正在吃东西。");
}
}
Dog子类
package com.hala.animal;
public class Dog extends Animal {
private String sex;
public Dog(){
System.out.println("我是子类的无参构造");
}
public Dog(String name,int math){
super(name,math);
//要调用父类带参构造方法,就要用super加相应参数来调用
//但要注意super语句必须放在第一行
System.out.println("我是子类的代参构造方法");
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//睡觉方法
public void sleep(){
eat();
super.eat();//用super访问父类中的eat()
System.out.println(super.name);
//用super还可以访问父类中的 允许 子类访问的属性
System.out.println(this.getName()+"正在睡觉。");
}
//重写eat()方法
public void eat(){
System.out.println(this.getName()+"最近没有食欲~");
}
}
测试类
package com.hala.test;
import com.hala.animal.Dog;
public class Test {
public static void main(String[] args) {
Dog one=new Dog("二哈",2);
System.out.print(one.st2);
}
}
输出结果
我是父类的代参构造
我是子类的代参构造方法
2