Java 笔记

注解

元注解–>meta-annotation
@Target-->注解的使用范围

@Retention-->注解的声明周期--source<class<runtime

@Documented-->注解包含在javadoc中 

@Inherited-->子类可以继承父类中的注解
自定义注解@interface
@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Inherited
@interface annotationDemo {

//注解的参数:参数类型+参数名() default 默认值;
//有默认值可以不写参数,无默认值参数必写
//参数名为value(),使用时参数名可省略
String name() default "";

int age() default 0;

//默认值-1代表不存在
int id() default -1;

String[] school();

}

反射

获取Class类的实例
-->类模板
//通过类名.class获取
Class<Test> testClass = Test.class;
//通过类对象.getClass()获取
Test test = new Test();
Class<? extends Test> aClass = test.getClass();
//通过Class.forName("类路径")获取
Class<?> aClass1 = Class.forName("bjsxt.Test");
//内置基本类型包装类可以通过包装类名.TYPE获得对应的类对象
Class<Integer> type = Integer.TYPE;
Class<Integer> integerClass = Integer.class;
//获得父类类对象
Class<? super Test> superclass = testClass.getSuperclass();

//类模板
Class<Object> objectClass = Object.class;
//接口模板
Class<Comparable> comparableClass = Comparable.class;
//一维数组模板
Class<String[]> aClass2 = String[].class;
//二维数组模板
Class<int[][]> aClass3 = int[][].class;
//注解模板
Class<Override> overrideClass = Override.class;
//枚举类模板
Class<ElementType> elementTypeClass = ElementType.class;
//包装类模板
Class<Integer> integerClass = Integer.class;
//void类模板
Class<Void> voidClass = void.class;
//Class类模板
Class<Class> classClass = Class.class;
运行时类的完整结构
-->获得类模板的信息
//类结构
-->FieldMethodConstructorSuperclassInterfaceAnnotation
//获取类对象-->Test为实体类
Class<Test> testClass = Test.class;
//获取包名+类名
System.out.println(testClass.getName());
//获取类名
System.out.println(testClass.getSimpleName());

-->获取Field
//获得成员变量集合-->public修饰的
Field[] fields = testClass.getFields();
for (Field field : fields) {
System.out.println(field);
}
//获得成员变量集合-->全部变量
Field[] declaredFields = testClass.getDeclaredFields();
for (Field field : declaredFields) {
System.out.println(field);
}
//获得指定成员变量-->public修饰的
Field id = testClass.getField("id");
System.out.println(id);
//获得指定成员变量-->任意变量
Field id = testClass.getDeclaredField("id");
System.out.println(id);

-->获取Method
//获得方法集合-->本类及其父类的全部public方法
Method[] methods = testClass.getMethods();
for (Method method : methods) {
System.out.println(method);
}
//获得方法集合-->本类全部方法
Method[] declaredMethods = testClass.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
System.out.println(declaredMethod);
}
//获得指定方法-->public修饰方法
Method setName = testClass.getMethod("setName", String.class);
System.out.println(setName);
//获得指定方法-->任意方法
Method siYou = testClass.getDeclaredMethod("siYou", String.class, int.class);
System.out.println(siYou);

