第七章
1、反射:
1)、用来实心动态代理;
2)、在运行过程中动态获取对象的类型;
3)、在运行过程中,动态加载对象;
4)、在运行过程中,动态的调用对象的方法;
5)、常常用来实现框架
2、Object是所有类型的父类,Java所有类型,包括自定义类型都默认继承Object
3、Class类型:反射技术的核心类,通过这个类,完成所有反射技术的实现,获取class对象的三种方法:类型.class:获取该类型的class对象
获取某个类型的class对象的方法:通过类型的对象.个体Class()
forName:Class类型的静态方法,通过字符串获取某一个的类型的class对象
package com.share.test_4_11;
public class Student extends Person{
private String name;
private int age;
public char sex ;
public static int i;
public Student(String name,int age,char sex){
this.name = name;
this.age = age;
this.sex = sex;
}
public Student(String name,String[] i){
this.name = name;
}
public Student(){
name ="sb111";
age = 11;
sex = '男';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "name:"+name+" age:"+age +" sex:"+sex;
}
}
package com.share.test_4_11;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Person {
public static void main(String[] args) {
try {
Class<Student> c = (Class<Student>) Class.forName("com.share.test_4_11.Student");
System.out.println(c.getName());
Student s = c.newInstance();//Student一定要包含一个无参的构造方法
System.out.println(s);
Constructor<?>[] c1 = c.getConstructors();//返回公共的构造方法
System.out.println("构造方法个数:"+c1.length);
for (int i = 0; i < c1.length; i++) {
System.out.println("---构造方法----");
System.out.println(c1[i].toString());
Class<?>[] c2 = c1[i].getParameterTypes();//获得这个构造方法的参数列表
System.out.println("c2:"+c2.length);
for (int j = 0; j < c2.length; j++) {
System.out.println(c2[j].getName());
}
}
Class<? super Student> superclass = c.getSuperclass();//返回当前类型的父类型
System.out.println(superclass.getName());
Class<?> superclass2 = superclass.getSuperclass();//如果没有父类,返回null
// System.out.println(superclass2.getName());
System.out.println("-------方法--------");
Method[] m = c.getDeclaredMethods();
for (int i = 0; i < m.length; i++) {
System.out.println(m[i].toString());
}
Method m1 = c.getMethod("setAge",int.class);
System.out.println(m1.getName());
m1.invoke(s, 100);//私有方法怎么执行???????????、setAccessible
System.out.println("-----属性-----");
Field[] f = c.getDeclaredFields();
for (int i = 0; i < f.length; i++) {
System.out.println(f[i].toString());
}
f[2].set(s, '女');//私有属性怎么修改???????????????
System.out.println(s);
System.out.println("属性值为:"+f[2].get(s));
f[1].setAccessible(true);//修改私有属性!!!
f[1].set(s, 250);
System.out.println(s);
System.out.println("属性值为:"+f[1].get(s));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Json解析
列1:
package com.share.test_4_11;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class HttpGetTest {
public static void main(String[] args) {
HttpGetTest get = new HttpGetTest();
try {
get.t();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String t() throws IOException{
URL url = new URL("http://172.18.6.3:8080/PunishmentVote/vote?result=1");
InputStream is = url.openStream();
InputStreamReader isr = new InputStreamReader(is,"UTF-8");
BufferedReader in = new BufferedReader(isr);
StringBuffer sb = new StringBuffer();
String inputLine;
while ((inputLine = in.readLine())!=null) {
sb.append(inputLine);
System.out.println(inputLine);
}
in.close();
return sb.toString();
}
}
package com.share.test_4_11;
public class Tangxin {
private String vote_result;
private String result;
public String getVote_result() {
return vote_result;
}
public void setVote_result(String vote_result) {
this.vote_result = vote_result;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
package com.share.test_4_11;
import java.io.IOException;
import com.google.gson.Gson;
public class JsonTest {
public static void main(String[] args) {
HttpGetTest t = new HttpGetTest();
String ht;
try {
ht = t.t();
Gson gson = new Gson();
Tangxin tx = gson.fromJson(ht, Tangxin.class);
System.out.println(tx.getResult());
System.out.println(tx.getVote_result());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
列2
package com.share.test_4_11;
import java.util.List;
import com.google.gson.Gson;
public class ZuoYe {
/*
* 字符串里的key一定要和类里面属性名称相对应
*/
public static void main(String[] args) {
String jsonstring = "{\"resultcode\":\"200\",\"reason\":\"successed!\",\"result\":{\"sk\":{\"temp\":\"36\",\"wind_direction\":\"西风\",\"wind_strength\":\"2级\",\"humidity\":\"50%\",\"time\":\"11:23\"},\"today\":{\"temperature\":\"30℃~38℃\",\"weather\":\"晴\",\"weather_id\":{\"fa\":\"00\",\"fb\":\"00\"},\"wind\":\"南风微风\",\"week\":\"星期六\",\"city\":\"苏州\",\"date_y\":\"2016年07月23日\",\"dressing_index\":\"炎热\",\"dressing_advice\":\"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。\",\"uv_index\":\"很强\",\"comfort_index\":\"\",\"wash_index\":\"较适宜\",\"travel_index\":\"较适宜\",\"exercise_index\":\"较适宜\",\"drying_index\":\"\"},\"future\":[{\"temperature\":\"30℃~38℃\",\"weather\":\"晴\",\"weather_id\":{\"fa\":\"00\",\"fb\":\"00\"},\"wind\":\"南风微风\",\"week\":\"星期六\",\"date\":\"20160723\"},{\"temperature\":\"30℃~38℃\",\"weather\":\"晴\",\"weather_id\":{\"fa\":\"00\",\"fb\":\"00\"},\"wind\":\"东南风微风\",\"week\":\"星期日\",\"date\":\"20160724\"},{\"temperature\":\"30℃~38℃\",\"weather\":\"晴\",\"weather_id\":{\"fa\":\"00\",\"fb\":\"00\"},\"wind\":\"东南风微风\",\"week\":\"星期一\",\"date\":\"20160725\"},{\"temperature\":\"30℃~39℃\",\"weather\":\"晴\",\"weather_id\":{\"fa\":\"00\",\"fb\":\"00\"},\"wind\":\"西南风微风\",\"week\":\"星期二\",\"date\":\"20160726\"},{\"temperature\":\"30℃~38℃\",\"weather\":\"晴\",\"weather_id\":{\"fa\":\"00\",\"fb\":\"00\"},\"wind\":\"西风微风\",\"week\":\"星期三\",\"date\":\"20160727\"},{\"temperature\":\"30℃~39℃\",\"weather\":\"晴\",\"weather_id\":{\"fa\":\"00\",\"fb\":\"00\"},\"wind\":\"西南风微风\",\"week\":\"星期四\",\"date\":\"20160728\"},{\"temperature\":\"30℃~38℃\",\"weather\":\"晴\",\"weather_id\":{\"fa\":\"00\",\"fb\":\"00\"},\"wind\":\"东南风微风\",\"week\":\"星期五\",\"date\":\"20160729\"}]},\"error_code\":0}";
System.out.println(jsonstring);
Gson g = new Gson();
JsonResult result = g.fromJson(jsonstring, JsonResult.class);//将字符串按照JsonResult作为模板解析,返回JsonResult对象
ResultTest r = result.getResult();//通过JsonResult获取结果集内容
List<JsonFuture> list = r.getFuture();//在结果集内容里面获得未来几天的天气
JsonFuture j = list.get(0);//获取第一天的天气情况
System.out.println("温度:"+j.getTemperature());
}
}
package com.share.test_4_11;
public class JsonResult {
private ResultTest result;
public ResultTest getResult() {
return result;
}
public void setResult(ResultTest result) {
this.result = result;
}
}
package com.share.test_4_11;
import java.util.List;
public class ResultTest {
private List<JsonFuture> future;
public List<JsonFuture> getFuture() {
return future;
}
public void setFuture(List<JsonFuture> future) {
this.future = future;
}
}
package com.share.test_4_11;
public class JsonFuture {
private String temperature;
private String weather;
private JsonWeather_id weather_id;
private String wind;
private String week;
private String date;
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public JsonWeather_id getWeather_id() {
return weather_id;
}
public void setWeather_id(JsonWeather_id weather_id) {
this.weather_id = weather_id;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
package com.share.test_4_11;
public class JsonWeather_id {
private String fa;
private String fb;
public String getFa() {
return fa;
}
public void setFa(String fa) {
this.fa = fa;
}
public String getFb() {
return fb;
}
public void setFb(String fb) {
this.fb = fb;
}
}