现在的位置: 首页 > 综合 > 正文

java Class object

2013年03月23日 ⁄ 综合 ⁄ 共 2564字 ⁄ 字号 评论关闭

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:

import java.lang.reflect.Constructor;
    class Shape{
    }
    class Triangle extends Shape{
    }
public class ClassTest {
    public Shape shape;
    public Triangle triangle;
    public ClassTest(){
    }
    public ClassTest(int i){
    }
    public static void main(String[] args) throws ClassNotFoundException,
          SecurityException, NoSuchMethodException, NoSuchFieldException{
    }
}

1.getConstructors

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

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

2.getConstructor

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

public elfylin.test.ClassTest(int)

3. getDeclaredFields

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

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

4.getDeclaredField

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

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

5.getDeclaredMethods

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

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/

抱歉!评论已关闭.