在构造Java类是有一条:对于不带参数的构造器在其第一行用this关键字来调用本类的其他构造器!这有什么具体用处??

解决方案 »

  1.   

    this 关键字是用来表示当前对象的··构造器的意思应该也是一个对象··
      

  2.   

    这样在写代码构造对象的时候,就省去了传参数的繁琐,之间new + 类名就搞掂了
      

  3.   

    这样在写代码构造对象的时候,就省去了传参数的繁琐,直接new + 类名就搞掂了
      

  4.   

    this便是当前的变量。你的问题不是很明白。
      

  5.   

    设置默认值
    例如
    class num{
    num(){
    this(1);//说明默认构造方法参数m默认值是1
    }
    num(int m){
    }
    }
      

  6.   

    因为一个类进行new之前,类实例(包括实例变量,方法)是不存在的,注意实例变量与静态的区别,构造函数的作用就是初始化,new其实就是构造类对象,初始化函数的过程。this关键字为你提供了多种初始化类实例的途径。仔细研究下面的这个类,
    public class Exercise
    {
    String name;
    int    age;
    String sex;
    int height;
    Exercise(String name,int age,String sex,int height)
    {
    this.name=name;
    this.age=age;
    this.sex=sex;
    this.height=height;
    }
    Exercise(String name,int age,String sex)
    {
    this(name,age,sex,getHeight());
    }
    Exercise(String name,int age)
    {
    this(name,age,getSex());
    }

    Exercise(String name)
    {
    this(name,getAge());
    }
    Exercise()
    {
    this(getName());
    }
    public static void main(String[] args)
    {
    Exercise ex=new Exercise("Mike",40);//多种参数都行。
    System.out.println(ex);

    }

    public String toString()
    {
    return "name="+this.name+" age="+this.age+
    " sex="+this.sex+" height="+this.height;
    }

    static String getName()
    {
    String name="Peter";
    return name;
    }
    static int getAge()
    {
    int age=25;
    return age;
    }

    static String getSex()
    {
    String sex="man";
    return sex;
    }

    static int getHeight()
    {
    int height=168;
    return height;
    }
    }