JAVA进阶24

1、局部内部类

 1 package day10;
 2 
 3 /**
 4  * 如果一个类是定义在一个方法内部的,那么这就是一个局部内部类
 5  * “局部”,只有当前所属的方法才能使用它,出了这个方法外面就不能用了
 6  * 定义格式:
 7  * 修饰符 class 外部类名称{
 8  * 修饰符 返回值类型 外部类名称(参数列表){
 9  * class 局部内部类名称{
10  * //.......
11  * }
12  * }
13  * }
14  */
15 public class Outer {
16     public void method01() {
17         class Inner {       //局部内部类
18             int num = 100;
19             public void methodInner(){
20                 System.out.println(num);
21             }
22         }
23         Inner in = new Inner();
24         in.methodInner();
25     }
26 }
Outer
 1 package day10;
 2 
 3 /**
 4  * -------调用outer 中的局部内部类
 5  *
 6  * @Classname OuterMain
 7  * @Description TODO
 8  * @Date 2019/5/5 22:25
 9  * @Created by Administrator
10  */
11 public class OuterMain {
12     public static void main(String[] args) {
13         Outer out = new Outer();
14         out.method01();
15     }
16 }
View Code

运行结果图

 

2、IO流练习(重新运行需更改文件路径位置)

 1 package cn.demo02;
 2 
 3 import java.io.*;
 4 
 5 /**
 6  * 1、图片读取到字节数组中
 7  * 2、将字节数组写出到文件
 8  */
 9 public class IO_Demo06 {
10     public static void main(String[] args) {
11         //图片转成字节数组
12         byte[] datas = fileToByteArray("F:/Java/test018/demo01/s.jpg");
13         System.out.println(datas.length);
14         byteArrayToFile(datas, "F:/Java/test018/demo01/s1.jpg");
15     }
16 
17     /*
18      *  1、图片读取到字节数组汇总
19      *  1)、图片到程序,FileputStream
20      *  2)、程序到字节数组ByteArrayOutStream
21      */
22     public static byte[] fileToByteArray(String filePath) {
23         //1、创建源与目的地
24         File src = new File(filePath);
25         byte[] dest = null;
26         //2、选择流
27         InputStream is = null;
28         ByteArrayOutputStream baos = null;
29         try {
30             is = new FileInputStream(src);
31             baos = new ByteArrayOutputStream();
32             //3、操作
33             byte[] flush = new byte[1024 * 10];   //缓冲容器
34             int len = -1;   //接收长度
35             while ((len = is.read(flush)) != -1) {
36                 baos.write(flush, 0, len);      //写出到字节数组中
37             }
38             baos.flush();
39             return baos.toByteArray();
40         } catch (FileNotFoundException e) {
41             e.printStackTrace();
42         } catch (IOException e) {
43             e.printStackTrace();
44         } finally {
45             try {
46                 if (null != is) {
47                     is.close();
48                 }
49             } catch (IOException e) {
50                 e.printStackTrace();
51             }
52         }
53         return null;
54     }
55 
56     /*
57      *  2、字节数组写出到图片
58      *  1)字节数组到程序 ByteArrayInputStream
59      *  2)程序到文件FileOutputStream
60      */
61     public static void byteArrayToFile(byte[] src, String filePath) {
62         //创建源
63         File dest = new File(filePath);
64         //选择流
65         InputStream is = null;
66         OutputStream os = null;
67         try {
68             is = new ByteArrayInputStream(src);
69             os = new FileOutputStream(dest);
70             //操作
71             byte[] flush = new byte[5];     //缓冲容器
72             int len = -1;       //接收长度
73             while ((len = is.read(flush)) != -1) {
74                 os.write(flush, 0, len);
75             }
76             os.flush();
77         } catch (FileNotFoundException e) {
78             e.printStackTrace();
79         } catch (IOException e) {
80             e.printStackTrace();
81         } finally {
82             //释放资源
83             try {
84                 if (null!=os){
85                     os.close();
86                 }
87             } catch (IOException e) {
88                 e.printStackTrace();
89             }
90 
91         }
92 
93     }
94 }
复制拷贝图片

运行结果图

 

3、装饰器设计模式

 1 package cn.demo02;
 2 
 3 /**
 4  * 模拟咖啡:
 5  * 1、抽象组件:需要装饰的抽象对象(接口或抽象父类)
 6  * 2、具体组件:需要装饰的对象
 7  * 3、抽象装饰类:包含了对抽象组件的引用以及装饰着共有的方法
 8  * 4、具体装饰类:被装饰的对象
 9  */
