java:12继承中的构造构造函数的编写方法

来源:互联网 发布:男士护肤 知乎 旁氏 编辑:程序博客网 时间:2024/05/21 08:43

一、子类返回参数到父类里面

package javastudy;public class Student extends Person {    private int score;    //构造函数    public Student()    {        //这个类似于Person(null,160)        //如果构造函数里没有name AND height这两个参数,我就直接super到        super(null,160);        score=0;    }    public Student(String name,int height,int score)    {        //如果Student构造函数里直接指定了这两个参数,也是类似上面的        //通过super函数,直接设置Person里面的参数的值。        //为什么不能通过定义的对象直接调用呢?因为name AND height这两个参数是私有的        //在Student里面不能直接调用,但是可以通过这种方式进行设置;        super(name,height);        this.score=score;    }    public void show()    {        System.out.println("姓名="+name+"身高="+height+"成绩="+score);    }}

这里小结一下,这里的继承的构造函数写法基本上和父类的写法基本上一致的,这里就突出一个super(null,0)的用法

二、

package javastudy;public class TestIt {    public static void main(String[] args)     {//      Person z=new Person("zhang",170);//      z.show();//      //如果定义了构造函数,那就必须按照构造函数的定义严格输入参数;//      Person w=new Person();//      w.show();        Student s=new Student();        s.show();        Student t=new Student("wang",180,90);        t.show();    }}