-->获取Constructor
//获取构造方法集合-->public修饰
Constructor<?>[] constructors = testClass.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
//获取构造方法集合-->全部构造方法
Constructor<?>[] declaredConstructors = testClass.getDeclaredConstructors();
for (Constructor declaredConstructor : declaredConstructors) {
System.out.println(declaredConstructor);
}
//获取指定构造方法-->public修饰
Constructor<Test> constructor1 = testClass.getConstructor(null);
System.out.println(constructor1);
Constructor<Test> constructor2 = testClass.getConstructor(String.class, int.class);
System.out.println(constructor2);
//获取指定构造方法-->任意构造方法
Constructor<Test> declaredConstructor = testClass.getDeclaredConstructor(null);
System.out.println(declaredConstructor);
Constructor<Test> declaredConstructor1 = testClass.getDeclaredConstructor(String.class, int.class);
System.out.println(declaredConstructor1);
操作类结构
//创建实体类对象
-->获得类对象
Class<Test> testClass = Test.class;
-->-->通过类对象.newInstance()
Test test = testClass.newInstance();
-->-->获取构造器,通过指定构造器创建对象
Constructor<Test> constructor2 = testClass.getConstructor(String.class, int.class);
Test test = constructor2.newInstance("yang", 5);
System.out.println(test);
//通过反射操作方法
Class<Test> testClass = Test.class;
Test test = testClass.newInstance();
Method siYou = testClass.getDeclaredMethod("siYou", String.class, int.class);
siYou.invoke(test, "yang", 5);
//通过反射操作属性
Class<Test> testClass = Test.class;
Test test = testClass.newInstance();
Field name = testClass.getDeclaredField("name");
-->-->当属性为private修饰时,需设置安全检查开关为true
name.setAccessible(true);
name.set(test, "jing");
System.out.println(test.getName());
获得泛型类型
//获取方法参数的泛型
Class<Test> testClass = Test.class;
Method fanXing = testClass.getDeclaredMethod("fanXing", Map.class, List.class);
Type[] genericParameterTypes = fanXing.getGenericParameterTypes();
-->-->获取泛型参数的集合
for (Type genericParameterType : genericParameterTypes) {
System.out.println(genericParameterType);
if (genericParameterType instanceof ParameterizedType) {
-->-->获取参数泛型的集合
Type[] actualTypeArguments = ((ParameterizedType)genericParameterType).getActualTypeArguments();
	for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}
}

//获取返回值类型的泛型
Class<Test> testClass = Test.class;
Method fanXing = testClass.getDeclaredMethod("fanXing", Map.class, List.class);
-->-->获取返回值的泛型参数
Type genericReturnType = fanXing.getGenericReturnType();
System.out.println(genericReturnType);
if (genericReturnType instanceof ParameterizedType) {
-->-->获取泛型参数的泛型
Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}
反射操作注解
-->ORM-->对象关系映射
//获得类对象
Class<Test> testClass = Test.class;
//通过类对象获得类的注解集合-->全部注解
Annotation[] declaredAnnotations = testClass.getDeclaredAnnotations();
for (Annotation declaredAnnotation : declaredAnnotations) {
System.out.println(declaredAnnotation);
}
//通过类对象过得类的注解集合-->public修饰的注解
Annotation[] annotations = testClass.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//通过类对象获得类注解对象
annotationTable annotation = (annotationTable) testClass.getAnnotation(annotationTable.class);
//注解对象.属性名()获得注解属性值
String value = annotation.value();
System.out.println(value);
//通过属性对象获得指定属性注解
Field name = testClass.getDeclaredField("name");
annotationField annotation1 = name.getAnnotation(annotationField.class);
//通过属性对象.属性名()获得属性值
String s = annotation1.columnName();
System.out.println(s);

多线程

线程创建三种方式
-->-->继承Thread类实现多线程
public class TheardDemo extends Thread {

static int num = 1;
private String name;
static Object object = new Object();

public TheardDemo(String name) {
this.name = name;
}

public TheardDemo() {
}

@Override
public void run() {
while (num <= 100) {
   synchronized (object) {
       if (num <= 100) {
           System.out.println("卖了" + num + "张票");
           num++;
       } else {
           System.out.println("票已卖光!!");
       }
   }
}
}
}

class RunDemo{
public static void main(String[] args) throws ExecutionException, InterruptedException {
//继承Thread类实现多线程
TheardDemo theardDemo1 = new TheardDemo("兔子");
TheardDemo theardDemo2 = new TheardDemo("乌龟");
System.out.println(theardDemo1.getPriority());
System.out.println(theardDemo2.getPriority());
System.out.println(Thread.currentThread().getPriority());
theardDemo1.setPriority(10);
theardDemo2.setPriority(1);
System.out.println(theardDemo1.getPriority());
System.out.println(theardDemo2.getPriority());
System.out.println(Thread.currentThread().getPriority());

theardDemo1.start();
theardDemo2.start();
//System.out.println("主线程结束");

//Runnable接口开启多线程
RunDemo2 runDemo2 = new RunDemo2();
Thread thread1 = new Thread(runDemo2, "线程1");
Thread thread2 = new Thread(runDemo2, "线程2");
thread1.start();
thread2.start();

//Callable接口开启多线程
CallDemo callDemo = new CallDemo();
FutureTask future1 = new FutureTask<>(callDemo);
FutureTask future2 = new FutureTask<>(callDemo);
//查看线程是否取消,只能在Callable中使用
System.out.println(future1.isCancelled());
//设置线程是否取消,默认为false
future1.cancel(false);
Thread thread3 = new Thread(future1, "线程1");
Thread thread4 = new Thread(future2, "线程2");
//thread3.start();
//thread4.start();
//System.out.println(future1.get());
//System.out.println(future1.get());
//thread3.join();
System.out.println("线程联合join");

}
}