10 public class IO_DecorateTest02 {
11     public static void main(String[] args) {
12         Drink coffee = new Coffee();
13         Drink suger = new Suger(coffee);    //装饰
14         System.out.println(suger.info() + "--->" + suger.cost());
15         Drink milk = new Milk(coffee);      //装饰
16         System.out.println(milk.info() + "--->" + milk.cost());
17     }
18 }
19 
20 //抽象组件
21 interface Drink {
22     double cost();      //费用
23 
24     String info();      //说明
25 }
26 
27 //具体组件
28 class Coffee implements Drink {
29     private String name = "原味咖啡!";
30 
31     @Override
32     public double cost() {
33         return 15;
34     }
35 
36     @Override
37     public String info() {
38         return name;
39     }
40 }
41 
42 //抽象装饰类
43 abstract class Decorate implements Drink {
44     //    对抽象组件的引用
45     private Drink drink;
46 
47     public Decorate(Drink drink) {
48         this.drink = drink;
49     }
50 
51     @Override
52     public double cost() {
53         return this.drink.cost();
54     }
55 
56     @Override
57     public String info() {
58         return this.drink.info();
59     }
60 }
61 
62 //具体装饰类
63 class Milk extends Decorate {
64 
65     public Milk(Drink drink) {
66         super(drink);
67     }
68 
69     @Override
70     public double cost() {
71         return super.cost() * 4;
72     }
73 
74     @Override
75     public String info() {
76         return super.info() + "加入了牛奶";
77     }
78 }
79 
80 //具体装饰类
81 class Suger extends Decorate {
82 
83     public Suger(Drink drink) {
84         super(drink);
85     }
86 
87     @Override
88     public double cost() {
89         return super.cost() * 2;
90     }
91 
92     @Override
93     public String info() {
94         return super.info() + "加入了糖";
95     }
96 }
View Code

运行结果图

4、匿名内部类

注意事项:①匿名内部类,在创建对象的时候,只能使用唯一一次!如果需要多次创建对象,而且类的内容一样的话,那么就必须使用单独定义的实现类

     ②匿名对象,在调用方法的时候,只能调用唯一一次。如果希望同一个对象,调用多次方法,那么必须给对象取一个名字。

     ③匿名内部类是省略了【实现类/子类名称】,但是倪敏对象时省略了【对象名称】

 1 package Day11;
 2 
 3 /**
 4  *      如果接口的实现类(或者是父类的子类)只需要使用唯一的一次。
 5  *      那么这种情况下就可以省略掉该类的定义。而改为使用【匿名内部类】
 6  *
 7  *      匿名内部类的定义格式:
 8  *                          接口名称 对象名 = new 接口名称(){
 9  *                              //覆盖重写所有的抽象方法
10  *                          };
11  * @Classname MyInterfaceMain
12  *  * @Description TODO
13  *  * @Date 2019/5/6 21:05
14  *  * @Created by Administrator
15  */
16 public class MyInterfaceMain {
17     public static void main(String[] args) {
18 //        MyInterface s = new MyInterfaceA();
19 //        s.method();
20         //使用匿名内部类
21         MyInterface mi = new MyInterface() {
22             @Override
23             public void method() {
24                 System.out.println("匿名内部类的使用!");
25             }
26         };
27         mi.method();
28     }
29 }
View Code

运行结果图

5、IO转换流

 1 package cn.demo02;
 2 
 3 import java.io.*;
 4 import java.nio.Buffer;
 5 
 6 /**
 7  * 转换流:InputStreamReader/OutputStreamWriter
 8  * 1、以字符流的形式操作字节流(纯文本)
 9  * 2、指定字符集
10  */
11 public class IO_BufferedTest01 {
12     //操作System.in 和 System.out
13     public static void main(String[] args) {
14         try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
15             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));){
16             //循环获取键盘的输入(exit退出),输出此内容
17             String msg = "";
18             while (!msg.equals("exit")){
19                 msg = reader.readLine();        //循环读取
20                 writer.write(msg);              //循环写出
21                 writer.newLine();
22                 writer.flush();     //强制刷新
23             }
24         }catch(IOException e){
25             System.out.println("操作异常!");
26         }
27     }
28 
29 }
View Code

运行结果图

