Method object class & toString

object class

Is the foundation class for all java class

If not specified base class extends keyword in the class declaration, the default is the base class for the object class

 

ToString Method

 

the object class definition has public  String  toString () method which returns a value of type String, information describing the current object.

Upon connection operations (such as String and other types of data: System.out.println ( "info + person ()), it will automatically call toString () method of the object class

Can be rewritten toString () method in accordance with user-defined types.

 

public class TestToString {
	public static void main(String[] args) {
	Dog d = new Dog();
	System.out.println("d:="+ d.toString());
	
	}
}

class Dog {
	public String toString() {
		return "I'm a cool Dog";
	}
	
}

 

Guess you like

Origin www.cnblogs.com/lsswudi/p/11263252.html