Skip to content

Commit 799741c

Browse files
author
uodut
committed
ConcurrentHashMap实现原理和特点
注解的使用方式(注解的原理没有细看) 代理模式(静态) 动态代理
1 parent a4de7b6 commit 799741c

22 files changed

+381
-8
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package AnnotationDemo;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
/**
8+
* 元注解包括:
9+
*   1.@Target,
10+
    2.@Retention,
11+
    3.@Documented,
12+
    4.@Inherited
13+
* Target取值(ElementType)有:
14+
*   1.CONSTRUCTOR:用于描述构造器
15+
    2.FIELD:用于描述域
16+
    3.LOCAL_VARIABLE:用于描述局部变量
17+
    4.METHOD:用于描述方法
18+
    5.PACKAGE:用于描述包
19+
    6.PARAMETER:用于描述参数
20+
    7.TYPE:用于描述类、接口(包括注解类型) 或Enum声明
21+
Retention取值(RetentionPolicy)有:
22+
    1.SOURCE:在源文件中有效(即源文件保留)
23+
    2.CLASS:在class文件中有效(即class保留)
24+
    3.RUNTIME:在运行时有效(即运行时保留)
25+
*/
26+
@Target(ElementType.TYPE)
27+
@Retention(RetentionPolicy.RUNTIME)//如果不加这句的话,在使用反射的时候找不到此注解
28+
public @interface AnnotationClass {
29+
public String className() default "className";
30+
}
31+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package AnnotationDemo;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target({ElementType.CONSTRUCTOR,ElementType.METHOD})//¿ÉÒÔÓжà¸ö
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface AnnotationConstructor {
11+
String name() default("");
12+
int age() default(0);
13+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package AnnotationDemo;
2+
3+
import java.lang.annotation.Annotation;
4+
import java.lang.reflect.Constructor;
5+
import java.lang.reflect.Method;
6+
7+
import BasicDao.PersonAnnotation;
8+
9+
/**
10+
* 2016年3月1日
11+
* @author <a href = "[email protected]">王高亮</a>
12+
* 得到注解内容
13+
*/
14+
public class AnnotationTest {
15+
public static void main(String[] args){
16+
parseTypeAnnotation();
17+
parseMethodAnnotation();
18+
parseConstructorAnnotation();
19+
}
20+
//类,接口,枚举注解
21+
static void parseTypeAnnotation(){
22+
Class<?> clazz;
23+
try {
24+
clazz = Class.forName("BasicDao.PersonAnnotation");
25+
Annotation[] annotations = clazz.getAnnotations();
26+
for(Annotation annotaion:annotations){
27+
AnnotationClass ac = (AnnotationClass)annotaion;
28+
System.out.println(ac.className());
29+
}
30+
} catch (ClassNotFoundException e) {
31+
// TODO Auto-generated catch block
32+
e.printStackTrace();
33+
}
34+
}
35+
//成员方法注解
36+
static void parseMethodAnnotation(){
37+
Method[] methods = PersonAnnotation.class.getDeclaredMethods();
38+
System.out.println(methods.length);
39+
for(Method method:methods){
40+
boolean hasAnnotation = method.isAnnotationPresent(AnnotationConstructor.class);
41+
if(hasAnnotation){
42+
AnnotationConstructor ac = method.getAnnotation(AnnotationConstructor.class);
43+
System.out.println("method = " +method.getName() + '\t'
44+
+ "name = " + ac.name() + '\t' + "age = " + ac.age());
45+
}
46+
}
47+
}
48+
//构造函数注解
49+
static void parseConstructorAnnotation(){
50+
Class<?> clazz = new PersonAnnotation().getClass();
51+
Constructor<?>[] constructors = clazz.getConstructors();
52+
for(Constructor<?> constructor:constructors){
53+
boolean hasAnnotation = constructor.isAnnotationPresent(AnnotationConstructor.class);
54+
if(hasAnnotation){
55+
AnnotationConstructor ac = constructor.getAnnotation(AnnotationConstructor.class);
56+
System.out.println("Constuctor = " + constructor.getName()
57+
+ '\t' + "name = " + ac.name() + '\t' + "age = "
58+
+ ac.age());
59+
}
60+
}
61+
}
62+
}

src/BasicDao/ArgsDemo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package BasicDao;
2+
3+
import java.util.concurrent.ConcurrentHashMap;
4+
25
/**
36
* 可变参数
47
* 必须为参数的最后一个,且在一个方法中只能有一个可变参数。可以把可变参数当做数组处理。

src/BasicDao/PersonAnnotation.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package BasicDao;
2+
3+
import AnnotationDemo.AnnotationClass;
4+
import AnnotationDemo.AnnotationConstructor;
5+
6+
@AnnotationClass(className = "PersonAnnotation,类注解")
7+
public class PersonAnnotation {
8+
@AnnotationConstructor(name="构造器注解",age=23)
9+
public PersonAnnotation(){
10+
}
11+
12+
private String name;
13+
private int age;
14+
private String otherInfo;
15+
public String getName() {
16+
return name;
17+
}
18+
@AnnotationConstructor(name="成员方法注解")
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
public int getAge() {
23+
return age;
24+
}
25+
@AnnotationConstructor(age = 13)
26+
public void setAge(int age) {
27+
this.age = age;
28+
}
29+
public String getOtherInfo() {
30+
return otherInfo;
31+
}
32+
public void setOtherInfo(String otherInfo) {
33+
this.otherInfo = otherInfo;
34+
}
35+
36+
37+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* 内部类和外部类的成员变量和访问规则
55
*
66
*/
7-
public class 常规内部类 {
7+
public class PlainInnerClass {
88
private int age = 11;
99

1010
public class In{
@@ -13,7 +13,7 @@ public void print(){
1313
int age = 13;
1414
System.out.println("内部类的变量" + age);//默认访问外部类变量,但内部类有同名变量时覆盖外部类变量
1515
System.out.println("内部成员变量"+this.age);//访问内部成员变量
16-
System.out.println(常规内部类.this.age);//访问外部类的成员变量
16+
System.out.println(PlainInnerClass.this.age);//访问外部类的成员变量
1717
}
1818
private void print2(){
1919
System.out.println("外部类的私有变量" + age);
@@ -32,7 +32,7 @@ public static void main(String[] args) {
3232
3333
  2.必须先有外部类的对象才能生成内部类的对象,因为内部类的作用就是为了访问外部类中的成员变量
3434
*/
35-
常规内部类.In in = new 常规内部类().new In();//内部类的实例化方式
35+
PlainInnerClass.In in = new PlainInnerClass().new In();//内部类的实例化方式
3636
in.print();
3737
in.print2();//调用内部类的私有成员方法print2()
3838

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ClassDemo;
22

3-
public class 私有内部类 {
3+
public class PrivateInnerClass {
44
private int age = 11;
55
private class In{
66
public void print(){
@@ -12,7 +12,7 @@ public In getInstance(){
1212
}
1313
public static void main(String[] args) {
1414
//私有内部类.In in = new 私有内部类.new In();
15-
私有内部类 s = new 私有内部类();//不能支持操作内部类,需要在外部类中提供得到内部类对象的方法。
15+
PrivateInnerClass s = new PrivateInnerClass();//不能支持操作内部类,需要在外部类中提供得到内部类对象的方法。
1616
s.getInstance().print();
1717
}
1818
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ClassDemo;
22

3-
public class 静态内部类 {
3+
public class StaticInnerClass {
44
private int age = 10;
55
private static String name = "tudou";
66
static class In{
@@ -10,7 +10,7 @@ void print(){
1010
}
1111
}
1212
public static void main(String[] args) {
13-
静态内部类.In in = new 静态内部类. In();//In被当做是“静态内部类”的一个静态成员,所以可以直接调用,不用实例化
13+
StaticInnerClass.In in = new StaticInnerClass. In();//In被当做是“静态内部类”的一个静态成员,所以可以直接调用,不用实例化
1414
in.print();
1515
}
1616
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ClassDemo;
22

3-
public class 匿名内部类 {
3+
public class UnnameInnerClass {
44
public static void main(String[] args) {
55
InterfaceDemo id = new InterfaceDemo(){
66
public void func(){
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package DesignMode.DynamicProxy;
2+
3+
public class Bird implements Sound, Flyable {
4+
5+
@Override
6+
public void fly() {
7+
// TODO Auto-generated method stub
8+
System.out.println("Bird can fly");
9+
}
10+
11+
@Override
12+
public void sound() {
13+
// TODO Auto-generated method stub
14+
System.out.println("Bird can speak");
15+
}
16+
17+
}

0 commit comments

Comments
 (0)