------- android培训、java培训、期待与您交流! ----------
package cn.itcast.day1;
public abstract class WeekDay1 {
private WeekDay1() {
}
public final static WeekDay1 SUN = new WeekDay1() {
public WeekDay1 nextDay() {
return MON;
}
};
public final static WeekDay1 MON = new WeekDay1() {
public WeekDay1 nextDay() {
return SUN;
}
};
public abstract WeekDay1 nextDay();
public String toString() {
if (this == SUN) {
return "SUN";
} else {
return "MON";
}
}
}
public enum WeekDay {
SUN,MON,TUE,WED,THI,FRI,SAT;
private WeekDay(){
}
private WeekDay(int day){}
}
public enum TrafficLamp {
RED(30) {
public TrafficLamp nextLamp() {
return GREEN;
}
},
GREEN(30) {
public TrafficLamp nextLamp() {
return YELLOW;
}
},
YELLOW(5) {
public TrafficLamp nextLamp() {
return RED;
}
};
public abstract TrafficLamp nextLamp();
private int time;
private TrafficLamp(int time) {
this.time = time;
}
}
isPrimitive() 是否属于基本类型,如果是那八种基本数据类型则返回true。
getConstructor(Class<?>... parameterTypes) 返回该类的构造方法。里面是参数列表。举例如下:
Constructor<String> c=String.class.getConstructor(StringBuffer.class);
package cn.itcast.day1;
import java.lang.reflect.*;
public class ReflectTest3 {
public static void main(String[] args) throws Exception {
String startingClassName = args[0];
Method methodMain = Class.forName(startingClassName).getMethod("main",
String[].class);
methodMain.invoke(null,
(Object) new String[] { "haha", "hehe", "xixi" });
}
}
class ReflectArguments {
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
}
}
在实际开发中还有另一种情况,就是要从配置文件中提取数据来参与到代码运算中来,先看一段代码,然后再来分析:
package cn.itcast.day1;
import java.util.*;
import java.io.*;
public class ReflectTest2 {
public static void main(String[] args) throws Exception {
InputStream ips = ReflectTest2.class
.getResourceAsStream("config.properties");
Properties props = new Properties();
props.load(ips);
ips.close();
String className = props.getProperty("className");
Collection collections = (Collection) Class.forName(className)
.newInstance();
ReflectPoint rp1 = new ReflectPoint(3, 5);
ReflectPoint rp2 = new ReflectPoint(3, 3);
ReflectPoint rp3 = new ReflectPoint(3, 8);
ReflectPoint rp4 = new ReflectPoint(3, 5);
collections.add(rp1);
collections.add(rp2);
collections.add(rp3);
collections.add(rp4);
collections.add(rp1);
System.out.println(collections.size());
}
}
public class ReflectPoint {
private int x;
public int y;
public String str1 = "ball";
public String str2 = "basketball";
public String str3 = "itcast";
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
public String toString() {
return str1 + "::" + str2 + "::" + str3;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ReflectPoint other = (ReflectPoint) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
需求是从配置文件中提取信息,是用哪种类型的Collection来存取对象,假设我的配置文件config.properties已经存放了一个键值对className=java.util.ArrayList,那么通过类加载器ReflectTest2.class.getResourceAsStream("config.properties")获得文件config.properties的文件流对象。注意,因为类加载器是寻找它本目录下的文件,所以如果只要把文件config.properties放在bin目录下的本包中的class文件一起,就可以用相对路径"config.properties"来表示,如果放在其他目录,要用绝对路径表示。然后用Properties类的对象来载入文件流,String className= props.getProperty("className") 这个语句可以获得className这个键的值,也就是字符串"java.util.ArrayList",所以再通过语句Collection collections=(Collection)Class.forName(className).newInstance() 就可以得到一个ArrayList的集合对象。由于集合可以添加相同元素,所以打印结果为5。