zoukankan      html  css  js  c++  java
  • 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+"]";
        }
    }
  • 相关阅读:
    JDK6的switch支持不是很好
    团队作业(2)
    团队作业(1)
    4月30日
    重构:改善既有代码的设计有感
    4月28日
    4月27日
    4月26日
    4月25日
    4月24日
  • 原文地址:https://www.cnblogs.com/lxh520/p/8186426.html
Copyright © 2011-2022 走看看