Class c1=Date.class;
Class c2=new Date().getClass();
Class c3=Class.forName("java.util.Date");
System.out.println(c1==c2);
System.out.println(c1==c3);
public boolean isPrimitive()判定指定的 Class 對象是否表示一個(gè)基本類型。 有九種預(yù)定義的 Class 對象,表示八個(gè)基本類型和 void。這些類對象由 Java 虛擬機(jī)創(chuàng)建,與其表示的基本類型同名,即 boolean、byte、char、short、int、long、float 和 double。 這些對象僅能通過下列聲明為 public static final 的變量訪問,也是使此方法返回 true 的僅有的幾個(gè) Class 對象。
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
Class c1=Date.class;
Class c2=int.class;
Class c3=Integer.class;
Class c4=Integer.TYPE;
System.out.println(c1.isPrimitive());
System.out.println(c2.isPrimitive());
System.out.println(c3.isPrimitive());
System.out.println(c4.isPrimitive());
System.out.println(c2==c3);
System.out.println(c2==c4);
Class c5=int[].class;
System.out.println(c5.isPrimitive());
System.out.println(c5.isArray());
}
}
public class Test {
public static void main(String[] args) throws Exception{
Constructor constructor1=String.class.getConstructor(StringBuffer.class);//要是用類型
String str2=(String)constructor1.newInstance(new StringBuffer("abc"));//要使用之前類型相同的對象
System.out.println(str2);
String str2=(String)Class.forName("java.lang.String").newInstance();//使用默認(rèn)的構(gòu)造方法
}
}
2.2 成員變量的反射應(yīng)用
“人有身高這一屬性”與“我有身高這一屬性不同”,也與“我的身高是XXX”不同
public class Test {
public static void main(String[] args) throws Exception{
Person me=new Person(180,140);
Field height_field=Person.class.getField("height");
//height_field指的是取得了Person這個(gè)類所具有的一個(gè)屬性,即身高這樣一個(gè)屬性,并不是取得的某個(gè)人的實(shí)際身高
System.out.println(height_field.get(me));
Field weight_field=Person.class.getDeclaredField("weight");
weight_field.setAccessible(true);
System.out.println(weight_field.get(me));
}
}
class Person{
public int height;
private int weight;
public Person(int height, int weight) {
super();
this.height = height;
this.weight = weight;
}
}
修改某一對象中的成員變量舉例:
import java.lang.reflect.Field;
public class Test {
public static void main(String[] args) throws Exception{
ReflectPoint pt1=new ReflectPoint(3,5);
Field[] fields=ReflectPoint.class.getFields();
for (Field field : fields) {
if(field.getType()==String.class){
String oldValue=(String)field.get(pt1);
String newValue=oldValue.replace("b", "a");
field.set(pt1, newValue);
}
}
System.out.println(pt1);
}
}
class ReflectPoint{
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
private int x;
public int y;
public String str1="ball";
public String str2="basketball";
public String str3="itcast";
@Override
public String toString() {
return "ReflectPoint [x=" + x + ", y=" + y + ", str1=" + str1 + ", str2=" + str2 + ", str3=" + str3 + "]";
}
}
2.3 成員方法的反射
基本應(yīng)用 “人有跳的能力”與“我有跳的能力”不一樣
public class Test {
public static void main(String[] args) throws Exception{
Method methodCharAt=String.class.getMethod("charAt", int.class);//后面指的是傳入的參數(shù)
//同樣,這取得的是String類的這樣一個(gè)方法,是一種屬性,而不是某個(gè)對象的成員方法
System.out.println(methodCharAt.invoke("abcde", 1));
//someMethod.invoke(null,parameter)指的是調(diào)用的靜態(tài)方法
}
}
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) throws Exception{
// TestArguments.main(new String[]{"111","222","333"});
String startingClassName=args[0];
Method mainMethod=Class.forName(startingClassName).getMethod("main", String[].class);
mainMethod.invoke(null, (Object)new String[]{"111","222","333"});
/* 如果沒有類型轉(zhuǎn)換會(huì)出現(xiàn)problems
* Type String[] of the last argument to method invoke(Object, Object...)
* doesn"t exactly match the vararg parameter type. Cast to Object[] to confirm the non-varargs invocation,
* or pass individual arguments of type Object for a varargs invocation.
*/
}
}
class TestArguments{
public static void main(String[] args) {
for (String string : args) {
System.out.println(string);
}
}
}
public class Test {
public static void main(String[] args) throws Exception{
int[] a0=new int[3];
int[] a1=new int[3];
int[] a2=new int[4];
System.out.println(a0.getClass()==a1.getClass());
System.out.println(a1.getClass()==a2.getClass());
System.out.println(a1.getClass().getName());
}
}
2.5 數(shù)組的反射應(yīng)用 舉例
public class Test {
public static void main(String[] args) throws Exception{
printObject(new String[]{"a","b","c"});
printObject("xyz");
}
private static void printObject(Object obj) {
Class c=obj.getClass();
if(c.isArray()){
int len=Array.getLength(obj);
for(int i=0;i
import java.util.HashSet;
public class Test {
public static void main(String[] args) throws Exception{
HashSet set=new HashSet<>();
TestArguments t1=new TestArguments(3);
TestArguments t2=new TestArguments(3);
set.add(t1);
set.add(t2);
System.out.println(set.size());
t1.x=456;
set.remove(t1);
System.out.println(set.size());
}
}
class TestArguments{
int x;
public TestArguments(int x) {
super();
this.x = x;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TestArguments other = (TestArguments) obj;
if (x != other.x)
return false;
return true;
}
}
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class Test {
public static void main(String[] args) throws Exception {
ReflectPoint pt1 = new ReflectPoint(3, 5);
System.out.println(BeanUtils.getProperty(pt1, "x"));
System.out.println(BeanUtils.getProperty(pt1, "x").getClass().getName());
BeanUtils.setProperty(pt1, "x", "9");//以string的形式對javabean進(jìn)行操作
System.out.println(pt1.getX());
BeanUtils.setProperty(pt1, "birthday.time", 111);
System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));
PropertyUtils.setProperty(pt1, "x", 9);//以屬性本身的類型的形式對javabean進(jìn)行操作
System.out.println(PropertyUtils.getProperty(pt1, "x").getClass().getName());
}
}
其中ReflectPoint為
import java.util.Date;
public class ReflectPoint {
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
this.birthday = new Date();
}
private int x;
private int y;
private Date birthday;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
三、注解1.了解注解
1.1 @Deprecated
public class AnnotationTest {
public static void main(String[] args) {
sayHello();
}
@Deprecated
public static void sayHello(){
System.out.println("hello!SF.GG!");
}
}
1.2 @Override
@Override
public String toString() {
return "AnnotationTest []";
}
2.注解的定義與反射調(diào)用
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@MyAnnotation
public class AnnotationTest {
public static void main(String[] args) {
sayHello();
if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
MyAnnotation myannotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(myannotation);
}
}
@Deprecated
public static void sayHello(){
System.out.println("hello!SF.GG!");
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
@interface MyAnnotation{
}
import java.lang.reflect.Constructor;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) throws Exception {
ArrayList
import java.util.ArrayList;
public class Test {
public static void main(String[] args) throws Exception {
ArrayList collection2 = new ArrayList<>();
ArrayList collection3 = new ArrayList<>();
System.out.println(collection2.getClass() == collection3.getClass());
}
}
import java.util.ArrayList;
import java.util.Collection;
public class Test {
public static void main(String[] args) throws Exception {
ArrayList collection2 = new ArrayList<>();
ArrayList collection3 = new ArrayList<>();
System.out.println(collection3);
printCollection(collection3);//編譯器不通過,因?yàn)橹罢f過,泛型類型并不存在類型參數(shù)的繼承關(guān)系
}
public static void printCollection(Collection collection){
}
}
這就需要通配符了
import java.util.ArrayList;
import java.util.Collection;
public class Test {
public static void main(String[] args) throws Exception {
ArrayList collection2 = new ArrayList<>();
ArrayList collection3 = new ArrayList<>();
System.out.println(collection3);
printCollection(collection3);//編譯器不通過,因?yàn)橹罢f過,泛型類型并不存在類型參數(shù)的繼承關(guān)系
}
public static void printCollection(Collection> collection){
// collection.add(123);
// 會(huì)報(bào)錯(cuò),因?yàn)槭褂昧送ㄅ浞虼瞬荒苷{(diào)用與類型參數(shù)相關(guān)的方法
// 參數(shù)(int)不適用于Collection 類型的add(capture#1-of?)方法,
collection.size();//這就沒錯(cuò),因?yàn)閟ize方法與類型參數(shù)沒有關(guān)系
for (Object object : collection) {
System.out.println(object);
}
}
}
使用?通配符可以引用各種參數(shù)類型,其主要作用是引用,而不是寫入 通配符也有拓展功能
限定上邊界 ? extends Number要求傳入的必須是Number的子類
限定下邊界 ? super Integer要求傳入的必須是Integer的父類
3.自定義泛型
3.1 泛型方法
public class Test {
public static void main(String[] args) throws Exception {
//結(jié)果就是二者的交集
Number num=add(3,51.0);
Integer inte=add(3,51);
Object o=add(3,"123");
swap(new String[]{"aaa","bbb","ccc"},1,2);
// swap(new int[]{123,456,789},1,2);//泛型變量只能是引用對象,int[]已經(jīng)是一個(gè)基本類型的數(shù)組,它并不能完成自動(dòng)裝箱
}
private static T add(T x,T y){
return null;
}
private static void swap(T[] a,int i,int j){
T temp=a[i];
a[i]=a[j];
a[j]=temp;
}
private static void sayHello() throws T{
try{
}catch(Exception e){//必須明確是哪個(gè)異常,不能catch T
throw (T)e;
}
}
}
3.2 在類上定義泛型
就是為了保障類中的泛型能夠統(tǒng)一,為此可以在類上定義泛型
public class Test {
public static void main(String[] args) throws Exception {
GenericDao dao=new GenericDao<>();
dao.add("123");
}
}
class GenericDao{
public void add(T x){
}
public T getByID(int id){
return null;
}
public void delete(T obj){
}
}
import java.util.Date;
public class ClassLoaderAttachment extends Date {
/**
*
*/
private static final long serialVersionUID = -1118939564631068343L;
public String toString(){
return "hello,worldxx";
}
}
其實(shí)我覺得這塊講的我沒太聽明白,以后看thingking in java的時(shí)候再補(bǔ)上吧