Dav*_*d K 2 java object tostring
我有一组表示数字逻辑门的类,我想创建一个toString()命令,它将以递归方式通过并打印它们.问题是它们可以包含Gate,Boolean或Integer,当我调用Boolean.toString()或Integer.toString()时,将打印Object ID而不是指定的值.有没有办法在这些对象上一般调用toString()(或类似的)命令并让它们打印指定的值?
使用下面的代码,输出看起来像:"AND([I @ 6ca1c,[Z @ 1bf216a]"
我希望它看起来像:"AND(11,true)"
public static class Gate{
public Object in1;
}
public static class ANDgate extends Gate{
public Object in2;
public ANDgate(Object first,Object second){
in1 = first; // these can be Integer, Boolean, or another Gate
in2 = second;
}
public String toString(){
return("AND(" + in1 + "," + in2 + ")");
}
}
public static class NOTgate extends Gate{
public NOTgate(Object obj){
in1 = obj; // this can be Integer, Boolean, or another Gate
}
public String toString(){
return("NOT(" + in1 + ")");
}
}
Run Code Online (Sandbox Code Playgroud)
你输出清晰地说,传递int[]和boolean[]到您的构造函数,而不是Integer和Boolean你说了(见Class.getName()二进制类型名称的含义,如[I).
如果是预期的,那么您需要提供自己的函数来将数组转换为String并使用它来代替使用toStirng()返回数组的基于散列值的默认实现.
| 归档时间: |
|
| 查看次数: |
290 次 |
| 最近记录: |