socket.shutdownoutput//socket的半关闭;
在输出流已经输出结束 帮助客户端跳出读取循环的方式;
--
反射
类加载的过程:
之后是显示赋值,构造器赋值等;(静态成员变量和静态代码块看书写顺序)
双亲委派模型: 类加载是一个懒加载,会先加载父类极其父类的类,最后加载自身;
新的jar包要进行add lib操作;
final static修饰的值,在编译的时赋值,在“准备”阶段的时候已赋值;准备阶段静态成员变量进行初始化
1.类加载器
1Bootstrap ClassLoader根类加载器
核心类加载(lang包等)
2 Extension ClassLoader 扩展类加载器
3 System(APP) Classloader 系统加载器
负责加载自己定义的java类
类加载器的双亲委派机制
类加载时机;
1.创建类的实例(new 对象)
2.访问类的静态变量(首次)
3.访问类的静态方法(首次)
4.反射(创建接口或对象)
5.加载某个子类的对象会触发父类的类加载
6.直接使用java.exe命令运行某个主类,也就是执行main方法;
java代码的三个阶段
1.阶段其实就是代码变成class文件;
2.阶段就是类加载器进行类加载的阶段;
3.就是运行代码了;
反射(重要)
获取运行时类信息的技术
Class类的特点:
获取字节码文件的三个方式:
1.Class.forName("全限定类名")
2.类名点Class;
3.对象名点getClass();
推荐使用第一种,第三种麻烦,第二种不能触发完整内加载
------------
Properties properties = new Properties(); properties.load(new InputStreamReader( new FileInputStream("config.properties"),"GBK")); String ip = properties.getProperty("ip"); String port = properties.getProperty("port");
#表示注释
getModifiers的调用对象是反射出的成员属性,或者方法
package _018dayClass; import sun.nio.cs.ext.GBK; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.*; import java.sql.Connection; import java.sql.DriverManager; import java.util.Hashtable; import java.util.List; import java.util.Properties; /** * @author looper * @ **/ public class _01Demo { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException { Class<?> personClazz = Class.forName("_018dayClass.Person"); //获取所有public的构造方法 Constructor<?>[] constructors = personClazz.getConstructors(); //获取所有的构造方法 Constructor<?>[] declaredConstructors = personClazz.getDeclaredConstructors(); //获取指定的public的构造方法,不带s Constructor<?> constructors1 = personClazz.getConstructor(String.class); //通过构造方法实例化对象 Object o = personClazz.getConstructor(String.class, int.class, String.class).newInstance("张三", 20, "男"); Constructor<?> deconstrutor = personClazz.getDeclaredConstructor(String.class, int.class); deconstrutor.setAccessible(true); Object o1 = deconstrutor.newInstance("破解", 15); //获取成员变量 Field[] fields = personClazz.getFields(); Field[] declaredFields = personClazz.getDeclaredFields(); Field nameFiled = personClazz.getField("name"); Field ageFiled = personClazz.getDeclaredField("age"); ageFiled.setAccessible(true); Constructor<?> declaredConstructor = personClazz.getDeclaredConstructor(); Object o2 = declaredConstructor.newInstance(); Person o21 = (Person) o2;//属性.set(对象,赋值) nameFiled.set(o21, "王五");//某个对象上的值 nameFiled.get(o21);//获取某个对象上的name //获取成员方法 Method[] methods = personClazz.getMethods(); Method[] declaredMethods = personClazz.getDeclaredMethods(); Method eat = personClazz.getMethod("eat"); eat.setAccessible(true); Object invoke = eat.invoke(o21); Method eat1 = personClazz.getDeclaredMethod("eat", String.class); eat1.setAccessible(true); //方法的形参,从这里传递 eat1.invoke(o21,"米饭"); //有默认无参构造的情况下,直接字节码对象来生成对象 Object o3 = personClazz.newInstance(); // System.out.println(personClazz.getSimpleName()); System.out.println(personClazz.getSuperclass()); System.out.println(personClazz.getName()); System.out.println(personClazz.getInterfaces()); //权限修饰符 Field[] declaredFields1 = personClazz.getDeclaredFields(); int modifiers = declaredFields1[0].getModifiers(); Class<?> type = declaredFields1[0].getType(); System.out.println(type.getSimpleName()); Class<?> returnType = eat1.getReturnType(); System.out.println(returnType); } } class Person { public String name; private int age; private String gender; private Person(String name, int age) { this.name = name; this.age = age; } public Person() { } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}'; } public Person(String gender) { this.gender = gender; } public Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public void eat() { System.out.println("吃了"); } private void eat(String food) { System.out.println("吃了" + food); } public static void eat1() { System.out.println("吃了" ); } }