------ Solution ---------------------------------------- ----
String toString method of Object rewritten , System.out.print (S1) , call toString method to print out the value of String . Otherwise, print a bunch of addresses.
------ Solution ---------------------------------------- ----
written examples below , do not know can not meet your request. You should be able to understand
public class TestString {
/**
* <pre>
* @author kanpiaoxue
* Date 2013-11-14
* 当前类没有重写Object的 public String toString()
* 这样,在打印该实例的引用的时候,会调用 Object 的 toString() 方法。
*
* 下面的这个 toString() 是JDK中Object类的方法。可以看见它的组成是由"类名 + hashCode"组成的。
* public String toString() {
* return getClass().getName() + "@" + Integer.toHexString(hashCode());
* }
* </pre>
*/
class WithoutToString {
protected String name;
protected int age;
public WithoutToString(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
/**
* <pre>
* @author kanpiaoxue
* Date 2013-11-14
* 当前类重写了 Object的 public String toString()
* 在打印该实例的引用的时候,因为这里重写了 Object的 public String toString(),所以就调用当前类的 public String toString()方法。
* </pre>
*/
class WithToString extends WithoutToString {
public WithToString(String name, int age) {
super(name, age);
}
@Override
public String toString() {
return "WithToString [name=" + name + ", age=" + age + "]";
}
}
/**
* @param args
*/
public static void main(String[] args) {
TestString test = new TestString();
String name = "kanpiaoxue";
int age = 200;
WithoutToString a = test.new WithoutToString(name, age);
System.out.println(a);
WithToString b = test.new WithToString(name, age);
System.out.println(b);
// -------- output ------------
//com.wanmei.test.TestString$WithoutToString@2a9931f5
//WithToString [name=kanpiaoxue, age=200]
}
}
------ Solution ------------------------------------- -------
in your own defined classes override toString () method
So you print out what you override the toString () method returns a
------ Solution ------------------ --------------------------
the floor inside more than good , full of enthusiasm , a good experience the main floor , or wall to go .
------ For reference only -------------------------------------- -
did not understand what mean.
------ For reference only -------------------------------------- -
is System.out to achieve the output , rather than String, want to know how to achieve the required look at the source , jdk directory src.zip
String interior is an array of characters , so you can use the index value to achieve substring
------ For reference only ------------------- --------------------
out his print method is OutputStream object when printing . print (Object o).
The first is to call String.valueOf (o) into bytes before printing. See the specific API
public void print (Object obj)
Prints an object. The string produced by the String.valueOf (Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write (int) method.
Parameters:
obj - The Object to be printed
See Also:
Object.toString ()
------ For reference only ----------------------------- ----------
this sentence mean really do not know
------ For reference only ------------ ---------------------------
landlord you see your description of the problem is not clear, three personal answer to three places to go. . .
------ For reference only -------------------------------------- -
is my own definition of String class, have their own definition of the class object to print output, but the output is the object was referenced , who was operating the function returns the class object is referenced in print
Is my own definition of String class, have their own definition of the class object to print output, but the output is the object was referenced , who was operating the function returns the class object is referenced in print
------ For reference only ---------------------------------------
is my own definition of String class, have their own definition of the class object to print output, but the output is the object was referenced , who was operating the function returns the class object is referenced in print
------ For reference only ---------------------------------- -----
is my own definition of String class, have their own definition of the class object to print output, but the output is the object was referenced , who was operating the function returns the class object is referenced in print < br> ------ For reference only ---------------------------------------
is my own definition of String class, have their own definition of the class object to print output, but the output is the object was referenced , who was operating the function returns the class object is referenced in print
--- --- For reference only ---------------------------------------
is my define their own String class, have their own definition of the class object to print output, but the output is the object was referenced , who was operating the function returns the class object is referenced in print
toString method that you write again
------ For reference only ----------------------- ----------------
thank you for it , basically understand
没有评论:
发表评论