解析java中的super关键字
1 前提
继承本质
a 继承内存原理的结构图
b 示例代码
Animal类
public class Animal {
private String name="张三";
private String type;
private int age;
public Animal() {
}
public Animal(String name, String type, int age) {
this.name = name;
this.type = type;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Cat类
public class Cat extends Animal{
public Cat(){
}
public Cat(String name, String type, int age) {
super(name, type, age);
}
@Override
public String toString() {
return "猫的名字为: "+getName()+",猫的种类为: "+getType()+",猫的年龄为: "+getAge();
}
}
TestCat类
public class TestCat {
public static void main(String[] args) {
Animal animal=new Animal("李四","旺财",20);
System.out.println(new Cat().getName());
Cat c=new Cat("叮当","短尾",9);
System.out.println(c.toString());
}
}
c 示例代码运行截图
2 super关键字含义
super词义
子类继承父类后,会把父类的所有属性和方法都拷贝一份到自己实例化对象所在的堆空间中,我们把那块区域叫做父类型特征区,而super就是找到父类型特征区的路标,可以通过super.方式区访问其里面的内容.
简单来说,作用就在子类方法中调用子类中的父类型特征区的属性和方法去进行操作。如果修改里面的值,也仅仅是特征区的值在变化,并不会影响父类的中的相同名字属性的内容
3 为什么要使用super关键字?
访问并调用父类型特征区里面的属性和方法,使得子类可以使用继承到的属性和方法,若不写super.,其是先找堆空间里面有该属性和方法,若无再去找父类型特征区的属性和方法,写了可以节省一步我们人为已知的无效操作(寻找堆空间)。
4.如何使用super关键字
4.1 super.属性名
目的
得到父类型特征区中的属性值
示例代码
Animal类
public class Animal {
private String name="张三";
private String type;
private int age;
public Animal() {
}
public Animal(String name, String type, int age) {
this.name = name;
this.type = type;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Cat类
public class Cat extends Animal{
public Cat(){
}
public Cat(String name, String type, int age) {
super(name, type, age);
}
@Override
public String toString() {
return "猫的名字为: "+getName()+",猫的种类为: "+getType()+",猫的年龄为: "+getAge();
}
}
TestCat类
public class TestCat {
public static void main(String[] args) {
Animal animal=new Animal("李四","旺财",20);
System.out.println(new Cat().getName());
}
}
示例代码运行截图
4.2 super.方法名(实参列表)
目的
得到父类型特征区中的方法
示例代码
Animal类
public class Animal {
private String name="张三";
private String type;
private int age;
public Animal() {
}
public Animal(String name, String type, int age) {
this.name = name;
this.type = type;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void initalValue(){
System.out.println("打印赋值副本时的初始值,名字为:"+name+",种类为: "+type+",年龄为:"+age);
}
}
Cat类
public class Cat extends Animal{
public Cat(){
}
public Cat(String name, String type, int age) {
super(name, type, age);
}
@Override
public String toString() {
return "猫的名字为: "+getName()+",猫的种类为: "+getType()+",猫的年龄为: "+getAge();
}
}
TestCat类
public class TestCat {
public static void main(String[] args) {
Animal animal=new Animal("李四","旺财",20);
new Cat().initalValue();
}
}
示例代码运行截图
4.3 super(实参列表)
目的
调用父类型特征区的构造方法,给父类型特征区里面的属性进行赋值
示例代码
Animal类
public class Animal {
private String name="张三";
private String type;
private int age;
public Animal() {
}
public Animal(String name, String type, int age) {
this.name = name;
this.type = type;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void initalValue(){
System.out.println("打印赋值副本时的初始值,名字为:"+name+",种类为: "+type+",年龄为:"+age);
}
}
Cat类
public class Cat extends Animal{
public Cat(){
}
public Cat(String name, String type, int age) {
super(name, type, age);
}
@Override
public String toString() {
return "猫的名字为: "+getName()+",猫的种类为: "+getType()+",猫的年龄为: "+getAge();
}
}
TestCat类
public class TestCat {
public static void main(String[] args) {
Animal animal=new Animal("李四","旺财",20);
//会调用父类型特征的构造方法,对里面的属性进行赋值操作
System.out.println(new Cat("叮叮当当", "加菲猫", 10).toString());
}
}
示例代码运行截图
4.4 规则
a 子类继承父类时,构造方法里面没有写this();系统运行时会在首行自动加上super()关键字,也就是会默认先调用父类的无参构造方法(模拟先有父亲,再有儿子);
b.super()必须出在子类构造方法的第一行
c. super关键字是只能出现在实例方法中,不能出现在static修饰的方法中
原因:因为new创建空间之后才有super关键字,而static修饰的方法无需要new对象。因此不能出现在static修饰的方法中