authorization

master
bseayin 2019-09-26 21:35:21 +08:00
parent 38c36bd954
commit 65862d538b
27 changed files with 766 additions and 0 deletions

View File

@ -11,5 +11,8 @@
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.8" level="project" />
<orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.55" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.5" level="project" />
</component> </component>
</module> </module>

View File

@ -11,5 +11,25 @@
<artifactId>corejava</artifactId> <artifactId>corejava</artifactId>
<dependencies>
<!--lombok,简化实体类,mybatis plus和它很搭配-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.55</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>
</project> </project>

View File

@ -0,0 +1,16 @@
package com.zz.classdemo;
import lombok.Data;
import java.util.Set;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/25$ 21:44$
*/
@Data
public class ClassDTO {
private String name;
private String students;
}

View File

@ -0,0 +1,18 @@
package com.zz.classdemo;
import lombok.Data;
import java.util.Set;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/25$ 21:42$
*/
@Data
public class ClassMaster {
private String name;
private Set<Student> students;
}

View File

@ -0,0 +1,38 @@
package com.zz.classdemo;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/25$ 21:28$
*/
public class Student {
private String name;
private Integer age;
public Student() {
}
public Student(String name,Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}

View File

@ -0,0 +1,148 @@
package com.zz.classdemo;
import com.zz.thread.Count;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/25$ 21:21$
*/
public class Test {
public static void main(String[] args) {
// Count count=new Count();
// Class c=count.getClass();
// //利用反射 获取一个类的所有属性
// Field[] fs=c.getDeclaredFields();
// for(Field field:fs){
// System.out.println(field.getName());
// }
try {
testConstructor();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testMethod() throws Exception {
Class<Student> aClass = (Class<Student>) Class.forName("com.zz.classdemo.Student");
//1.获取方法
// 获取取clazz对应类中的所有方法--方法数组(一)
// 不能获取private方法,并且获取从父类继承来的所有方法
Method[] methods = aClass.getMethods();
for (Method method:methods) {
System.out.println(method);
}
System.out.println("================================");
//2.获取方法
// 获取取clazz对应类中的所有方法--方法数组(一)
// 不能获取private方法,不获取从父类继承来的所有方法
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method method:declaredMethods) {
System.out.println(method);
}
System.out.println("=================================");
// 1.3.获取指定的方法
// 需要参数名称和参数列表,无参则不需要写
// 对于方法public void setName(String name) { }
Method method = aClass.getDeclaredMethod("setName", String.class);
System.out.println(method);
// 而对于方法public void setAge(int age) { }
method = aClass.getDeclaredMethod("setAge", Integer.class);
System.out.println(method);
// 这样写是获取不到的如果方法的参数类型是int型
// 如果方法用于反射那么要么int类型写成Integer public void setAge(Integer age) { }
// 要么获取方法的参数写成int.class
//2.执行方法
// invoke第一个参数表示执行哪个对象的方法剩下的参数是执行方法时需要传入的参数
Object obje = aClass.newInstance();
method.invoke(obje,2);
//如果一个方法是私有方法,第三步是可以获取到的,但是这一步却不能执行
//私有方法的执行必须在调用invoke之前加上一句method.setAccessibletrue;
}
/**
*
*
*
*
* @param obj: .
* @param methodName: . .
* @param args:
* @return:
*
*/
public Object invoke(Object obj, String methodName, Object ... args) throws Exception{
//1. 获取 Method 对象
// 因为getMethod的参数为Class列表类型所以要把参数args转化为对应的Class类型。
Class [] parameterTypes = new Class[args.length];
for(int i = 0; i < args.length; i++){
parameterTypes[i] = args[i].getClass();
System.out.println(parameterTypes[i]);
}
Method method = obj.getClass().getDeclaredMethod(methodName, parameterTypes);
//如果使用getDeclaredMethod就不能获取父类方法如果使用getMethod就不能获取私有方法
//
//2. 执行 Method 方法
//3. 返回方法的返回值
return method.invoke(obj, args);
}
public static void testField() throws Exception {
Class<Student> aClass = (Class<Student>) Class.forName("com.zz.classdemo.Student");
//1.获取字段
// 1.1 获取所有字段 -- 字段数组
// 可以获取公用和私有的所有字段,但不能获取父类字段
Field[] declaredFields = aClass.getDeclaredFields();
for (Field field:declaredFields) {
System.out.println(field);
}
System.out.println("=============================");
// 1.2获取指定字段
Field field = aClass.getDeclaredField("name");
System.out.println(field.getName());
System.out.println("==============================");
Student student = new Student();
//如果字段是私有的不管是读值还是写值都必须先调用setAccessibletrue方法
field.setAccessible(true);
student.setAge(1);
student.setName("张三");
//2.使用字段
// 2.1获取指定对象的指定字段的值
Object o = field.get(student);
System.out.println(o);
System.out.println("==========================");
// 2.2设置指定对象的指定对象Field值
field.set(student, "DEF");
System.out.println(student.getName());
}
public static void testConstructor() throws Exception{
Class<Student> aClass = (Class<Student>) Class.forName("com.zz.classdemo.Student");
//1. 获取 Constructor 对象
// 1.1 获取全部
Constructor<?>[] constructors = aClass.getConstructors();
for (Constructor constructor:constructors) {
System.out.println(constructor);
}
System.out.println("============================");
// 1.2获取某一个,需要参数列表
Constructor<Student> constructor = aClass.getConstructor(String.class, Integer.class);
System.out.println(constructor);
System.out.println("============================");
//2. 调用构造器的 newInstance() 方法创建对象
Object obj = constructor.newInstance("zhagn", 1);
}
}

View File

@ -0,0 +1,17 @@
package com.zz.classdemo;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/25$ 21:45$
*/
public class Test2 {
public static void main(String[] args) {
ClassDTO dto=new ClassDTO();
dto.setName("java10班");
dto.setStudents("[{name:\"张三\",age:21},{name:\"李四\",age:81}]");
}
}

View File

@ -0,0 +1,22 @@
package com.zz.proxy;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/23$ 21:11$
*/
// 委托类
public class HelloService implements IHelloService {
@Override
public String sayHello(String userName) {
System.out.println(userName + " hello");
return userName + " hello";
}
@Override
public String sayByeBye(String userName) {
System.out.println(userName + " ByeBye");
return userName + " ByeBye";
}
}

View File

@ -0,0 +1,25 @@
package com.zz.proxy;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/23$ 21:10$
*/
// 委托类接口
public interface IHelloService {
/**
* 1
* @param userName
* @return
*/
String sayHello(String userName);
/**
* 2
* @param userName
* @return
*/
String sayByeBye(String userName);
}

View File

@ -0,0 +1,64 @@
package com.zz.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/23$ 21:11$
*/
// 中间类
public class JavaProxyInvocationHandler implements InvocationHandler {
/**
* ,
*/
private Object obj ;
/**
* ,
* @param obj
*/
public JavaProxyInvocationHandler(Object obj){
this.obj = obj;
}
/**
* ,Proxy.newProxyInstance
* @return
*/
public Object newProxyInstance() {
return Proxy.newProxyInstance(
//指定代理对象的类加载器
obj.getClass().getClassLoader(),
//代理对象需要实现的接口,可以同时指定多个接口
obj.getClass().getInterfaces(),
//方法调用的实际处理者,代理对象的方法调用都会转发到这里
this);
}
/**
*
* @param proxy
* @param method
* @param args
* @return
* @throws Throwable
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("invoke before");
Object result = method.invoke(obj, args);
System.out.println("invoke after");
return result;
}
// @Override
// public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// return null;
// }
}

View File

@ -0,0 +1,18 @@
package com.zz.proxy;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/23$ 21:12$
*/
// 测试动态代理类
public class MainJavaProxy {
public static void main(String[] args) {
JavaProxyInvocationHandler proxyInvocationHandler = new JavaProxyInvocationHandler(new HelloService());
IHelloService helloService = (IHelloService) proxyInvocationHandler.newProxyInstance();
helloService.sayByeBye("allen");
helloService.sayHello("jerry");
}
}

View File

@ -0,0 +1,18 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/21$ 19:14$
*/
public class A implements Runnable {
Count count;
public A(Count count99){
this.count=count99;
}
@Override
public void run() {
count.t1();
}
}

View File

@ -0,0 +1,25 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/21$ 19:14$
*/
public class B implements Runnable {
Count count;
public B(Count count55){
this.count=count55;
}
@Override
public void run() {
try {
Thread.sleep(5000);
this.count.t2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,49 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/21$ 16:52$
*/
public class Count {
public int balance=1000;
public String name;
public int getBalance(){
return this.balance;
}
public void reduce(int num) {
synchronized(this) {
if(num<=this.balance){
System.out.println("成功取出:" + num);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.balance = this.balance - num;
}
}
}
public synchronized void t1(){
System.out.println("进入t1方法");
try {
wait();
System.out.println("t1方法被叫醒");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void t2(){
notifyAll();
}
}

View File

@ -0,0 +1,17 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/20$ 21:16$
*/
public class Demo1 implements Runnable{
@Override
public void run() {
// System.out.println("run方法里面就是一个线程需要执行的任务");
for(int i=0;i<2000;i++){
System.out.println("**********demo1运行**********");
}
}
}

View File

@ -0,0 +1,17 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/20$ 21:16$
*/
public class Demo2 implements Runnable{
@Override
public void run() {
// System.out.println("run方法里面就是一个线程需要执行的任务");
for(int i=0;i<2000;i++){
System.out.println("@@@@@@@@@@@@demo2运行**********");
}
}
}

View File

@ -0,0 +1,22 @@
package com.zz.thread;
import java.util.Date;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/20$ 21:40$
*/
public class Demo3 implements Runnable{
@Override
public void run() {
try {
System.out.println("开始"+new Date());
//让当前线程睡3秒后执行
Thread.sleep(3000);
System.out.println("结束"+new Date());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,21 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/20$ 21:16$
*/
public class Demo4 implements Runnable{
Task task;
public Demo4(Task task){
this.task=task;
}
@Override
public void run() {
// System.out.println("run方法里面就是一个线程需要执行的任务");
for(int i=0;i<300;i++){
this.task.finishtask();
}
}
}

View File

@ -0,0 +1,21 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/20$ 21:16$
*/
public class Demo5 implements Runnable{
Task task;
public Demo5(Task task){
this.task=task;
}
@Override
public void run() {
// System.out.println("run方法里面就是一个线程需要执行的任务");
for(int i=0;i<300;i++){
this.task.finishtask();
}
}
}

View File

@ -0,0 +1,14 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/21$ 16:04$
*/
public class Demo6 extends Thread {
public void run(){
System.out.println("线程demo6 运行");
}
}

View File

@ -0,0 +1,19 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/21$ 16:55$
*/
public class T1 extends Thread {
Count count;
T1(Count c){
this.count=c;
}
public void run(){
count.reduce(1000);
System.out.println(count.getBalance());
}
}

View File

@ -0,0 +1,19 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/20$ 21:50$
*/
public class Task {
int total=600;
int runtime=0;
public void finishtask(){
//相当于this.total=this.total-1
this.total--;
this.runtime++;
System.out.println("total="+total+"****runtime="+runtime);
}
}

View File

@ -0,0 +1,33 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/20$ 21:21$
*/
public class Test {
public static void main(String[] args) {
// Demo1 demo1=new Demo1();
// Thread t1=new Thread(demo1);
// //启动一个线程是执行start方法
// t1.start();
//
// Demo2 demo2=new Demo2();
// Thread t2=new Thread(demo2);
// t2.start();
Task task=new Task();
Demo4 demo4=new Demo4(task);
Thread t2=new Thread(demo4);
t2.start();
Demo5 demo5=new Demo5(task);
Thread t3=new Thread(demo5);
t3.start();
System.out.println("执行次数"+task.runtime);
System.out.println("任务总数"+task.total);
}
}

View File

@ -0,0 +1,14 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/21$ 16:07$
*/
public class Test2 {
public static void main(String[] args) {
Demo6 demo6=new Demo6();
demo6.start();
}
}

View File

@ -0,0 +1,18 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/21$ 16:58$
*/
public class Test3 {
public static void main(String[] args) {
Count count=new Count();
T1 t1=new T1(count);
T1 t2=new T1(count);
t1.start();
t2.start();
}
}

View File

@ -0,0 +1,21 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/21$ 19:21$
*/
public class Test4 {
public static void main(String[] args) {
Count count2=new Count();
A a1=new A(count2);
B b=new B(count2);
Thread t1=new Thread(a1);
Thread t2=new Thread(b);
t1.start();
t2.start();
}
}

View File

@ -0,0 +1,49 @@
package com.zz.thread;
/**
* @Description: java
* @Author: Bsea
* @CreateDate: 2019/9/21$ 20:35$
*/
public class Test5 {
public static void main(String[] args) {
Test5 test5=new Test5();
Thread t1=new Thread(
new Runnable(){
@Override
public void run() {
test5.t1();
}
}
);
Thread t2=new Thread(
new Runnable(){
@Override
public void run() {
test5.t2();
}
}
);
t1.start();
t2.start();
};
public synchronized void t1(){
try {
System.out.println("开始等待");
wait();
System.out.println("被叫醒");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void t2(){
System.out.println("开始叫醒");
notify();
}
}