java - Convert Object to String -
i have couple class in i'm getting , setting few things , calling in main method. when call class in main method gives me object instead of name,address , age. know structure complicated want keep structure because later on adding lot of things this. amazing if tell me how this. appreciate this. below code classes
this first class
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; } }
this second class
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; } }
this third class
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; } }
this fourth class method maker
public class methodmaker { public methodthree brandnewfunction(methodtwo object) { methodthree thirdmethod = new methodthree(); thirdmethod.setmethodtwoinmethodthree(object); return thirdmethod; } }
this main class calls methodmaker. want achieve when print value should print name,address , age instead prints trial.methodthree@4de5ed7b
public class mainclass { public static void main(string args[]) { methodmaker makerofmethods = new methodmaker(); methodone 1 = new methodone(); methodtwo object = new methodtwo(one); system.out.println(makerofmethods.brandnewfunction(object).tostring()); }
}
what need override default implementation of .tostring()
method in objects want print out:
@override public string tostring() { return "name: " + this.name; }
edit: not know printing, , naming convention doesn't out, understanding, need implement in of classes since seem related each other.
so, in methodone
class (can applied methodtwo
):
@override public string tostring() { return "name: " + this.name + " age: " + this.age + " address: + " this.address; }
in methodthree
class:
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(); }
Comments
Post a Comment