Core Java学习笔记 反射

Posted on 2009-07-16 12:25 eric_xu 阅读(222) 评论(0)  编辑  收藏 所属分类: Java


一个对象可以引用多个实际类型的现象称为多态。在运行时能够自动地选择调用的适当方法的现象称为动态绑定。

Java中,只有基本类型的值不是对象。

The equals method, as implemented in the Object class, determines whether two object references are identical.两个对象是否具有相同的引用。

The getClass method returns the class of an object返回一个对象所属的类。

A hash code is an integer that is derived from an object.由对象导出的一个整数值。

 

java.lang.Class

String getName()

returns the name of this class.

Class getSuperclass()

returns the superclass of this class as a Class object.

 

ArrayList是一个采用参数类型的泛型类。

java.util.ArrayList<T>

ArrayList<T>()

constructs an empty array list.

 

ArrayList<T>(int initialCapacity)

constructs an empty array list with the specified capacity.

Parameters:

initialCapacity

数组列表的初始容量

 

boolean add(T obj)

appends an element at the end of the array list. Always returns true.

Parameters:

obj

the element to be added

 

int size()

returns the number of elements currently stored in the array list. 当前元素数量。

 

void ensureCapacity(int capacity)

ensures that the array list has the capacity to store the given number of elements without relocating its internal storage array.确保数组列表在不重新分配内部存储空间的情况下能够保持给定数量的元素。

Parameters:

capacity

the desired storage capacity

 

void trimToSize()

reduces the storage capacity of the array list to its current size.

将数组列表的容量削减为当前尺寸。

 

void set(int index, T obj)

puts a value in the array list at the specified index, overwriting the previous contents.

 

T get(int index)

gets the value stored at a specified index.

 

void add(int index, T obj)

shifts up elements to insert an element.

 

T remove(int index)

removes an element and shifts down all elements above it. The removed element is returned.

 

The wrapper classes are immutable—you cannot change a wrapped value after the wrapper has been constructed. They are also final, so you cannot subclass them.对象包装类是不可变的,不允许更改包装在其中的值。

ArrayList<Integer> list = new ArrayList<Integer>();

can analyze the capabilities of classes is called reflective.能够分析类的能力

1. Analyze the capabilities of classes at run time;

2. Inspect objects at run time, for example, to write a single toString method that works for all classes;

3. Implement generic array manipulation code; and

4. Take advantage of Method objects that work just like function pointers in languages such as C++.

 

While your program is running, the Java runtime system always maintains what is called runtime type identification on all objects. Java runtime system始终为所有的对象维护一个运行时类别标识。保存这些信息的类为Class

1.       Object类的getClass()方法返回一个Class类实例。

Employee e;
Class cl = e.getClass();

2.       Class的静态方法forName可获得对应的Class对象。

String className = "java.util.Date";
Class cl = Class.forName(className);

3.       T.class返回对应的Class对象。

Class cl1 = Date.class; // if you import java.util.*;
Class cl2 = int.class;
Class cl3 = Double[].class;
 
Object newInstance()

returns a new instance of this class.

 

java.lang.reflect包内的三个类Field, MethodConstructor分别描述类的域、方法和构造器。

Class类的getFields, getMethods, getConstructors方法返回类及超类的公有成员。

Field[] getFields()//返回该类及其超类的所有域

Field[] getDeclaredFields()//返回该类的所有域

同理Method[] getMethods()Method[] getDeclaredMethods()Constructor[] getConstructors()Constructor[] getDeclaredConstructors()

All three of these classes also have a method called getModifiers that returns an integer,描述public,static等修饰符。java.lang.reflect.Modifier类的isPublic, isPrivate, isFinal方法可以分析该整数。

The setAccessible method is a method of the AccessibleObject class, the common superclass of the Field, MethodConstructor classes.

AccessibleObjectField, Method, and Constructor类的超类。

java.lang.reflect.AccessibleObject

void setAccessible(boolean flag)

设置对象可访问权限

 

boolean isAccessible()

返回对象可访问权限

 

java.lang.reflect.Field 1.1

 

Object get(Object obj)

获得域值

 

void set(Object obj, Object newValue)

设置域值


只有注册用户登录后才能发表评论。


网站导航:
 

posts - 37, comments - 5, trackbacks - 0, articles - 0

Copyright © eric_xu