任务描述
相关知识
super关键字
super关键字的使用
super与this关键字的比较
编程要求
测试说明
任务描述
本关任务:掌握super关键字的使用。
相关知识
为了完成本关任务,你需要掌握:1.super关键字;2.super关键字的使用;3.super与this关键字的比较。
super关键字
在上一节中曾经提到过super的使用,那super到底是什么呢?super关键字出现在子类中,我们new子类的实例对象的时候,子类对象里面会有一个父类对象。怎么去引用里面的父类对象呢?使用super来引用,所以可以得出结论:super主要的功能是完成子类调用父类中的内容,也就是调用父类中的属性或方法。
super关键字的使用
super关键字的用法如下:
super可以用来引用直接父类的实例变量。
super可以用来调用直接父类方法。
super()可以用于调用直接父类构造函数。
1.super用于引用直接父类实例变量
public class TestSuper1 {
public static void main(String args[]) {
Dog d = new Dog();
d.printColor();
}
}
class Animal {
String color = "white";
}
class Dog extends Animal {
String color = "black";
void printColor() {
System.out.println(color);// prints color of Dog class
System.out.println(super.color);// prints color of Animal class
}
}
输出结果:
black
white
在上面的例子中,Animal和Dog都有一个共同的属性:color。 如果我们打印color属性,它将默认打印当前类的颜色。要访问父属性,需要使用super关键字指定。
2.通过super来调用父类方法
public class TestSuper2 {
public static void main(String args[]) {
Dog d = new Dog();
d.work();
}
}
class Animal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal {
void eat() {
System.out.println("eating bread...");
}
void bark() {
System.out.println("barking...");
}
void work() {
super.eat();
bark();
}
}
输出结果:
eating...
barking...
在上面的例子中,Animal和Dog两个类都有eat()方法,如果要调用Dog类中的eat()方法,它将默认调用Dog类的eat()方法,因为当前类的优先级比父类的高。所以要调用父类方法,需要使用super关键字指定。
3.使用super来调用父类构造函数
public class TestSuper3 {
public static void main(String args[]) {
Dog d = new Dog();
}
}
class Animal {
Animal() {
System.out.println("animal is created");
}
}
class Dog extends Animal {
Dog() {
super();
System.out.println("dog is created");
}
}
输出结果:
animal is created
dog is created
注意:如果没有使用super()或this(),则super()在每个类构造函数中由编译器自动添加。
super与this关键字的比较
super关键字:我们可以通过super关键字来实现对父类成员的访问,用来引用当前对象的父类。
this关键字:指向自己的引用。
范例:
public class TestAnimalDogDemo {
public static void main(String[] args) {
Animal a = new Animal();
a.eat();
Dog d = new Dog();
d.eatTest();
}
}
class Animal {
void eat() {
System.out.println("animal : eat");
}
}
class Dog extends Animal {
void eat() {
System.out.println("dog : eat");
}
void eatTest() {
this.eat(); // this 调用自己的方法
super.eat(); // super 调用父类方法
}
}
输出结果:
animal : eat
dog : eat
animal : eat
上表对 this 与 super 的差别进行了比较,从上表中不难发现,用 super 或 this 调用构造方法时都需要放在首行,所以super 与 this 调用构造方法的操作是不能同时出现的。
编程要求
根据提示,在右侧编辑器Begin-End处补充代码:
声明一个名为Person的类,里面有name与age两个属性,并声明一个含有两个参数的构造方法;
声明一个名为Student的类,此类继承自Person类,添加一个属性school,在子类的构造方法中调用父类中有两个参数的构造方法;
实例化一个Student类的对象s,为Student对象s中的school赋值,打印输出姓名:张三,年龄:18,学校:哈佛大学。
测试说明
测试输入:无
预期输出:
姓名:张三,年龄:18,学校:哈佛大学
开始你的任务吧,祝你成功!
package case3;
public class superTest {
public static void main(String[] args) {
// 实例化一个Student类的对象s,为Student对象s中的school赋值,打印输出信息
/********* begin *********/
Student s=new Student("张三",18);
s.school="哈佛大学";
s.print();
/********* end *********/
}
}
class Person {
/********* begin *********/
String name;
int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
/********* end *********/
}
class Student extends Person {
/********* begin *********/
String school;
public Student(String name,int age){
super(name,age);
}
public void print(){
System.out.print("姓名:"+name+","+"年龄:"+age+","+"学校:"+school);
}
/********* end *********/
}