本文共 2047 字,大约阅读时间需要 6 分钟。
class Demo{} 等价于以下的代码: class Demo extends Object{} |
class Person{ }; public class Demo13{ public static void main(String artgs[]){ Person p = new Person() ; System.out.println(p) ; } }; |
Person@757aef |
class Person{ public String toString(){ return "HELLO WORLD!!!" ; } }; public class Demo13{ public static void main(String artgs[]){ Person p = new Person() ; System.out.println(p) ; } }; |
HELLO WORLD!!! |
class Person{ private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } public String toString(){ return " 姓名: "+this.name+" ,年龄: "+this.age ; } }; public class Demo13{ public static void main(String artgs[]){ Person p = new Person(" 张三 ",30) ; System.out.println(p) ; } }; |
class Person{ private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } public boolean equals(Object obj){ if(this==obj){ return true ; } if(!(obj instanceof Person)){ return false ; } Person p = (Person)obj ; if(this.name.equals(p.name)&&this.age==p.age){ return true ; }else{ return false ; } } public String toString(){ return " 姓名: "+this.name+" ,年龄: "+this.age ; } }; public class Demo14{ public static void main(String artgs[]){ Person p1 = new Person(" 张三 ",30) ; Person p2 = new Person(" 张三 ",30) ; System.out.println(p1.equals(p2)) ; } }; |