234234234

构造方法

                构造方法


构造方法:  作用是用于初始化参数

  1. 所有的数字变量全部设置为0 默认值
  2. 所有的Boolean类型全部设置为false   默认值
  3. 所有的对象变量全部设置为null   默认值
  4. 如果自己写了有参的构造方法,那么编辑器不会在提供默认的构造的构造方法了。
  5. 如果我们还想用无参的构造方法,那么需要手动实现。

    package demo6;

    public class FunReset {
    String name;
    int age;
    String gender;
    String height;

    public FunReset() {

    }

    public FunReset(String name) {
    // super();// 调用Object 的构造函数
    this.name = name;
    }
    public FunReset(String name, int age) {
    this(name); // 调用前面一个构造函数
    this.age = age;
    }
    public FunReset(String name, int age, String gender) {
    this(name, age); // 调用前面一个构造函数
    this.gender = gender;

    }
    public FunReset(String name, int age, String gender, String height) {
    this(name, age, gender); // 调用前面一个构造函数
    this.height = height;
    }
    }

  6. 结果如图:  

2018-06-10    20:53:21

posted @ 2018-06-10 20:53  你若愿意,我一定去  阅读(118)  评论(0编辑  收藏  举报
23423423423