java core technology to read 10 (c) - Inheritance (toString method overrides)

Reflector
can be called capacity-based parser reflected
java runs, added a run category for each object class, actually a type. java virtual machine can handle it, get this class by getclass, object.class and Class.forName ( "xxx") way. Second, the object may be instantiated by newInstance () manner.
try catch catch the exception.

May utilize a reflective type outputs all category includes information (fields, constructors, methods, etc.), the public can be obtained by get, if you want to view the private domain or a proprietary method can be utilized setAccessible method, the field is set to be accessible.

ToString override method using reflection, of any object to view internal information, and monitor and modify
jdk1.8 modify object reflector above a warning

public String toString(Object obj){
        if(obj == null) return "null";

        if(visited.contains(obj)) return "...";
        else visited.add(obj);

        Class cl = obj.getClass();
        if(cl == String.class) return (String) obj;
        if(cl.isArray()){
            String res = cl.getComponentType() + "[]{";
            for(int i = 0; i < Array.getLength(obj); i++)
            {
                if(i > 0) res += ",";
                Object val = Array.get(obj, i);
                if(cl.getComponentType().isPrimitive()) res += val;
                else res += toString(val);
            }
            return res + "}";
        }

        String res = cl.getName();
        //inspect the fields of this class and all superclasses
        do{
            res += "[";
            Field[] fields = cl.getDeclaredFields();
            AccessibleObject.setAccessible(fields, true);

            //get name and values
            for(Field ele : fields){
                //filter the static variable
                if(!Modifier.isStatic(ele.getModifiers()))
                {
                    if(!res.toString().endsWith("[")) res += ",";
                    res += ele.getName() + "=";

                    //identify the primitive and object
                    try
                    {
                        Class t = ele.getType();
                        Object val = ele.get(obj);
                        if(t.isPrimitive()) res += val;
                        else res += toString(val);
                    }
                    catch (IllegalAccessException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
            res += "]";
            cl = cl.getSuperclass();
        }
        while (cl != null);
        return res;
    }
    
**继承设计技巧**

 1. 公共操作和域放在超类中
 2. 不要使用proteced受保护域,一是因为子类的的访问破环了封装性,二是因为同一个包中所有的类都能方位受保护域。***protected在同一个包中和public相同,子类能继承,在不同类中子类和父类都能访问。而在不同包中,子类和父类都不能访问。**
 3. 利用is-a去判断是否使用继承
 4. 除非所有继承的方法都有意义, 否则不要使用继承
 5. 在覆盖方法时, 不要改变预期的行为
 6. **使用多态,而非类型判断。可以减少代码,并且让代码易于维护。**
 7. 不要过多使用反射,功能对于编写系统程序来说极其实用,但是通常不适于编写应用程序,很多错误是在运行中产生的。

Published 15 original articles · won praise 1 · views 134

Guess you like

Origin blog.csdn.net/qq_17236715/article/details/103757308