一個對象可以引用多個實際類型的現象稱為多態。在運行時能夠自動地選擇調用的適當方法的現象稱為動態綁定。
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.由對象導出的一個整數值。
String getName()
returns the name of this class.
Class getSuperclass()
returns the superclass of this class as a
Class object.
ArrayList是一個采用參數類型的泛型類。
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, Method和Constructor分別描述類的域、方法和構造器。
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, Method和Constructor classes.
AccessibleObject是Field, Method, and Constructor類的超類。
java.lang.reflect.AccessibleObject
void setAccessible(boolean flag)
設置對象可訪問權限
boolean isAccessible()
返回對象可訪問權限
Object get(Object obj)
獲得域值
void set(Object obj, Object newValue)
設置域值