动手动脑--super关键字

请自行编写代码测试以下特性(动手动脑): 在子类中,若要调用父类中被覆盖的方法,可以使用super关键字。

package TestInherits;

public class Person

{

    String name;

    int age;

    double score;

    void introduce()

    {

            String name;

            int age;

            double score;

            System.out.println("我的名字是:"+name+",我的年龄是:"+age+",我的成绩是:"+score);

     }

}

 

package TestInherits;

public class Student extends Person

{

    void introduce()

    {

          System.out.println("我的名字是:"+name+",我的年龄是:"+age+",我的成绩是:"+score);     

    }

}

 

package TestInherits;

public class Fruit

{

      public static void main(String[] args)

      {

              String name;

              int age;

              double score;

              // 生成子类对象,调用子类的方法

              Student a=new Student();

              a.name="hsn";

              a.age=10;

              a.score=99.0;

              a.introduce();

              // 生成父类对象,调用父类的方法

              Person b=new Person();

              b.name="hhh";

              b.age=20;

              b.score=120;

              b.introduce();

    }

}

结果:

posted @ 2022-10-14 00:16  一统天下。  阅读(16)  评论(0编辑  收藏  举报