学习java—第二十九天学习笔记

206 阅读4分钟

2019.8.14 NIIT第二十九天

单元测试

package com.igeek1;


import org.junit.Test;

/**
 * @author jerryHe
 * @create 2019-08-14 10:01
 * 一个程序只能有一个入口方法
 * 不方便我们进行测试
 * 正常的需求是:能够对不同的方法进行测试,而不用在main方法中去测试每个方法,方法前也不用写static修饰符
 * 单元测试--测试每个功能(方法)
 *
 */
public class TestPerson {
    public static void main(String[] args) {
        //m1();
        //m2();
        //m3();
    }
    //@Test相当于提供给我们一个main方法,执行被@Test标注的方法
    //需要引入junit单元测试架包,一般这个测试架包,编辑工具都自带
    //idea中,按住alt+enter键
    @Test
    public  void m1(){
        System.out.println("m1");
    }
    public static void m2(){
        System.out.println("m2");
    }
    public static void m3(){
        System.out.println("m3");
    }
}

  • @Test,用于修饰需要执行的方法
  • @Before,测试方法前执行的方法
  • @After,测试方法后执行的方法

反射

根据包名加类名来创建对象,使用对象中的属性和方法

类的加载

当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化。

  • 加载 就是指将class文件读入内存,并为之创建一个Class对象。 任何类被使用时系统都会建立一个Class对象
  • 连接 验证 是否有正确的内部结构,并和其他类协调一致 准备 负责为类的静态成员分配内存,并设置默认初始化值
  • 初始化 就是我们以前讲过的初始化步骤

反射private方法

  • 通过反射运行private方法流程
  • 1 获得实例
  • 2 执行方法
  • 2.1 获得方法
  • 2.2 强制设置运行访问私有访问(暴力)
  • 2.3 执行方法 代码
 @Test
    public void m2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class c = Class.forName("com.igeek1.Video");
        Object obj = c.newInstance();

        //Method[] methods = c.getMethods();  获取的是公有方法
        //System.out.println(Arrays.toString(methods));
        //Method[] getDeclaredMethods()

        Method[] methods = c.getDeclaredMethods();
        System.out.println(Arrays.toString(methods));

        //private void com.igeek1.Video.show()
        //获取私有方法

        Method method = c.getDeclaredMethod("show");
        //强制访问私有方法(暴力破解)
        method.setAccessible(true);
        method.invoke(obj);
    }

反射静态方法

  • 通过反射运行静态方法流程
  • 1 获得实例
  • 2 执行方法
  • 2.1 获得方法
  • 2.2 执行方法 代码
@Test
    public void m3() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class c = Class.forName("com.igeek1.Video");
        Object obj = c.newInstance();
        Method[] methods = c.getMethods();
        System.out.println(Arrays.toString(methods));
        //public static void com.igeek1.Video.getInfo()
        Method method = c.getMethod("getInfo");
        //如果方法是static修饰的,可以直接调用,参数obj为null即可
        method.invoke(null);
    }

反射public字段

反射public字段执行流程 1 获得实例对象 2 给字段赋值 3 获得字段值

  @Test
    public  void m2() throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        //获取实例对象
        Class c=Class.forName("com.igeek.Video");
        Object obj=c.newInstance();
        //获取所有公共属性
        Field[] fields=c.getFields();
        System.out.println(Arrays.toString(fields));
        //获取某个公共属性
        Field field=c.getField("tag");
        System.out.println(field.get(obj));
    }

反射private字段

反射private字段流程 1 获得实例 2 获得声明的字段(私有) 3 设置运行访问私有(暴力) 4 设置数据 5 获得数据

@Test
    public  void m3() throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        //获取实例对象
        Class c=Class.forName("com.igeek.Video");
        Object obj=c.newInstance();
        //获取私有属性
        Field[] fields=c.getDeclaredFields();
        System.out.println(Arrays.toString(fields));
        Field id=c.getDeclaredField("name");
        //暴力破解,允许代码访问私有字段

        id.setAccessible(true);
        id.set(obj,"有妖气");
        System.out.println(id.getName()+":"+id.get(obj));
    }

Properties的概述

  • Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
  • 属性文件:
  • 属性文件的后缀名一般是.properties结尾
  • 属性文件中的内容是一对一对的键值对key=value

特点: 1、Map接口的子类,map中的方法都可以用。 2、该集合没有泛型。键值都是字符串。 3、它是一个可以持久化的属性集。键值可以存储到集合中,也可以存储到持久化的设备(硬盘、U盘、光盘)上。键值的来源也可以是持久化的设备。 4、有和流技术相结合的方法。

常用方法

  • public Object setProperty(String key, String value)调用 Map 的方法 put。
  • public Set stringPropertyNames()返回此属性列表中的键集,
  • public String getProperty(String key)用指定的键在此属性列表中搜索属性

保存操作

@Test
    public void m1() throws IOException {
        Properties ps=new Properties();
        ps.load(new FileInputStream("my.properties"));
        /*key=value结构*/
        System.out.println(ps.getProperty("ip"));
        //老版的迭代器
        Enumeration<String> enumeration = (Enumeration<String>) ps.propertyNames();
        while(enumeration.hasMoreElements()){
            String key=enumeration.nextElement();
            String value=ps.getProperty(key);
            System.out.println(key+":"+value);
        }

        System.out.println("遍历属性文件信息的第二种方式");

        Set<String> set= (Set<String>) ps.stringPropertyNames();
        for (String s: set
             ) {
            System.out.println(s+":"+ps.getProperty(s));
        }
    }

    /*设置值*/
    @Test
    public void m3(){
        Properties ps=new Properties();
        ps.setProperty("name","马东梅");
        Set<String> set=ps.stringPropertyNames();
        for (String s:set
             ) {
            System.out.println(s+":"+ps.getProperty(s));
        }
    }
@Test
    public void m1() throws IOException {
        Properties ps=new Properties();
        ps.setProperty("name","徐宁");
        ps.setProperty("age","12");
        FileOutputStream fos=new FileOutputStream("save.properties");
        ps.store(fos,"savetest");
    }
    @Test
    public void m2() throws IOException {
        Properties ps=new Properties();
        FileInputStream fis=new FileInputStream("save.properties");
        ps.load(fis);
        Set<String> keys =ps.stringPropertyNames();
        for (String s:keys) {
            System.out.println(s+":"+ps.getProperty(s));
        }
    }