14.IO流

1. 覆盖完整版

方法名,参数列表相同, 返回类型可以变小, 权限访问修饰符可以变大, 异常可以变小。

class A {
	
	protected Object abc(Object o) throws IOException {
		return null;
	}
}
class B extends A {

	@Override
	public String abc(Object o) throws FileNotFoundException, NotSerializableException {
		return null;
	}
}

2 IO; inputstream: 输入流 outputStream:输出流

在这里插入图片描述

  • Read—输入
  • Write—输出

3. File类:

File类::: 代表 文件或者 目录

  • long length() : 只对文件有用。 多少byte
  • String getName(); 获取文件名
  • String getPath(); 文件路径
  • isFile() : 是否是文件
  • isDirectory() : 是否 是目录
  • exists(); 是否存在
  • getParentFile(); 获取父目录
  • boolean createNewFile() throws IOException: true创建成功,false文件已存在, IOException 创建失败没父。
    • ※保证父目录存在
  • boolean mkdir(); 创建目录
    • ※保证父目录存在
  • boolean mkdirs(); 创建目录,如果父目录不存在,自动父目录
  • delete(); 删除文件
    • ※ 目录下有内容是删除不了的,返回false
  • String[] list(): 目录下的 File的name(一级)
  • File[] listFile(): 目录下的File对象

练习删除目录

4. 枚举enum

  1. 解决的问题是,对象类型常量

  2. 枚举可以用数组操作

    以前:

    package com.etc.lesson14;
    
    import lombok.Getter;
    import lombok.Setter;
    
    @Getter
    public class Color {
    	
    	public static final Color RED = new Color(255, 0, 0);
    	public static final Color GREEN = new Color(0, 255, 0);
    	public static final Color BLUE = new Color(0, 0, 255);
    	public static final Color BLACK = new Color(0, 0, 0);
    	// 255,0,0
    	private int red;
    	private int green;
    	private int blue;
    	public Color(int red, int green, int blue) {
    		super();
    		this.red = red;
    		this.green = green;
    		this.blue = blue;
    	}
    	public Color() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    	
    	
    }
    
    

    现在: 枚举。

    • values(); 获取所有枚举数组
    • valueOf(String str); 字符出和枚举转换
    • ordinal() ; 获取枚举数组当前的下标
    package com.etc.lesson14;
    
    public enum ColorEnum {
    	// 第一行必须定义常量
    	RED(255, 0, 0), GREEN(0, 255, 0), BLUE(0, 0, 255), BLACK();
    	// 255,0,0
    	private int red;
    	private int green;
    	private int blue;
    	ColorEnum(int red, int green, int blue) {
    		this.red = red;
    		this.green = green;
    		this.blue = blue;
    	}
    	ColorEnum() {
    	}
    	public int getRed() {
    		return red;
    	}
    	public int getGreen() {
    		return green;
    	}
    	public int getBlue() {
    		return blue;
    	}
    }
    package com.etc.lesson14;
    
    public class TestMain4 {
    	public static void main(String[] args) {
    		ColorEnum red = ColorEnum.RED;
    		System.out.println(red);
    		switch (red) { // 枚举
    		case RED:
    			break;
    		default:
    			break;
    		}
    		
    		ColorEnum a2 = ColorEnum.valueOf("GREEN"); // 字符串转换枚举
    		System.out.println(a2);
    		System.out.println("--------------");
    		// 获取枚举数组
    		ColorEnum[] a3 = ColorEnum.values();
    		for (ColorEnum item: a3) {
    			System.out.println(item);
    		}
    		// 获取小标
    		System.out.println(a2.ordinal());
    	}
    }
    
    
    

5. 反射 reflect

反射: 类的类型就是反射。

学生类:构造 学号,姓名, 打游戏 : 实例: 每个学生

Animal: 构造 名字, 吃 : 实例: 每个动物


Class类: 构造-Constructor 属性 - Filed 方法- Method : 实例:每个类

学生类可以实例化出 高鹏

Class类可以实例化出 :学生类

Declared: 定义类的是有的

非Declared: 能访问的


		Constructor c1;
		Field f1;
		Method m1;
		
		// 获取反射对象:
		Class stuclazz = Student.class;
		System.out.println(stuclazz);
		Class aniclazz = Animal.class;
		System.out.println(aniclazz);
		
		Constructor[] stucon = stuclazz.getConstructors();
		// 遍历学生类型 Class对象中的构造方法
		for (Constructor item: stucon) {
			System.out.println(item);
		}
		System.out.println("-----");
		Constructor[] animalcon = aniclazz.getConstructors();
		// 遍历学生类型 Class对象中的构造方法
		for (Constructor item: animalcon) {
			System.out.println(item);
		}
		System.out.println("-----");
		// 遍历学生类型中有哪些属性
		Field[] stufs = stuclazz.getDeclaredFields();
		for (Field item: stufs) {
			System.out.println(item);
		}
		System.out.println("-----");
		// 遍历学生类型中有哪些方法
		Method[] stumethods = stuclazz.getDeclaredMethods();
		for (Method item: stumethods) {
			System.out.println(item);
		}
		// Declared 声明; 定义类中有的内容
		// 无Declared 可以访问到。 包括了Object继承下来的方法	
	

通过反射实例化

	Student stu = new Student("Tom", 10);
	stu.study();
	
	Class c  = Student.class;
	// 根据参数的类型获取构造方法
	Constructor con = c.getConstructor(new Class[]{String.class, int.class});
	// 实例化 反射类
	Object o = con.newInstance(new Object[]{"TOM", 20});
	if (o instanceof Student) {
		Student stu1 = (Student) o;
		stu1.study();
	}
	
	// 通过反射执行方法 invoke
	Method m = c.getDeclaredMethod("study", null);
	m.invoke(o, null);
	
	// 通过反射修改 属性
	Field f = c.getDeclaredField("name");
	f.setAccessible(true); // 私有属性允许访问
	f.set(o, "jerry"); // 修改属性
	System.out.println(f.get(o)); // 获取属性

反射对象的获取

  1. 类名.class

  2. 对象.getClass();

  3. Class.forName(“类全名”); // java.util.Date

6 Annotation: 注解

  • 活在哪:@Retention(RetentionPolicy.RUNTIME) jvm运行时; RetentionPolicy.CLASS存活在.class文件中 ; RetentionPolicy.SOURCE存活源码中

    源码中— javac 时编译 —.class ---- jvm

  • 修饰什么: @Target(ElementType.FIELD):属性 ; ElementType.METHOD ;

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.FIELD)
public @interface NotBlank {

	String message() default "不为空";
}

源码中— javac 时编译 —.class ---- jvm

  • 修饰什么: @Target(ElementType.FIELD):属性 ; ElementType.METHOD ;
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.FIELD)
public @interface NotBlank {

	String message() default "不为空";
}

posted @ 2021-04-11 12:42  剑心空明  阅读(0)  评论(0编辑  收藏  举报  来源