Skip to content

Commit 85377fb

Browse files
author
uodut
committed
添加codeStyle之后格式更改
添加日志包LogDemo,新增日志单例
1 parent 106de81 commit 85377fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+940
-1031
lines changed

.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="lib" path="lib/log4j-1.2.17.jar"/>
56
<classpathentry kind="output" path="bin"/>
67
</classpath>

src/AnnotationDemo/AnnotationClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626
@Target(ElementType.TYPE)
2727
@Retention(RetentionPolicy.RUNTIME)//如果不加这句的话,在使用反射的时候找不到此注解
2828
public @interface AnnotationClass {
29-
public String className() default "className";
29+
String className() default "className";
3030
}
3131

src/AnnotationDemo/AnnotationTest.java

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,69 @@
33
import java.lang.annotation.Annotation;
44
import java.lang.reflect.Constructor;
55
import java.lang.reflect.Method;
6-
6+
import org.apache.log4j.Logger;
77
import BasicDao.PersonAnnotation;
8+
import LogDemo.LogDemo;
89

910
/**
1011
* 2016年3月1日
11-
* @author <a href = "[email protected]">王高亮</a>
12-
* 得到注解内容
12+
*
13+
* @author <a href = "[email protected]">王高亮</a> 得到注解内容
1314
*/
1415
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-
}
16+
private static Logger logger = Logger.getLogger(AnnotationTest.class);
17+
public static void main(String[] args) {
18+
parseTypeAnnotation();
19+
parseMethodAnnotation();
20+
parseConstructorAnnotation();
21+
}
22+
23+
// 类,接口,枚举注解
24+
static void parseTypeAnnotation() {
25+
Class<?> clazz;
26+
try {
27+
clazz = Class.forName("BasicDao.PersonAnnotation");
28+
Annotation[] annotations = clazz.getAnnotations();
29+
for (Annotation annotaion : annotations) {
30+
AnnotationClass ac = (AnnotationClass) annotaion;
31+
//System.out.println(ac.className());
32+
logger.info(ac.className());
33+
}
34+
} catch (ClassNotFoundException e) {
35+
// TODO Auto-generated catch block
36+
e.printStackTrace();
37+
}
38+
}
39+
40+
// 成员方法注解
41+
static void parseMethodAnnotation() {
42+
Method[] methods = PersonAnnotation.class.getDeclaredMethods();
43+
//System.out.println(methods.length);
44+
for (Method method : methods) {
45+
boolean hasAnnotation = method.isAnnotationPresent(AnnotationConstructor.class);
46+
if (hasAnnotation) {
47+
AnnotationConstructor ac = method.getAnnotation(AnnotationConstructor.class);
48+
/*System.out.println("method = " + method.getName() + '\t' + "name = " + ac.name()
49+
+ '\t' + "age = " + ac.age());*/
50+
logger.info("method = " + method.getName() + '\t' + "name = " + ac.name()
51+
+ '\t' + "age = " + ac.age());
52+
}
53+
}
54+
}
55+
56+
// 构造函数注解
57+
static void parseConstructorAnnotation() {
58+
Class<?> clazz = new PersonAnnotation().getClass();
59+
Constructor<?>[] constructors = clazz.getConstructors();
60+
for (Constructor<?> constructor : constructors) {
61+
boolean hasAnnotation = constructor.isAnnotationPresent(AnnotationConstructor.class);
62+
if (hasAnnotation) {
63+
AnnotationConstructor ac = constructor.getAnnotation(AnnotationConstructor.class);
64+
/*System.out.println("Constuctor = " + constructor.getName() + '\t' + "name = "
65+
+ ac.name() + '\t' + "age = " + ac.age());*/
66+
logger.info("Constuctor = " + constructor.getName() + '\t' + "name = "
67+
+ ac.name() + '\t' + "age = " + ac.age());
68+
}
69+
}
70+
}
6271
}