-->-->实现Runnable接口实现多线程
//可以避免单继承问题
//可以实现线程间数据共享
class RunDemo2 implements Runnable {
int i = 10;
@Override
public void run() {
for (int j = 0; j < 100; j++) {
   i += j;
   System.out.println(Thread.currentThread().getName() + "\t" + i);
}
}
}

-->-->实现Callable接口实现多线程
//可以有返回值
//可以抛出异常
class CallDemo implements Callable<Integer> {
int i = 10;
@Override
public Integer call() throws Exception {
for (int j = 0; j < 100; j++) {
   i += j;
   System.out.println(Thread.currentThread().getName() + "\t" + i);
}
return i;
}
}
锁–>线程同步
Synchronized锁

代码块-->无差别

普通方法-->this

静态方法-->类对象-->模板
Lock锁
//创建Lock锁-->使用ReentrantLock实现类-->普通锁
Lock lock = new ReentrantLock();
lock.lock();
lock.unlock();
//创建ReadWriteLock-->使用ReentrantReadWriteLock实现类-->读写锁
ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
//读锁-->共享锁
readWriteLock.readLock().lock();
readWriteLock.readLock().unlock();
//写锁-->独占锁
readWriteLock.writeLock().lock();
readWriteLock.writeLock().unlock();
//条件队列
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
condition.await();
condition.signal();
线程通信
public class ThreadTongXin {

public static void main(String[] args) {
  Product product = new Product();
  //开启生产者线程
  new Thread(new ShengChanZhe(product)).start();
  //开启消费者线程
  new Thread(new XiaoFeiZhe(product)).start();


}
}

-->资源类
class Product {
private String name;
private String color;
private boolean flag;

public Product(String name, String color) {
  this.name = name;
  this.color = color;
}

public Product() {
}

public String getName() {
  return name;
}

public void setName(String name) {
  this.name = name;
}

public String getColor() {
  return color;
}

public void setColor(String color) {
  this.color = color;
}

public boolean isFlag() {
  return flag;
}

public void setFlag(boolean flag) {
  this.flag = flag;
}
}

