java用super调用父类的构造方法:

2019年2月15日23:04:41 发表评论 1,036 views
import java.util.SplittableRandom;

/**
 * Created by xabcd on 2019/2/15.
 */
public class Person7
{
    String name;
    int age;
    public Person7(String name,int age){
        this.name=name;
        this.age= age;
        //System.out.println("1.public Person7(){}" );

    }
}
class Studentt extends Person7{
    String school;
    public Studentt()
    {
        //在这里调用父类的构造方法
        super("张三",25);
        //System.out.println("2.public Student(){}");
    }


    public static void main(String[] args)
    {
        Studentt s = new Studentt();
        s.school= "北京";
        System.out.println("姓名:"+s.name+",年龄"+s.age+",学校:"+s.school);

    }
}





结果:
姓名:张三,年龄25,学校:北京






注意: 用 super 调用 父 类 中的 构造 方法, 只能 放在 程序 的 第 1 行。 super 关键字 不仅 可用 于 调用 父 类 中的 构造 方法, 也可 用于 调用 父 类 中的 属性 或 方法,

 

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: