Class与加载方式
public class Demo {
public static void main(String[] args) throws ClassNotFoundException {
Class c = Person.class;
System.out.println(c);
Person p = new Person();
Class<Person> c1 = (Class<Person>) p.getClass();
System.out.println(c1);
Class<Person> c2 = (Class<Person>) Class.forName("com.kaikeba.learn.Person");
System.out.println(c2);
System.out.println(c == c1 && c1 == c2);
}
public static Class getClass2(String className) throws ClassNotFoundException {
return Class.forName("className");
}
}
反射
反射
反射就是在动态运行代码的时候,调用类被调用它的方法和属性。
先创建一个Person对象
import java.util.Objects;
public class Person {
private String name;
private int age;
private int height;
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
height == person.height &&
Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age, height);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public Person(String name, int age, int height) {
this.name = name;
this.age = age;
this.height = height;
}
private static void say(){
System.out.println("锄禾日当午");
}
}
反射中的构造方法
import java.lang.reflect.Constructor;
public class Demo1 {
public static void main(String[] args) throws Exception {
Class c = Class.forName("com.kaikeba.learn.Person");
Constructor cs = c.getConstructor();
System.out.println(cs);
Constructor[] css = c.getConstructors();
for (Constructor cs1:css) {
System.out.println(cs1);
}
}
}
反射中的方法
public class Demo2 {
public static void main(String[] args) throws Exception {
Class c = Class.forName("com.kaikeba.learn.Person");
Method m = c.getDeclaredMethod("say");
System.out.println(m);
Method[] ms = c.getMethods();
for (Method m1:ms) {
System.out.println(m1);
}
}
}
反射中的属性
import java.lang.reflect.Field;
public class Demo3 {
public static void main(String[] args) throws Exception {
Class c = Class.forName("com.kaikeba.learn.Person");
Field f = c.getDeclaredField("name");
System.out.println(f);
Field[] fs = c.getDeclaredFields();
for (Field f1:fs) {
System.out.println(f1);
}
}
}
反射与注解
反射和注解
在定义注解的时候可能会传进一些参数,当初我们并额米有解释参数该如何获取和使用;
留到现在,等反射讲完了在解释会容易理解一点
TableAnnotation
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface TableAnnotation {
String value();
}
columnAnnotation
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.FIELD)
public @interface columnAnnotation {
String name();
int age();
String info();
}
Bean类
import java.io.Serializable;
import java.util.Objects;
@TableAnnotation(value = "table_hello")
public class Bean implements Serializable {
@columnAnnotation(name = "张三",age = 17, info ="学生")
private String name;
@columnAnnotation(name = "李四",age = 37, info ="老师")
private int age;
@columnAnnotation(name = "王二",age = 17, info ="警察")
private String info;
@Override
public String
toString() {
return "Bean{" +
"name='" + name + '\'' +
", age=" + age +
", info='" + info + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Bean bean = (Bean) o;
return age == bean.age &&
Objects.equals(name, bean.name) &&
Objects.equals(info, bean.info);
}
@Override
public int hashCode() {
return Objects.hash(name, age, info);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Bean() {
}
public Bean(String name, int age, String info) {
this.name = name;
this.age = age;
this.info = info;
}
}
Demo–测试类
import com.sun.xml.internal.ws.addressing.WsaActionUtil;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class Demo {
public static void main(String[] args) throws Exception {
Class<?> c = Class.forName("com.java.demo.Bean");
TableAnnotation ta = c.getAnnotation(TableAnnotation.class);
String v = ta.value();
System.out.println("表名:" + v);
Field[] fs = c.getDeclaredFields();
for(Field f:fs){
columnAnnotation ca = f.getAnnotation(columnAnnotation.class);
System.out.println("" + f.getName() + "属性的name是" + ca.name() + ",age是" + ca.age() + ",info是" + ca.info() + "。");
}
}
}
内省
内省introspector
有人觉得内省很难,其实并不难。以下是我对它的内部原理的解释
1.在内存中,introspector先通过getBeanInfo方法加载类,获得类的属性信息BeanInfo(这里会生成一个bi对象)。
2.然后通过bi对象调用getPropertyDescriptors方法得到PropertyDescriptors(这里会生成一个pds对象)。
3.最后通过pds对象调用getReadMethod和getWriteMethod分别获取get和set方法。
这就是内省,下面将用代码向大家展示。
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) throws IntrospectionException {
Class c = Express.class;
BeanInfo bi = Introspector.getBeanInfo(c);
PropertyDescriptor[] pds = bi.getPropertyDescriptors();
for (PropertyDescriptor pd:pds) {
Method get = pd.getReadMethod();
Method set = pd.getWriteMethod();
System.out.println(get);
System.out.println(set);
String name = pd.getName();
System.out.println(name);
Class path = pd.getPropertyType();
System.out.println(path);
}
}
}
设计模式导学
什么是设计模式
设计模式
什么是设计模式?
1990年,编程界才开始注意到设计模式的问题,并加以讨论,但是一直没有大的突破。
直到1995年,“四人组”发布了一本书,《设计模式:可复用面向对象软件的基础》,书中提出了23中设计模式,
这是设计模式里程碑型的时间,直到今日狭义的设计模式还是这23种设计模式。
概念:
它是前辈们编程经验的总结,用来解决一些在编程中时常会出现的问题,具有一定的普遍性,可以重复使用。其目的是
提高代码的复用性,可读性和可靠性。
其实简而言之就是模板。
设计模式的六大原则
六种设计原则:
1.开闭原则
2.单一职责原则
3.里氏替换原则
4.依赖倒置原则
5.接口隔离原则
6.迪米特法则