5-2 toString方法

package object;

public class Employee {

    /**
     * @author:lixh
     */
    
    private String name;
    private int x;
    
    public static void main(String[] args) {
        
        String string = "aa";
        string.toString();
        
        Employee e1 = new Employee();
//        e1.toString();//4.不重写,默认是类名加散列码
        
        System.out.println(e1);//3.默认调用的是 e1.toString()
    }
    
    /*
     * 1.toString() 用于返回表示对象值的字符串
     * 
     * 
     *2. ""+x 调用是 ""+x.toString
     */
    
    public String toString(){
        return getClass().getName()+"[name:" +name + ",x:"+x+"]";
    }
}
package object;

public class Manager extends Employee {
    /**
     * @author:lixh
     */
    private int age;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Manager m1  = new Manager();
        System.out.println(m1.toString());
    }
    public String toString(){
        return super.toString()+"[age:"+age+"]";
    }
}

 

posted @ 2018-01-03 23:52  aLa神灯  阅读(112)  评论(0编辑  收藏  举报