//生产者线程
class ShengChanZhe implements Runnable{

private int num = 0;
private Product product;

public ShengChanZhe(Product product) {
  this.product = product;
}

@Override
public void run() {
  while (true) {
      synchronized (product) {
          if (product.isFlag()) {
              try {
                  product.wait();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
          if (num % 2 == 0) {
              product.setName("馒头");
              try {
                  Thread.sleep(500);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              product.setColor("白色");
          } else {
              product.setName("玉米饼");
              try {
                  Thread.sleep(500);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              product.setColor("黄色");
          }
          num++;
          System.out.println("生产者生产了:" + product.getColor() + product.getName());
          product.setFlag(true);
          product.notify();
      }
  }
}
}

//消费者线程
class XiaoFeiZhe implements Runnable {
private Product product;

public XiaoFeiZhe(Product product) {
  this.product = product;
}

@Override
public void run() {

  while (true) {
      synchronized (product) {
          if (!product.isFlag()) {
              try {
                  product.wait();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
          System.out.println("消费者消费了:" + product.getColor() + product.getName());
          product.setFlag(false);
          product.notify();
      }
  }
}
}

JUC

泛型、枚举、反射、注解、Lambda表达式、链式编程、函数式接口、Stream流式计算、方法引用、单例模式、排序算法、生产者与消费者、死锁
四大函数式编程接口
-->@FunctionalInterface
 
1Consumer<T>-->消费型接口-->void accept(T t);

2Function<T, R>-->函数型接口-->R apply(T t);

3Predicate<T>-->断言型接口-->boolean test(T t);

4Supplier<T>-->供给型接口-->T get();
//匿名内部类写法
Function<String, Integer> function = new Function<>() {
@Override
public Integer apply(String o) {
    return 0;
}
};  
//Lambda表达式写法
Function<String, Integer> function1 = (str) -> {
return 0;
};
//调用重写方法
System.out.println(function.apply("aa"));
//Lamba表达式--链式编程--函数式接口--Stream流式计算联合使用
	   /**
         * 用户ID必须是偶数
         * 年龄必须大于23岁
         * 用户名转为大写字母
         * 用户名字母倒序排序
         * 只输出一个用户
         * */
     StreamDemo u1 = new StreamDemo(1, "yang", 20);
     StreamDemo u2 = new StreamDemo(2, "jing", 25);
     StreamDemo u3 = new StreamDemo(4, "da", 27);

     List<StreamDemo> streamDemos = Arrays.asList(u1, u2, u3);
     Stream<StreamDemo> stream = streamDemos.stream();
     stream.filter((u) -> { return u.getId() % 2 == 0; })
             .filter((u)->{return u.getAge() > 23;})
             .map((u)->{return u.getName().toUpperCase();})
             .sorted((uu,uuu)->{return uu.compareTo(uuu);})
             .limit(1)
             .forEach(System.out::println);
//输出结果-->DA
集合–>线程安全

List–>

Vector<Integer> vector = new Vector<>();
List<Integer> list = Collections.synchronizedList(new ArrayList<>());
List<Integer> cowList = new CopyOnWriteArrayList<>();

Set–>

Set<Integer> set = Collections.synchronizedSet(new HashSet<>());
Set<Integer> cowSet = new CopyOnWriteArraySet<>();

Map–>

Map<Integer, Integer> map = Collections.synchronizedMap(new HashMap<>());
Map<Integer, Integer> cctMap = new ConcurrentHashMap<>();
线程池
1、三大方法
//创建固定线程池
ExecutorService executorService = Executors.newFixedThreadPool(10);
//创建单一线程池
ExecutorService executorService1 = Executors.newSingleThreadExecutor();
//创建定时线程池
ExecutorService executorService2 = Executors.newCachedThreadPool();
//创建核心线程池
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
2、七个参数
//自定义线程池
ExecutorService ziDingYi = new ThreadPoolExecutor(
     2,  //核心数
     16,  //最大线程数
     3,  //线程存活时间
     TimeUnit.SECONDS,  //线程存活时间单位
     new LinkedBlockingQueue<>(3),  //阻塞队列
     Executors.defaultThreadFactory(),  //线程工厂
     new ThreadPoolExecutor.DiscardOldestPolicy());  //拒绝策略
3、四种策略
//运行线程数>最大线程数+阻塞队列线程数
new ThreadPoolExecutor.AbortPolicy()-->抛出异常
new ThreadPoolExecutor.CallerRunsPolicy()-->交由原线程处理
new ThreadPoolExecutor.DiscardPolicy()-->放弃执行此线程
new ThreadPoolExecutor.DiscardOldestPolicy()-->查看最先执行线程是否执行完毕,执行完毕执行此线程,未执行完毕,放弃执行此线程
4、最大线程数

CPU密集型–>int xianChengShu = Runtime.getRuntime().availableProcessors();

IO密集型–>IO线程数/总线程数=1/2

线程辅助类
CountDownLatch–>门闩类
CountDownLatch countDownLatch = new CountDownLatch(3);  //设置门闩数量
countDownLatch.countDown();  //门闩数量-1
countDownLatch.await();  //门闩数量为0,执行await代码
CyclicBarrier–>回环屏障
CyclicBarrier cyclicBarrier = new CyclicBarrier(5,()->{
         System.out.println("召唤神龙!!");
     });  //设置回环数量,数量达成,执行线程“召唤神龙”

cyclicBarrier.await();  //线程执行计数叠加到设置的回环数,执行await之后语句-->可用以上构造方法,舍弃此await方法
Semaphore–>计数信号量(停车位)
Semaphore semaphore = new Semaphore(4);  //设置计数量
semaphore.acquire();  //得到一个计数
semaphore.release();  //放弃一个计数
BlockingQueue四组API
ArrayBlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue(5);
LinkedBlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue(5);
方式抛出异常有返回值,不抛出异常阻塞等待超时退出
添加add()offer()put()offer()
移除remove()poll()take()poll()
查看队首元素element()peek()--
SynchronousQueue–>同步队列
SynchronousQueue<Integer> synchronousQueue = new SynchronousQueue();  //无容量,只能存取元素
synchronousQueue.offer(1);  //存放
synchronousQueue.poll();  //取出
ForkJoin–>分支合并框架
public class Test extends RecursiveTask<Long> {

private long start;
private long end;

private Long temp = 10000L;

public Test() {
}

public Test(long start, long end) {
  this.start = start;
  this.end = end;
}

public long getStart() {
  return start;
}

public void setStart(long start) {
  this.start = start;
}

public long getEnd() {
  return end;
}

public void setEnd(long end) {
  this.end = end;
}

public Long getTemp() {
  return temp;
}

public void setTemp(Long temp) {
  this.temp = temp;
}

@Override
protected Long compute() {
  if ((end - start) < temp) {
      long sum = 0L;
      for (long i = start; i <= end; i++) {
          sum += i;
      }
      return sum;
  } else {
      long mid = (start + end) / 2;
      Test test1 = new Test(start, mid);
      test1.fork();
      Test test2 = new Test(mid + 1, end);
      test2.fork();
      long sum = test1.join() + test2.join();
      return sum;
  }
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
  //ForkJoin
  long l = System.currentTimeMillis();
  ForkJoinPool forkJoinPool = new ForkJoinPool();
  ForkJoinTask<Long> test = new Test(0L, 10_0000_0000L);
  ForkJoinTask<Long> submit = forkJoinPool.submit(test);
  long l1 = System.currentTimeMillis();
  System.out.println(submit.get() + "  " + (l1 - l));

  //流式计算
  long l2 = System.currentTimeMillis();
  long reduce = LongStream.rangeClosed(0L, 10_0000_0000L).parallel().reduce(0, Long::sum);
  long l3 = System.currentTimeMillis();
  System.out.println(reduce + "  " + (l3 - l2));


}
}
//输出结果-->500000000500000000  6      500000000500000000  182
JMM–>内存模型

img

线程–>主存、工作内存

主内存对应的是Java堆中的对象实例部分,工作内存对应的是栈中的部分区域,从更底层的来说,主内存对应的是硬件的物理内存,工作内存对应的是寄存器和高速缓存。

JVM在设计时候考虑到,如果JAVA线程每次读取和写入变量都直接操作主内存,对性能影响比较大,所以每条线程拥有各自的工作内存,工作内存中的变量是主内存中的一份拷贝,线程对变量的读取和写入,直接在工作内存中操作,而不能直接去操作主内存中的变量。但是这样就会出现一个问题,当一个线程修改了自己工作内存中变量,对其他线程是不可见的,会导致线程不安全的问题。

内存交互操作八种:

lock (锁定):作用于主内存的变量,把一个变量标识为线程独占状态

unlock (解锁):作用于主内存的变量,它把一个处于锁定状态的变量释放出来,释放后的变量才可以被其他线程锁定

read (读取):作用于主内存变量,它把一个变量的值从主内存传输到线程的工作内存中,以便随后的load动作使用

load (载入):作用于工作内存的变量,它把read操作从主存中变量放入工作内存中

use (使用):作用于工作内存中的变量,它把工作内存中的变量传输给执行引擎,每当虚拟机遇到一个需要使用到变量的值, 就会使用到这个指令

assign (赋值):作用于工作内存中的变量,它把一个从执行引擎中接受到的值放入工作内存的变量副本中

store (存储):作用于主内存中的变量,它把一个从工作内存中一个变量的值传送到主内存中,以便后续的write使用

write  (写入):作用于主内存中的变量,它把store操作从工作内存中得到的变量的值放入主内存的变量中

JMM对这八种指令的使用,制定了如下规则:

不允许read和load、store和write操作之一单独出现。即使用了read必须load,使用了store必须write

不允许线程丢弃他最近的assign操作,即工作变量的数据改变了之后,必须告知主存

不允许一个线程将没有assign的数据从工作内存同步回主内存

一个新的变量必须在主内存中诞生,不允许工作内存直接使用一个未被初始化的变量。就是对变量实施use、store操作之前,必须经过assign和load操作

一个变量同一时间只有一个线程能对其进行lock。多次lock后,必须执行相同次数的unlock才能解锁

如果对一个变量进行lock操作,会清空所有工作内存中此变量的值,在执行引擎使用这个变量前,必须重新load或assign操作初始化变量的值

如果一个变量没有被lock,就不能对其进行unlock操作。也不能unlock一个被其他线程锁住的变量

对一个变量进行unlock操作之前,必须把此变量同步回主内存

Volatile

1、保证可见性

private volatile static int num = 0;

public static void main(String[] args) {

  new Thread(()->{
      while (num == 0) {

      }
  }).start();

  try {
      TimeUnit.SECONDS.sleep(1);
  } catch (InterruptedException e) {
      e.printStackTrace();
  }

  num = 1;
  System.out.println(num);

}

2、不保证原子性(不使用lock锁和synchronized锁)

//volatile不保证原子性
public class Test {

private volatile static int num = 0;

public static void main(String[] args) {

  for (int i = 0; i < 20; i++) {
      new Thread(()->{
          for (int j = 0; j <1000 ; j++) {
              num++;
          }
      }).start();
  }

  while (Thread.activeCount() > 2) {
      Thread.yield();
  }

  System.out.println(num);

}
}
//输出结果-->17296
//package java.util.concurrent.atomic;-->保证变量原子性
public class Test {

private volatile static AtomicInteger num = new AtomicInteger(0);

public static void main(String[] args) {

  for (int i = 0; i < 20; i++) {
      new Thread(()->{
          for (int j = 0; j <1000 ; j++) {
              num.getAndIncrement();
          }
      }).start();
  }

  while (Thread.activeCount() > 2) {
      Thread.yield();
  }

  System.out.println(num);

}
}
//输出结果-->20000

3、禁止指令重排序

设计模式

类之间的关系

继承关系

实现关系

依赖关系

关联关系

聚合关系

组合关系

面向对象设计原则

开闭原则

依赖倒置原则

单一职责原则

里氏替换原则

复合复用原则

迪米特法则

接口隔离原则

简单工厂模式

优势–>当需要多个某一类对象时,可交由工厂方法生成,至于工厂方法内部如何实现与用户无关,使用方便

劣势–>不符合开闭原则,对扩展开放,对修改并不满足要求

创建型设计模式
单例模式

要点–>

–>一个类只能有一个实例

–>此类必须自行创建这个实例

–>此类必须自行向整个系统提供此实例

懒汉式单例模式

构造函数私有化

声明对象并私有化,但不创建对象

公有方法内部判断创建对象

线程不安全,需加双重检测锁

饿汉式单例模式

构造函数私有化

内部创建实例并静态且私有化

提供外部获得此对象的静态方法

缺点–>类加载时单例实例就被创建,此对象可能闲置,浪费资源

工厂方法模式

工厂中只能产生一种对象

符合开闭原则–>扩展代码时直接新增工厂方法即可,不需修改源码

抽象工厂模式

工厂中可以产生多种对象

建造者模式

原型模式

浅克隆–>调用克隆的实例全部克隆,生成多份,实例中包含的引用数据类型一直只有一份

实现Cloneable接口–>未实现接口出现异常–>CloneNotSupportedException

重写Object类的clone()方法

克隆实例对象

深克隆

将非主克隆类实现Cloneable接口

重写Object类的clone()方法

将主克隆类实现Cloneable接口

重写Object类的clone()方法,方法内部不直接返回super.clone(),接着克隆非主克隆类,并赋值给主克隆对象中变量

返回重写方法中的主克隆类对象

结构型设计模式

适配器模式

桥接模式

组合模式

装饰模式

外观模式

service层–>业务层

亨元模式

代理模式

行为型设计模式

访问者模式

模板模式

策略模式

状态模式

观察者模式

备忘录模式

中介者模式

迭代器模式

解释器模式

命令模式

责任链模式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值