6、IO数据流

 1 package cn.demo02;
 2 
 3 import java.io.*;
 4 
 5 /**
 6  *      IO数据流:
 7  *          1、写出后读取
 8  *          2、读取的顺序与写出保持一致
 9  *
10  *          DataOutputStream
11  *          DataInputStream
12  * @Classname IO_DataTest01
13  * @Description TODO
14  * @Date 2019-5-7 11:28
15  * @Created by Administrator
16  */
17 public class IO_DataTest01 {
18     public static void main(String[] args) throws IOException {
19         //写出
20         ByteArrayOutputStream baos = new ByteArrayOutputStream();
21         DataOutputStream dot = new DataOutputStream(new BufferedOutputStream(baos));
22         //操作数据类型+数据
23         dot.writeUTF("你好");
24         dot.writeInt(22);
25         dot.writeBoolean(false);
26         dot.writeChar('s');
27         dot.flush();
28         byte[] datas = baos.toByteArray();
29         //读取
30         DataInputStream dis = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
31         //顺序与写出的一致(不一致会报错)
32         String msg = dis.readUTF();
33         int age = dis.readInt();
34         boolean flag = dis.readBoolean();
35         char ch = dis.readChar();
36         System.out.println(flag);
37     }
38 }
View Code

运行结果图

7、IO对象流(读取与写出的顺序要保持一致)

 1 package cn.demo02;
 2 
 3 import java.io.*;
 4 
 5 /**
 6  *      对象流:
 7  *              1、写出后读取
 8  *              2、读取的顺序与写出保持一致
 9  *              3、不是所有的对象都可以序列化 Serializable
10  * @Classname IO_Objtest01
11  * @Description TODO
12  * @Date 2019-5-7 11:48
13  * @Created by Administrator
14  */
15 public class IO_Objtest01{
16     public static void main(String[] args) throws IOException, ClassNotFoundException {
17         //写出
18         ByteArrayOutputStream baos = new ByteArrayOutputStream();
19         ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(baos));
20         //操作数据类型+数据
21         oos.writeInt(24);
22         oos.writeUTF("黄平");
23         oos.writeObject("弱是原罪!");
24         Emp e = new Emp("岳飞",50000);
25         oos.writeObject(e);
26         oos.flush();
27         byte[] datas = baos.toByteArray();
28         System.out.println(datas.length);
29         //读取
30         ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
31         //顺序与写出一致
32         int s = ois.readInt();
33         String s1 = ois.readUTF();
34         //对象的数据还原
35         Object a = ois.readObject();
36         Object b = ois.readObject();
37 
38         if (b instanceof Emp){
39             Emp emp = (Emp) b;
40             System.out.println(emp.getName()+"--》"+emp.getSlary());
41         }
42         System.out.println(s1);
43         System.out.println(a);
44     }
45 }
46 class Emp implements java.io.Serializable{
47     private String name;      //transient可用于不需要序列化的
48     private double slary;
49 
50     public Emp() {
51     }
52 
53     public Emp(String name, double slary) {
54         this.name = name;
55         this.slary = slary;
56     }
57 
58     public String getName() {
59         return name;
60     }
61 
62     public void setName(String name) {
63         this.name = name;
64     }
65 
66     public double getSlary() {
67         return slary;
68     }
69 
70     public void setSlary(double slary) {
71         this.slary = slary;
72     }
73 }
View Code

运行结果图

8、IO打印流

 1 package cn.demo02;
 2 
 3 import java.io.*;
 4 
 5 /**
 6  *
 7  *      打印流: PrintStream
 8  * @Classname IO_PrintTest01
 9  * @Description TODO
10  * @Date 2019-5-7 14:13
11  * @Created by Administrator
12  */
13 public class IO_PrintTest01 {
14     public static void main(String[] args) throws FileNotFoundException {
15         //打印流System.out
16         PrintStream ps = System.out;
17         ps.println("打印流");
18         ps.println(true);
19 
20         ps = new PrintStream(new BufferedOutputStream(new FileOutputStream("aaa.txt")),true);
21         ps.println("打印流");
22         ps.println(true);
23         ps.close();
24 
25         //重定向输出端
26         System.setOut(ps);
27         System.out.println("change");
28 
29         //重定向回控制台
30         ps = new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true);
31         System.out.println("i am back...");
32     }
33 }
View Code

运行结果图

 

posted @ 2019-05-07 18:21  贫血的吸血鬼  阅读(268)  评论(0编辑  收藏  举报