将对象转换为字符串

时间:2014-05-30 07:39:03

标签: java string object get converter

我有几个上课,我正在收集并设置一些内容,然后最终在我的main方法中调用它。但是当我在main方法中调用我的类时,它只给了我一个对象而不是名字,地址和年龄。我知道这个结构非常复杂,但我想保留这个结构,因为稍后我将添加很多东西。如果有人能告诉我如何做到这一点,那将是令人惊讶的。我真的很感激。下面是我所有课程的代码

这是我的第一堂课

   public class methodOne
    {
      public String getName()
      {
         String name = "UserOne";
         return name;
      }

     public int getAge()
     {
        int age = 17;
        return age;
     }

     public String getAddress()
     {
       String address  = "United States";
       return address;
     }
 }

这是我的第二堂课

    public class methodTwo
{
   String name;
   String address;
   int age;

   public methodTwo(methodOne objectOne)
   {
     name=objectOne.getName();
     address=objectOne.getAddress();
     age=objectOne.getAge();
   }
   public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}

这是我的第三课

public class methodThree {

        private methodTwo methodTwoInMethodThree;
        private methodOne methodOneInMethodThree;

        public methodThree()
        {
            this.methodOneInMethodThree = new methodOne();
            this.methodTwoInMethodThree = new methodTwo(methodOneInMethodThree);
        }

        public methodTwo getMethodTwoInMethodThree() {
            return methodTwoInMethodThree;
        }

        public void setMethodTwoInMethodThree(methodTwo methodTwoInMethodThree) {
            this.methodTwoInMethodThree = methodTwoInMethodThree;
        }


}

这是我的第四个类,即方法制作者

public class methodMaker {

    public methodThree brandNewFunction(methodTwo object)
    {
        methodThree thirdMethod = new  methodThree();

        thirdMethod.setMethodTwoInMethodThree(object);

        return thirdMethod;
    }

}

这是我调用methodMaker的主类。我想要实现的是,当我打印该值时,它应该打印姓名,地址和年龄,但它只是打印trial.methodThree@4de5ed7b

public class mainClass {

public static void main(String args[])
{
    methodMaker makerOfMethods  = new methodMaker();
    methodOne one = new methodOne();
    methodTwo object = new methodTwo(one);

    System.out.println(makerOfMethods.brandNewFunction(object).toString());

}

}

4 个答案:

答案 0 :(得分:0)

您需要做的是覆盖要打印的对象中.toString()方法的默认实现:

@Override
public String toString()
{
    return "Name: " + this.name; 
}

编辑: 我不确切知道你在哪里打印,而且你的命名约定并没有真正帮助,但是根据我的理解,你需要在所有类中实现它,因为它们似乎都与每个类都相关其他

因此,在您的methodOne课程中(也可以应用于methodTwo):

@Override
public String toString()
{
    return "Name: " + this.name + " Age: " + this.age + " Address: + " this.address;
}

methodThree课程中:

 private methodTwo methodTwoInMethodThree;
 private methodOne methodOneInMethodThree;

@Override
public String toString()
{
    StringBulder sb = new StringBuilder();
    if(this.methodTwoInMethodThree != null)
    {
         sb.append("Method 2:").append(methodTwoInMethodThree.toString());
    }

    if(methodOneInMethodThree != null)
    {
        sb.append("Method 1:").append(methodOneInMethodThree.toString());
    }  

    return sb.toString();
}

答案 1 :(得分:0)

致电时

MyClass myObject = new MyClass();
System.out.println(myObject);

隐式地,java调用

System.out.println(myObject.toString());

因此,如果在MyClass中,您覆盖toString(),则无论您的toString方法返回什么,都会打印出来。


旁注:您是否混淆了类和方法?方法是类中的函数,类是围绕一堆属性和方法的包装器。你的命名令人困惑。

答案 2 :(得分:0)

试试这段代码:

 public class methodTwo
{
   String name;
   String address;
   int age;

   public methodTwo(methodOne objectOne)
   {
     name=objectOne.getName();
     address=objectOne.getAddress();
     age=objectOne.getAge();
   }
   public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public String toString(){
      return name+" "+address+" "+age;
    }

}

答案 3 :(得分:0)

您是否使用println()打印对象?

来自docs,println():

  

首先调用String.valueOf(x)来获取打印对象的字符串值

此字符串值是从对象的toString()方法获得的,其中:

  

返回一个字符串,该字符串由对象为实例的类的名称,符号字符“@”以及对象的哈希码的无符号十六进制表示形式组成

因此,如果要打印除此之外的任何内容,则必须覆盖对象中的toString()方法,并返回包含所需内容的字符串。

只是谷歌“覆盖tostring java”,你会看到很多例子。