2013年8月10日星期六

getFileds () and getDeclaredFields () the difference between howelse

API 1.6 in such a description:

getFields
public Field[] getFields()
                  throws SecurityException返回一个包含某些 Field 对象的数组,这些对象反映此 Class 对象所表示的类或接口的所有可访问公共字段。返回数组中的元素没有排序,也没有任何特定的顺序。如果类或接口没有可访问的公共字段,或者表示一个数组类、一个基本类型或 void,则此方法返回长度为 0 的数组。
特别地,如果该 Class 对象表示一个类,则此方法返回该类及其所有超类的公共字段。如果该 Class 对象表示一个接口,则此方法返回该接口及其所有超接口的公共字段。
该方法不反映数组类的隐式长度字段。用户代码应使用 Array 类的方法来操作数组。

getDeclaredFields
public Field[] getDeclaredFields()
                          throws SecurityException返回 Field 对象的一个数组,这些对象反映此 Class 对象所表示的类或接口所声明的所有字段。包括公共、保护、默认(包)访问和私有字段,但不包括继承的字段。返回数组中的元素没有排序,也没有任何特定的顺序。如果该类或接口不声明任何字段,或者此 Class 对象表示一个基本类型、一个数组类或 void,则此方法返回一个长度为 0 的数组。


from the API point of view, getFields () and getDeclaredFields () only difference is that an accessible public fields and returns an array of non-specific sort, and the other is to visit all of the fields, and returns an array!

But! I was quite depressing scene met
I look at the following code:
public class AClone {
public static void main(String[] args)
{
String name ;
if(args.length>0){
name = args[0];
}
else
{
Scanner sa = new Scanner(System.in);
System.out.println("Enter class name (e.g. java.util.Date):");
name = sa.next() ;
}
try{
Class cl = Class.forName(name);
Class supercl = cl.getSuperclass();
String modifiers = Modifier.toString(cl.getModifiers());
if(modifiers.length() > 0 ){
System.out.println(modifiers + "");
}
System.out.println("class"+ name);
if( supercl != null && supercl != Object.class ) System.out.println("extends "+supercl.getName());
System.out.println("\n{\n");
printFields(cl);
System.out.println("}");
}catch(ClassNotFoundException err){err.printStackTrace();}
}

private static void printFields(Class cl) {
Field[] fields = cl.getClass().getFields();
for (Field field : fields) {
Class type = field.getType() ;
String name = field.getName() ;
System.out.println("   ");
String modifiers = Modifier.toString(cl.getModifiers());
if ( modifiers.length() > 0 ) {
System.out.print(modifiers + " ");
}
System.out.println(type.getName() + " " + name + " ");
}
}
}


results are as follows:
Input: java.lang.Double
Display: [do not display this kind of data] Double

------------ dividing line --------------
above code, if printFields (Class cl) This function uses the Field [] fields = cl.getDeclaredFields ();
There you can export all fields!


ask, this is why! ! !
------ Solution ---------------------------------------- ----
getFields (): all public (public) fields, including the parent class.

getDeclaredFields: including public, private and proteced, excluding parent class declaration fields.
------ Solution -------------------------------------- ------
LZ private methods in little change:
Field [] fields = cl.getFields ();

cl is already a Class object, and if we getClass, it is not a Double Class, but rather a class Class Class (Class has no public methods).
------ Solution ---------------------------------------- ----
Honestly, do not understand, but do a test:

TypeVariable[] types = List.class.getTypeParameters();
       
        for(TypeVariable temp:types)
        {
            System.out.println(temp);
        }


print is a generic List interface defined character, E
------ Solution ------------------------ --------------------

+ +
------ For reference only --------- ------------------------------


So, positive solution! ! !

But I'm curious, in the Class which has a getTypeParameterd () What is the effect?
I'm baffled ...
understand the API was not any explanation ......
------ For reference only -------------------- -------------------


thank you! CSDN brothers ...

------ For reference only ---------------------------------------

  
thank you! CSDN brothers ...   
    

for portraits ah? Could not recognize it. . .
------ For reference only ---------------------------------------

+ +  


FML! Successful ...

没有评论:

发表评论