课后练习题--第三题 忘是亡心i 2021-06-24 13:57 466阅读 0赞 ## super关键字的使用 ## > 任务描述 :掌握super关键字的使用。 > 相关知识 为了完成本关任务,你需要掌握: > 1.super关键字; > 2.super关键字的使用; > 3.super与this关键字的比较。 **super关键字** > 1.在上一节中曾经提到过super的使用,那super到底是什么呢? > 2.super关键字出现在子类中,我们new子类的实例对象的时候,子类对象里面会有一个父类对象。怎么去引用里面的父类对象呢? > 3.使用super来引用,所以可以得出结论:super主要的功能是完成子类调用父类中的内容,也就是调用父类中的属性或方法。 **\#\#\#\#\#super关键字的使用** > super关键字的用法如下: > > (1)super可以用来引用直接父类的实例变量。 > (2)super可以用来调用直接父类方法。 > (3)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关键字的比较** > 1.super关键字:我们可以通过super关键字来实现对父类成员的访问,用来引用当前对象的父类。 > 2.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 **!用 super 或 this 调用构造方法时都需要放在首行,所以super 与 this 调用构造方法的操作是不能同时出现的!** \#\#\#\#编程要求 > 1.声明一个名为Person的类,里面有name与age两个属性,并声明一个含有两个参数的构造方法; > > 2.声明一个名为Student的类,此类继承自Person类,添加一个属性school,在子类的构造方法中调用父类中有两个参数的构造方法; > > 3.实例化一个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 Sname; int Sage; public Person(String Sname,int Sage){ this.Sname=Sname; this.Sage=Sage; } /********* end *********/ } class Student extends Person { /********* begin *********/ public Student(String Sname,int Sage){ super(Sname,Sage); } String school; public void print(){ System.out.print("姓名:"+Sname+","+"年龄:"+Sage+","+"学校:"+school); } /********* end *********/ } > 续上----小白冲鸭鸭鸭!!在大佬的帮助下成长(\* ̄︶ ̄)
还没有评论,来说两句吧...