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:

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/





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值