1.UDP udp是一种不安全的协议 容易丢失数据 不需要建立连接 效率高 数据是以数据包的形式发送 数据 IP 端口
客户端:
public class Client {
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
//Socket 对象
while(true) {
byte[] b = "看海".getBytes();
DatagramSocket ds = new DatagramSocket();
InetAddress address = InetAddress.getLocalHost();
DatagramPacket pa = new DatagramPacket(b, b.length, address, 8090);
ds.send(pa);
ds.close();
}
}
}
服务器端:
public class Server {
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
DatagramSocket ds = new DatagramSocket(8090);
while(true) {
byte[] b = new byte[64*1024];
DatagramPacket p = new DatagramPacket(b, b.length);
//接收数据
ds.receive(p);
byte[] data = p.getData();
int l = p.getLength();
System.out.println(new String(data,0,l));
ds.close();
}
}
}
2.reflect
//获取User类的Class对象
//一 通过对象 getClass()f方法
User user = new User();
Class<? extends User> c1 = user.getClass();
//方式二 通过类
Class<User> c2 = User.class;
//方法三 通过字符串
Class<?> c3 = Class.forName("day14.reflect.User");
System.out.println(c3.getName());
//获取所有方法
Method[] methods = c1.getMethods();
//获取本类中的方法
Method[] methods2 = c1.getDeclaredMethods();
//获取公共的属性
Field[] fields = c.getFields();
//获取所有属性
Field[] fields2 = c.getDeclaredFields();
//获取所有构造函数
Constructor<?>[] ct = c4.getConstructors();
//获取某一个构造函数,并且创建对象
Constructor<?> con = c4.getConstructor(int.class,String.class); //获取某一个构造函数
Object user = con.newInstance(12,"haha");
//强制修改私有变量
Field f2 = c.getDeclaredField("name");
f2.setAccessible(true);
f2.set(user, "刘惜君");
//某个具体的方法
Method m1 = c.getDeclaredMethod("run", String.class);
m1.setAccessible(true);
m1.invoke(user, "刘惜君我爱你");
3.增强父类方法的三种方式 (1)重写方法
public void add() {
System.out.println("开始购买。。。");
super.add();
}
(2)静态代理(在子类增加一个构造函数)
public class MyProxy extends UserServiceImpl{
UserService impl;
public MyProxy() {
}
//构造函数 传递父类
public MyProxy(UserService impl) {
this.impl=impl;
}
public void run() {
System.out.println("开始事务。。。");
impl.add();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
UserService us = new UserServiceImpl();
MyProxy m = new MyProxy(us);
m.run();
}
}
(3)动态代理
public class TestProxy {
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
* 第一个参数 类加载器 加载本项目中的所有类的的字节码
* 第二个参数 获取被代理的对象的所有方法
* 参数三 告诉他你的代理的业务,用来进行增强事务
*/
UserService object = (UserService) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), (Class<?>[]) UserServiceImpl.class.getGenericInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("开启事务");
Object invoke = method.invoke(new UserServiceImpl(), args);
System.out.println("结束事务");
return null;
}
});
object.sub();
}
}
4.配置文件 动态代理
Properties p =new Properties();
p.load(new FileInputStream("conf/bean.properties"));
String className = p.getProperty("classname");
Reader r = (Reader)Class.forName(className).newInstance();//创建实例对象
r.read();
配置文件(bean.properties):
classname=day14.test.ReaderFile