Java复习打卡day25

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");
        //say方法是私有的,需要使用getDeclaredMethod方法获取所有权限
        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 {

    /**
     * 姓名属性
     * @return
     */
    String name();

    /**
     * 年龄属性
     * @return
     */
    int age();

    /**
     * 信息属性
     * @return
     */
    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");
     /*   Annotation[] as = c.getAnnotations();
        for (Annotation a:as) {
            System.out.println(a);
        }*/
        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) {
            //获取属性的get和set方法
            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.迪米特法则
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值