public void Author(int i,String n,String d,String b,String p,String e) throws ParseException{
int id=i;
String name=n;
String description=d;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date birthday = formatter.parse(b);
String phone=p;
String email=e;
}
public String toString(){
String a="The No."+id+"author:"+"/n"+"name:"+name+"/n"+"description:";
a=a+description+"/n"+"birthday:"+birthday.toString()+"/n"+"phone:";
a=a+phone+"/n"+"email:"+email+"/n";
}
在toString的方法里,报错,根本不识别我定义的id,name,description,birthday,phone和email。为什么?我在构造函数里面定义了呀,怎么改?

解决方案 »

  1.   

    id,name,description,birthday,phone和email
    应该是你的成员变量,不应该是构造方法里的局部变量,所以toString里是不可看到的!
      

  2.   

    变量的范围,你可以这样认为的范围以前花括号{后花括号的}为界的变量在整个{}里面别的地方口可以调用。你的很明显是错误的。http://www.bcclt.cn编程村论坛
      

  3.   

    能用记事本最好的嘛,这是一个能力的象征,我一般用的editplushttp://www.bcclt.cn编程村论坛
      

  4.   

    在函数内定义的变量是局部变量,其作用域只在函数内有效,自然其它地方是无效的。其实在任何一个{ }对内定义的变量都是局部变量,只在{ }内有效,看看教材,变量的作用域和生命期。
    你要想在toString中使用上述变量,可以通过:1)定义上述变量为全局变量;2)通过参数传递来传递上述变量;3)在类声明中申明上述变量为类的变量。这些方法来完成。
      

  5.   

    public void Author(int i,String n,String d,String b,String p,String e) throws ParseException{ 
    int id=i; 
    String name=n; 
    String description=d; 
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
    Date birthday = formatter.parse(b); 
    String phone=p; 
    String email=e; 

    请注意,这方法个java编译器认为这不是构造方法!!为什么呢?因为你写的方法定义了void返回值。这是构造方法和成员方法的根本区别。
    name等属性也应该在方法外面定义,toString方法就无错了。
      

  6.   

    把变量定义在方法外:int id;
    String name;
    在构造方法这样: this.id = id
                  this.name =name;
    这要就好了
      

  7.   

    int id=i; 
    String name=n; 
    String description=d; 
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
    Date birthday = formatter.parse(b); 
    String phone=p; 
    String email=e; 
    应该把以上几个变量提到Author()方法之外,不然就是Author()这个方法的局部变量,另一个方法是看不到的,当然也就不能用了。
      

  8.   

    根据楼主给定这个代码意思应该是给这个类填充属性Bean,那声明的时候不应该放在一个方法里吧
    要想通过这个方法填充,把属性声明在外,还有你的toString()方法的return呢????
      

  9.   

    嘿嘿,谢谢大家啊,后来我自己也发现错误了..汗!
    辛苦你们了~~~
    另外,回xian_hf:俺那会儿忘记写了,不过那不重要~~~
    一人给点分吧
      

  10.   

    package com.aaron.test;import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;/**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2008</p>
     *
     * <p>Company: </p>
     *
     * @author not attributable
     * @version 1.0
     */
    public class SimpleTest {
        int id;
        String name;
        String description;
        Date birthday;
        String phone;
        String email;
        public static void main(String[] args) {
            SimpleTest test20080525 = new SimpleTest();        test20080525.Author(4, "name", "test method", "2008-04-04",
                                "1394324324",
                                "[email protected]");
            System.out.println(test20080525);
        }    public void Author(int i, String n, String d, String b, String p, String e) {
            this.id = i;
            this.name = n;
            this.description = d;
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            try {
                this.birthday = formatter.parse(b);
            } catch (ParseException ex) {
            }
            this.phone = p;
            this.email = e;
        }    public String toString() {
            String a = "The No." + id + "author:" + "/n" + "name:" + name + "/n" +
                       "description:";
            a = a + description + "/n" + "birthday:" + birthday.toString() + "/n" +
                "phone:";
            return a = a + phone + "/n" + "email:" + email + "/n";
        }}
      

  11.   

    你这个是两个不同的方法 
    id,name,description,birthday,phone和email
    是Author()里的局部变量,在该方法执行结束就被销毁
    toString()里面是无法得到该局部变量的。