java Class object

1. Class object

"Class object" represent the type information of java.

Class object is used to create all "regular" objects.

All class has a "Class object", JVM use class loader to load "Class object" to create class.

All "Class objects" belongs to "class Class".

 

2. class Class

"class Class" with definition "Class <?>" is a "class tmeplate".

"Class object" is the object of  "class Class" 

 

3. class Class's main method

forName
getClassLoader
getField
getMethod
isInstance

newInstance

 

4.How to get "Class object" of a class

a. ClassforName("classname");
b.Name.class
for example Class Dog
ClassforName("Dog");Dog.class;

 

5. judge the class of a object

isInstanceof

 

6.class object of Primitive type

boolelan.class=Boolean.TYPE
char.class=Character.TYPE
byte.class=Byte.TYPE
short.class=Short.TYPE
int.class=Short.TYPE
long.class=Long.TYPE
float.class=Float.TYPE
double.class=Double.TYPE
void.class=Void.TYPE 

 

7.other
a.get length of array
b.Class classes[]; classes.length;
c.public class, class name must be the same as filename;

 

TestCode:

 

  1. import java.lang.reflect.Constructor;  
  2.     class Shape{  
  3.     }  
  4.     class Triangle extends Shape{  
  5.     }  
  6. public class ClassTest {  
  7.     public Shape shape;  
  8.     public Triangle triangle;  
  9.     public ClassTest(){  
  10.     }  
  11.     public ClassTest(int i){  
  12.     }  
  13.     public static void main(String[] args) throws ClassNotFoundException,  
  14.           SecurityException, NoSuchMethodException, NoSuchFieldException{  
  15.     }  
  16. }  


1.getConstructors

 

 

  1. Constructor constructors[] = Class.forName("elfylin.test.ClassTest").getConstructors();  
  2. for (int i = 0; i<constructors.length; i++){  
  3.     System.out.println(constructors[i].toString());  
  4. }  

public elfylin.test.ClassTest()
public elfylin.test.ClassTest(int)

 

2.getConstructor

 

  1. Constructor constructor = Class.forName("elfylin.test.ClassTest").  
  2.          getConstructor(int.class);  
  3. System.out.println(constructor.toString());  

public elfylin.test.ClassTest(int)

 

3. getDeclaredFields

 

  1. Field fields[] =  Class.forName("elfylin.test.ClassTest").getDeclaredFields();  
  2. for (int i = 0; i<fields.length; i++){  
  3.     System.out.println(fields[i].toString());  
  4. }  

public elfylin.test.Shape elfylin.test.ClassTest.shap
public elfylin.test.Triangle elfylin.test.ClassTest.triangle

 

4.getDeclaredField

 

  1. Field field = Class.forName("elfylin.test.ClassTest").getDeclaredField("triangle");  
  2. System.out.println(field);  

public elfylin.test.Triangle elfylin.test.ClassTest.triangle

 

5.getDeclaredMethods

 

  1. Method methods[] =  Class.forName("elfylin.test.ClassTest").getDeclaredMethods();  
  2. for (int i = 0; i<methods.length; i++){  
  3.     System.out.println(methods[i].toString());  
  4. }  

public static void elfylin.test.ClassTest.main(java.lang.String[]) throws  java.lang.ClassNotFoundException,java.lang.SecurityException,java.lang.NoSuchMethodException,java.lang.NoSuchFieldException

6. getFields()

 

will also return field of it's parent

7.getMethods()

will also return method of it's parent.

posted @ 2012-04-14 22:52  cascais  阅读(320)  评论(0编辑  收藏  举报