src/BasicDao/ArgsDemo.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
package BasicDao;
2-
3-
import java.util.concurrent.ConcurrentHashMap;
4-
52
/**
63
* 可变参数
74
* 必须为参数的最后一个,且在一个方法中只能有一个可变参数。可以把可变参数当做数组处理。
85
* @author UODUT
96
*
107
*/
118
public class ArgsDemo {
12-
public static void main(String[] args) {
13-
System.out.println(add(2,3,4,5,6,7,8,9));
14-
}
15-
static int add(int a,int...args){
16-
int sum = a;
17-
for(int i = 0 ; i < args.length;i++){
18-
sum+=args[i];
19-
}
20-
return sum;
21-
}
9+
public static void main(String[] args) {
10+
//System.out.println(add(2,3,4,5,6,7,8,9));
11+
}
12+
13+
static int add(int a, int... args) {
14+
int sum = a;
15+
for (int i = 0; i < args.length; i++) {
16+
sum += args[i];
17+
}
18+
return sum;
19+
}
2220
}

src/BasicDao/PersonAnnotation.java

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
package BasicDao;
2-
32
import AnnotationDemo.AnnotationClass;
43
import AnnotationDemo.AnnotationConstructor;
5-
64
@AnnotationClass(className = "PersonAnnotation,类注解")
75
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-
6+
@AnnotationConstructor(name = "构造器注解", age = 23)
7+
public PersonAnnotation() {
8+
}
9+
private String name;
10+
private int age;
11+
private String otherInfo;
12+
public String getName() {
13+
return name;
14+
}
15+
@AnnotationConstructor(name = "成员方法注解")
16+
public void setName(String name) {
17+
this.name = name;
18+
}
19+
public int getAge() {
20+
return age;
21+
}
22+
@AnnotationConstructor(age = 13)
23+
public void setAge(int age) {
24+
this.age = age;
25+
}
26+
public String getOtherInfo() {
27+
return otherInfo;
28+
}
29+
public void setOtherInfo(String otherInfo) {
30+
this.otherInfo = otherInfo;
31+
}
3732
}

src/BasicDao/PersonHashSet.java

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,55 @@
11
package BasicDao;
2-
32
public class PersonHashSet {
4-
public PersonHashSet(){
5-
}
6-
public PersonHashSet(int age,String name){
7-
this.age = age;
8-
this.name = name;
9-
}
10-
private String name;
11-
private int age;
12-
public String getName() {
13-
return name;
14-
}
15-
public void setName(String name) {
16-
this.name = name;
17-
}
18-
public int getAge() {
19-
return age;
20-
}
21-
public void setAge(int age) {
22-
this.age = age;
23-
}
24-
@Override
25-
public int hashCode() {
26-
final int prime = 31;
27-
int result = 1;
28-
result = prime * result + age;
29-
result = prime * result + ((name == null) ? 0 : name.hashCode());
30-
return result;
31-
}
32-
@Override
33-
public boolean equals(Object obj) {
34-
if (this == obj)
35-
return true;
36-
if (obj == null)
37-
return false;
38-
if (getClass() != obj.getClass())
39-
return false;
40-
PersonHashSet other = (PersonHashSet) obj;
41-
if (age != other.age)
42-
return false;
43-
if (name == null) {
44-
if (other.name != null)
45-
return false;
46-
} else if (!name.equals(other.name))
47-
return false;
48-
return true;
49-
}
50-
3+
public PersonHashSet() {
4+
}
5+
public PersonHashSet(int age, String name) {
6+
this.age = age;
7+
this.name = name;
8+
}
9+
private String name;
10+
private int age;
11+
public String getName() {
12+
return name;
13+
}
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
public int getAge() {
18+
return age;
19+
}
20+
public void setAge(int age) {
21+
this.age = age;
22+
}
23+
@Override
24+
public int hashCode() {
25+
final int prime = 31;
26+
int result = 1;
27+
result = prime * result + age;
28+
result = prime * result + ((name == null) ? 0 : name.hashCode());
29+
return result;
30+
}
31+
@Override
32+
public boolean equals(Object obj) {
33+
if (this == obj) {
34+
return true;
35+
}
36+
if (obj == null) {
37+
return false;
38+
}
39+
if (getClass() != obj.getClass()) {
40+
return false;
41+
}
42+
PersonHashSet other = (PersonHashSet) obj;
43+
if (age != other.age) {
44+
return false;
45+
}
46+
if (name == null) {
47+
if (other.name != null) {
48+
return false;
49+
}
50+
} else if (!name.equals(other.name)) {
51+
return false;
52+
}
53+
return true;
54+
}
5155
}

0 commit comments

Comments
 (0)