Topic: 请问一个关于构造方法之间调用的问题

  Print this page

1.请问一个关于构造方法之间调用的问题 Copy to clipboard
Posted by: 2352439
Posted on: 2005-04-15 02:02

我是个JAVA新手,请问各位大虾们:下面的代码到底是哪出错了

class person
{
  private int age;
  private String name;
void shout()
{
system.out.println(name+"'s age is"+age);
}
  public person()
  {
    System.out.println("wo hao ben");
  }
  public person(String name)
  {
    this.name=name;
  }
  public person(String name,int age)
  {
    //this.name=name;
    this.age=age;
    this(name);
  }
  public static void main(String[] args)
  {
    person p1=new person("wangwu");
    person p2=new person("zhanglu",50);
    person p3=new person();
    p1.shout();
    p2.shout();
    p3.shout();
  }
}

编译时出现这样的错误:
D:\javawork\lesson3>javac lesson43.java
lesson43.java:21: call to this must be first statement in constructor
this(name);
^
1 error

2.Re:请问一个关于构造方法之间调用的问题 [Re: 2352439] Copy to clipboard
Posted by: 2352439
Posted on: 2005-04-15 02:11

跪求答案呀!!!!
哪位好心的可怜可怜我呀
为了学JAVA,我考试都没时间复习了

3.Re:请问一个关于构造方法之间调用的问题 [Re: 2352439] Copy to clipboard
Posted by: why
Posted on: 2005-04-15 03:11

lesson43.java:21: call to this must be first statement in constructor
this(name);


public person(String name,int age)
{
this(name); // make this constructor call the first statement!!!
//this.name=name;
this.age=age;
}

BTW, "class person" should have used "class Person", by convention.
In fact, it should be "public class Person"

4.Re:请问一个关于构造方法之间调用的问题 [Re: 2352439] Copy to clipboard
Posted by: 2352439
Posted on: 2005-04-15 11:05

谢谢这位大哥,我等了一夜,终于有结果了


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923