Java基础编程题目——super关键字的使用

super可以用来引用直接父类的实例变量。
super可以用来调用直接父类方法。
super()可以用于调用直接父类构造函数。

代码最后使用super调用父类的方法

import java.util.SplittableRandom;

public class text {
    public static void main(String[] args) {
        Student P = new Student();
        P.setName("张三");
        P.setAge(18);
        P.school = "哈佛大学";
        System.out.println(P.talk());
    }
}

class Person3 {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String talk() {
        String s = "我是:" + this.getName() +
                ",今年:" + this.getAge() + "岁,";
        return s;
    }
}

class Student extends Person3 {
    String school;
    public String  talk() {
        String s = super.talk() + "我在" + this.school + "上学";
        return s;
    }
}
发布了203 篇原创文章 · 获赞 14 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43479432/article/details/105085045