Java 学习 day09

01-面向对象(内部类访问规则)

 1 package myFirstCode;
 2 
 3 /*
 4 内部类的访问规则:
 5 1. 内部类可以直接访问外部类的成员,包括私有private。
 6     之所以可以直接访问外部类中的成员,是因为内部类中持有一个外部类的引用,格式 外部类名.this----//Outer.this.x
 7 2. 外部类要访问内部类,必须建立内部类对象。
 8  */
 9 
10 class Outer
11 {
12     private int x = 3;
13      class Inner //内部类
14      {
15          void function()
16          {
17              System.out.println("inner : "+Outer.this.x);//Outer.this.x
18          }
19      }
20      
21      void method()
22      {
23          Inner in = new Inner();
24          in.function();
25      }
26 }
27 
28 public class InnerClassDemo {
29 
30     public static void main(String[] args) {
31 //        Outer out = new Outer();
32 //        out.method();
33         
34         //直接访问内部类中的成员。
35 //        Outer.Inner in = new Outer().new Inner();//面试会问到,实际不会使用
36 //        in.function();
37     }
38 
39 }
View Code

02-面向对象(静态内部类)

 1 package myFirstCode;
 2 
 3 /*  // 不做重点掌握
 4 内部类的访问规则:
 5 1. 内部类可以直接访问外部类的成员,包括私有private。
 6     之所以可以直接访问外部类中的成员,是因为内部类中持有一个外部类的引用,格式 外部类名.this----//Outer.this.x
 7 2. 外部类要访问内部类,必须建立内部类对象。
 8 
 9 访问格式:
10 1. 当内部类定义在外部类的成员位置上,而且非私有,可以在外部类其他类中。
11 可以直接建立内部类对象。
12 格式
13     外部类名.内部类名 变量名= 外部类对象.内部类对象
14     Outer.Inner in = new Outer().new Inner();//面试会问到,实际不会使用
15 
16 2. 当内部类在成员位置上,就可以被成员修饰符所修饰。
17     比如,private:将内部类在外部类中进行封装。
18         static:内部类就具备static的特性。
19         当内部类被static修饰后,只能直接访问外部类中的static成员。出现了访问局限。
20         
21         在外部其他类中,如何直接访问静态内部类的非静态成员呢?
22         new Outer.Inner().function();
23         
24         在外部其他类中,如何直接访问静态内部类的静态成员呢?
25         Outer.Inner().function();//使用频率很低
26         
27     注意:当内部类中定义了静态成员,该内部类必须是static的。
28  */
29 
30 class Outer
31 {
32     private int x = 3;
33     static class Inner //静态内部类出现次数不多
34      {
35          void function()
36          {
37              System.out.println("inner : "+x);
38          }
39      }
40      
41      void method()
42      {
43          Inner in = new Inner();
44          in.function();
45      }
46 }
47 
48 public class InnerClassDemo {
49 
50     public static void main(String[] args)
51     {
52 //        new Outer.Inner().function();
53         Outer.Inner.function();
54         //直接访问内部类中的成员。
55 //        Outer.Inner in = new Outer().new Inner();//面试会问到,实际不会使用
56 //        in.function();
57     }
58 
59 }
View Code

03-面向对象(内部类定义原则)

 1 package myFirstCode;
 2 
 3 /*  // 不做重点掌握
 4 内部类的访问规则:
 5 1. 内部类可以直接访问外部类的成员,包括私有private。
 6     之所以可以直接访问外部类中的成员,是因为内部类中持有一个外部类的引用,格式 外部类名.this----//Outer.this.x
 7 2. 外部类要访问内部类,必须建立内部类对象。
 8 
 9 访问格式:
10 1. 当内部类定义在外部类的成员位置上,而且非私有,可以在外部类其他类中。
11 可以直接建立内部类对象。
12 格式
13     外部类名.内部类名 变量名= 外部类对象.内部类对象
14     Outer.Inner in = new Outer().new Inner();//面试会问到,实际不会使用
15 
16 2. 当内部类在成员位置上,就可以被成员修饰符所修饰。
17     比如,private:将内部类在外部类中进行封装。
18         static:内部类就具备static的特性。
19         当内部类被static修饰后,只能直接访问外部类中的static成员。出现了访问局限。
20         
21         在外部其他类中,如何直接访问静态内部类的非静态成员呢?
22         new Outer.Inner().function();
23         
24         在外部其他类中,如何直接访问静态内部类的静态成员呢?
25         Outer.Inner().function();//使用频率很低
26         
27     注意:当内部类中定义了静态成员,该内部类必须是static的。
28         当外部类中的静态方法访问内部类时,内部类也必须是static的。
29         
30 当描述事务时,事务的内部还有事务,该事务用内部类来描述,
31 因为内部事务在使用外部事务的内容。
32 
33 class Body
34 {
35     private class XinZang
36     {
37         
38     }
39     
40     public void show()
41     {
42     new XinZang().
43     }
44  */
45 
46 class Outer
47 {
48     private int x = 3;
49     static class Inner //静态内部类出现次数不多
50      {
51          void function()
52          {
53              System.out.println("inner : "+x);
54          }
55      }
56      
57      void method()
58      {
59          Inner in = new Inner();
60          in.function();
61      }
62 }
63 
64 public class InnerClassDemo {
65 
66     public static void main(String[] args)
67     {
68 //        new Outer.Inner().function();
69         Outer.Inner.function();
70         //直接访问内部类中的成员。
71 //        Outer.Inner in = new Outer().new Inner();//面试会问到,实际不会使用
72 //        in.function();
73     }
74 
75 }
View Code

04-面向对象(匿名内部类)

 1 package myFirstCode;
 2  /*
 3 匿名内部类:
 4 1. 匿名内部类其实就是内部类的简写格式。
 5 2. 定义匿名内部类的前提:
 6     内部类必须是继承一个类或者实现接口。
 7 3. 匿名内部类的格式:new 父类或接口(){定义子类的内容}
 8 4. 匿名内部类就是一个匿名子类对象。而且这个对象有点胖,可以理解为带内容的对象。
 9 5. 匿名内部类中定义的方法最好不要超过3个。
10   */
11 abstract class AbsDemo
12 {
13     abstract void show();
14 }
15 
16 class Outer4
17 {
18     int x = 3;
19 //    class Inner extends AbsDemo
20 //    {
21 //        void show()
22 //        {
23 //            System.out.println("show :"+ x);
24 //        }
25 //        void abc()
26 //        {
27 //        System.out.println("haha");
28 //        }
29 //    }
30     public void function()
31     {
32 //        new Inner().show();
33         
34         AbsDemo d = new AbsDemo()//匿名子类对象
35         {
36             void show()
37             {
38                 System.out.println("show :"+ x);
39             }
40             void abc()
41             {
42                 System.out.println("abc");
43             }
44         };
45         d.show();
46         //d.abc();//编译失败,转不了;
47     }
48 }
49 
50 class InnerClassDemo4 {
51 
52     public static void main(String[] args) {
53         // TODO Auto-generated method stub
54         new Outer4().function();
55     }
56 
57 }
View Code
 1 package myFirstCode;
 2 
 3 interface Inter5
 4 {
 5     void method();
 6 }
 7 
 8 class Test5
 9 {
10     //补足代码。通过匿名内部类。.
11     /*
12     static class Inner implements Inter5
13     {
14         public void method()
15         {
16             System.out.println("method run");
17         }
18     }
19     
20         static Inter5 function()
21         {
22             return new Inner();
23         }*/
24     
25     static Inter5 function()
26     {
27         return new Inter5()
28         {
29             public void method()
30             {
31                 System.out.println("method run");
32             }
33         };//要记得加分号
34 }
35 }
36 
37 class InnerClassTest {
38 
39     public static void main(String[] args) {
40         // TODO Auto-generated method stub
41         //Test.function():Test 类中有一个静态的方法 function。
42         //.method():function这个方法运算后的结果是一个对象。而且是一个Inter类型的对象。
43         //因为只有是Inter类型的对象,才可以调用method方法。
44         
45         Test5.function().method();
46         Inter5 in = Test5.function();
47         in.method();
48     }
49 
50 }
View Code

05-面向对象(异常概述)

 1 package myFirstCode;
 2 
 3 /*
 4 异常:就是程序在运行时出现不正常情况。
 5 异常由来:问题也是现实生活中一个具体的事务,也可以通过Java的类的形式进行描述。并封装成对象。
 6         其实就是Java对不正常情况进行描述后的对象体现。
 7         
 8 对于问题的划分:两种:一种是严重的问题,一种非严重的问题。
 9 
10 对于严重的,Java通过Error类进行描述。
11     对于Error一般不编写针对性的代码对其进行处理。
12 
13 对于非严重的,Java通过Exception类进行描述
14     对于Exception可以使用针对性的处理方式进行处理。
15     
16 无论Error或者Exception都具有一些共性内容。
17 比如:不正常情况的信息,引发原因等。
18 
19 Throwable
20     |--Error
21     |--Exception
22  */
23 
24 class Demo
25 {
26     int div(int a,int b)
27     {
28         return a/b;
29     }
30 }
31 
32 public class ExceptionDemo {
33 
34     public static void main(String[] args) {
35         // TODO Auto-generated method stub
36         Demo d = new Demo();
37         int x = d.div(4, 0);
38         System.out.println("x="+x);
39         System.out.println("over");
40     }
41 
42 }
View Code

06-面向对象(异常try-catch)

 1 package myFirstCode;
 2 
 3 /*
 4 异常:就是程序在运行时出现不正常情况。
 5 异常由来:问题也是现实生活中一个具体的事务,也可以通过Java的类的形式进行描述。并封装成对象。
 6         其实就是Java对不正常情况进行描述后的对象体现。
 7         
 8 对于问题的划分:两种:一种是严重的问题,一种非严重的问题。
 9 
10 对于严重的,Java通过Error类进行描述。
11     对于Error一般不编写针对性的代码对其进行处理。
12 
13 对于非严重的,Java通过Exception类进行描述
14     对于Exception可以使用针对性的处理方式进行处理。
15     
16 无论Error或者Exception都具有一些共性内容。
17 比如:不正常情况的信息,引发原因等。
18 
19 Throwable
20     |--Error
21     |--Exception
22     
23 2. 异常的处理
24  java提供了特有的语句进行处理。
25  try
26  {
27      需要被检测的代码;
28  }
29  catch(异常类 变量)
30  {
31      处理异常的代码;(处理方式)
32  }
33  finally
34  {
35      一定会执行的语句;
36  }
37  
38 3. 对捕获到的异常对象进行常见方法操作。
39     String getMessage(); 获取异常信息
40  */
41 
42 class Demo6
43 {
44     int div(int a,int b)
45     {
46         return a/b;
47     }
48 }
49 
50 public class ExceptionDemo {
51 
52     public static void main(String[] args) {
53         // TODO Auto-generated method stub
54         Demo6 d = new Demo6();
55         try {
56             int x = d.div(4, 0);
57             System.out.println("x="+x);
58         } catch (Exception e)   // Exception e = new ArithmeticException();
59         {
60             System.out.println("除零啦");
61             System.out.println(e.getMessage());  //  /by zero
62             System.out.println(e.toString());  // 异常名称:异常信息
63             e.printStackTrace();  // 异常名称,异常信息,异常出现的位置。
64                                 // 其实jvm默认的异常处理机制,就是在调用printStackTrace方法
65                                 //    打印异常的堆栈的跟踪信息。
66         }
67 
68         System.out.println("over");
69     }
70 
71 }
View Code

07-面向对象(异常声明throws)

 1 package myFirstCode;
 2 
 3 /*
 4 异常:就是程序在运行时出现不正常情况。
 5 异常由来:问题也是现实生活中一个具体的事务,也可以通过Java的类的形式进行描述。并封装成对象。
 6         其实就是Java对不正常情况进行描述后的对象体现。
 7         
 8 对于问题的划分:两种:一种是严重的问题,一种非严重的问题。
 9 
10 对于严重的,Java通过Error类进行描述。
11     对于Error一般不编写针对性的代码对其进行处理。
12 
13 对于非严重的,Java通过Exception类进行描述
14     对于Exception可以使用针对性的处理方式进行处理。
15     
16 无论Error或者Exception都具有一些共性内容。
17 比如:不正常情况的信息,引发原因等。
18 
19 Throwable
20     |--Error
21     |--Exception
22     
23 2. 异常的处理
24  java提供了特有的语句进行处理。
25  try
26  {
27      需要被检测的代码;
28  }
29  catch(异常类 变量)
30  {
31      处理异常的代码;(处理方式)
32  }
33  finally
34  {
35      一定会执行的语句;
36  }
37  
38 3. 对捕获到的异常对象进行常见方法操作。
39     String getMessage(); 获取异常信息
40     
41 
42 4. 
43 
44  */
45 
46 class Demo7
47 {
48     int div(int a,int b) throws Exception //在功能上通过throws的关键字 声明了该功能有可能会出现问题。
49     {
50         return a/b;
51     }
52 }
53 
54 public class ExceptionDemo1 {
55 
56     public static void main(String[] args) //throws Exception 
57     {
58         Demo7 d = new Demo7();
59 
60         int x;
61         try {
62             x = d.div(4,0);
63             System.out.println("x="+x);
64         } catch (Exception e) 
65         {
66             System.out.println(e.toString()); 
67         }
68 
69 
70         System.out.println("over");
71     }
72 
73 }
View Code

08-面向对象(多异常处理)

 1 package myFirstCode;
 2 
 3 /*
 4 异常:就是程序在运行时出现不正常情况。
 5 异常由来:问题也是现实生活中一个具体的事务,也可以通过Java的类的形式进行描述。并封装成对象。
 6         其实就是Java对不正常情况进行描述后的对象体现。
 7         
 8 对于问题的划分:两种:一种是严重的问题,一种非严重的问题。
 9 
10 对于严重的,Java通过Error类进行描述。
11     对于Error一般不编写针对性的代码对其进行处理。
12 
13 对于非严重的,Java通过Exception类进行描述
14     对于Exception可以使用针对性的处理方式进行处理。
15     
16 无论Error或者Exception都具有一些共性内容。
17 比如:不正常情况的信息,引发原因等。
18 
19 Throwable
20     |--Error
21     |--Exception
22     
23 2. 异常的处理
24  java提供了特有的语句进行处理。
25  try
26  {
27      需要被检测的代码;
28  }
29  catch(异常类 变量)
30  {
31      处理异常的代码;(处理方式)
32  }
33  finally
34  {
35      一定会执行的语句;
36  }
37  
38 3. 对捕获到的异常对象进行常见方法操作。
39     String getMessage(); 获取异常信息
40 
41 在函数上声明异常。
42 便于提高安全性,让调用出进行处理。不处理编译失败。
43 
44 对多异常的处理。
45 
46 1. 声明异常时,建议声明更为具体的异常,这样处理的可以更具体。
47 2. 对方声明几个异常,就对应几个catch块。不要定义多余的catch块。
48     如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面。
49     
50 建议在进行catch处理时,catch中一定要定义具体处理方式。
51 不要简单的定义一句 e.printStackTrace(),
52 也不要简单的就书写一条输出语句。
53 
54  */
55 
56 class Demo7
57 {
58     int div(int a,int b) throws ArithmeticException,ArrayIndexOutOfBoundsException,Exception //在功能上通过throws的关键字 声明了该功能有可能会出现问题。
59     {
60         int[] arr = new int[a];
61         
62         System.out.println(arr[4]);
63         return a/b;
64     }
65 }
66 
67 public class ExceptionDemo1 {
68 
69     public static void main(String[] args) //throws Exception 
70     {
71         Demo7 d = new Demo7();
72 
73         int x;
74         try {
75             x = d.div(4,0);
76             System.out.println("x="+x);
77         } 
78         catch(Exception e)
79         {
80             System.out.println("hahaha :"+e.toString());
81         }
82 /*
83         catch (ArithmeticException e) 
84         {
85             System.out.println(e.toString()); 
86             System.out.println("被零除了"); 
87         }
88         catch (ArrayIndexOutOfBoundsException e) 
89         {
90             System.out.println(e.toString()); 
91             System.out.println("角标越界啦"); 
92         }
93 */
94 
95         System.out.println("over");
96     }
97 
98 }
View Code

09-面向对象(自定义异常)

  1 package myFirstCode;
  2 
  3 /*
  4 因为项目中会出现特有的问题。
  5 而这些问题并未被Java所描述并封装对象。
  6 所以对于这些特有的问题可以按照Java的对问题封装的思想。
  7 将特有的问题,进行自定义的异常封装。
  8 
  9 自定义异常。
 10 
 11 
 12 需求:在本程序中,对于除数是负数-1,也视为是错误的,是无法进行运算的。
 13      那么就需要对这个问题进行自定义的描述。
 14      
 15 当在函数内部出现了throw抛出异常对象,那么就必须给出对应的处理动作。
 16 要不在内部try catch处理。
 17 要不在函数上声明 让调用者处理。
 18 
 19 一般情况下,函数内出现异常,函数上需要声明。
 20 
 21 发现打印的结果中只有异常的名称,却没有异常的信息。
 22 因为自定义的异常并未定义信息。
 23 
 24 如何定义异常信息呢?
 25 因为父类中已经把异常信息的操作都完成了。
 26 所以子类只要在构造时,将异常信息传递给父类通过super语句。
 27 那么就可以直接通过getMessage方法获取自定义的异常信息。
 28 
 29 
 30 自定义异常:
 31 必须是自定义类继承Exception。
 32 
 33 继承Exception原因:
 34 异常体系有一个特点:因为异常类和异常对象都需要被抛出。
 35 他们都具备可抛性。这个可抛性是throwable这个体系中的独有特点。
 36 
 37 只有这个体系中的类和对象才可以被throws和throw操作。
 38  */
 39 class FuShuException extends Exception //getMessage();
 40 {
 41     private int value;
 42     
 43     FuShuException(String msg,int value)
 44     {
 45         super(msg);
 46         this.value = value;
 47     }
 48     public int getValue()
 49     {
 50         return value;
 51     }
 52 }
 53 
 54 class Demo8
 55 {
 56     int div(int a,int b) throws FuShuException
 57     {
 58         if(b<0)
 59             throw new FuShuException("出现了除数是负数的情况----- /By fushu",b); // 手动通过throw关键字抛出一个自定义异常对象。
 60         return a/b;
 61     }
 62 }
 63 
 64 public class ExceptionDemo1 {
 65 
 66     public static void main(String[] args) //throws Exception 
 67     {
 68         Demo8 d = new Demo8();
 69         try 
 70         {
 71             int    x = d.div(4,-1);
 72             System.out.println("x="+x);
 73         } 
 74         catch (FuShuException e) 
 75         {
 76             System.out.println(e.toString());
 77             //System.out.println("除数出现负数了");
 78             System.out.println("错误的负数是:"+e.getValue());
 79         }
 80 
 81         System.out.println("over");
 82     }
 83 
 84 }
 85 
 86 /*
 87 class Throwable
 88 {
 89     private String message;
 90     Throwable(String message)
 91     {
 92         this.message = message;
 93     }
 94     public String getMessage()
 95     {
 96         return message;
 97     }
 98 }
 99 
100 class Exception extends Throwable
101 {
102     Exception(String message)
103     {
104         super(message);
105     }
106 }
107 */
View Code

10-面向对象(throw和throws的区别)

throws和throw的区别:
throws使用在函数上。(小括号和大括号之间)()***{}
throw使用在函数内。

throws后面跟的异常类,可以跟多个,用逗号隔开。
throw后跟的是异常对象。

11-面向对象(RuntimeException)

 1 package myFirstCode;
 2 
 3 /*
 4 Exception中有一个特殊的子类异常RuntimeException 运行时异常。
 5 
 6 如果在函数内容抛出该异常,函数上可以不用声明,编译一样通过。
 7 
 8 如果在函数上声明了该异常。调用者可以不用进行处理。编译一样通过。
 9 
10 之所以不用函数上声明,是因为不需要让调用者处理。
11 当该异常发生,希望程序停止。因为在运行时,出现了无法继续运算的情况,系统停止程序后,
12 对代码进行修正。
13 
14 自定义异常时:如果该异常的发生,无法再继续进行运算,
15 就让自定义异常继承RunTimeException。
16 
17 对于异常分两种:
18 1、 编译时被检测的异常。
19 2. 编译时不被检测的异常(运行时异常,RunTimeExceptiong以及其子类)。
20  */
21 
22 class Demo9
23 {
24     int div(int a,int b) //throws ArithmeticException
25     {
26         if(b==0)
27             throw new ArithmeticException("被零除啦");
28         return a/b;
29     }
30 }
31 
32 public class ExceptionDemo4 {
33 
34     public static void main(String[] args) //throws Exception 
35     {
36         Demo9 d = new Demo9();
37         int x = d.div(4, 0);
38         System.out.println("x="+x);
39 
40         System.out.println("over");
41     }
42 
43 }
View Code

12-面向对象(异常练习)

 1 package myFirstCode;
 2 
 3 /*
 4  毕老师用电脑上课。
 5 
 6  开始思考上课出现的问题。
 7 
 8  比如问题是:电脑蓝屏。
 9  电脑冒烟。
10 
11  要对问题进行描述,封装成对象。
12 
13 可是当冒烟发生后,出现讲课进度无法继续。
14 
15 出现了讲师的问题:课时计划无法完成。
16  */
17 class LanPingException extends Exception {
18     LanPingException(String message) {
19         super(message);
20     }
21 }
22 
23 class MaoYanException extends Exception {
24     MaoYanException(String message) {
25         super(message);
26     }
27 }
28 
29 class NoPlanException extends Exception
30 {
31     NoPlanException(String message) {
32         super(message);
33     }
34 }
35 
36 class Computer {
37     private int state = 3;
38 
39     public void run() throws LanPingException, MaoYanException {
40         if (state == 2)
41             throw new LanPingException("蓝屏啦");
42         if (state == 3)
43             throw new MaoYanException("冒烟啦");
44         System.out.println("电脑运行");
45     }
46 
47     public void reset() {
48         state = 1;
49         System.out.println("电脑重启");
50     }
51 }
52 
53 class Teacher {
54     private String name;
55     private Computer cmpt;
56 
57     Teacher(String name) {
58         this.name = name;
59         cmpt = new Computer();
60     }
61 
62     public void prelect() throws NoPlanException 
63     {
64         try {
65             cmpt.run();
66 
67         } catch (LanPingException e) {
68             cmpt.reset();
69 
70         } catch (MaoYanException e) {
71             test();
72             throw new NoPlanException("课时无法继续"+e.getMessage());//throw 存在,下面不要有语句
73         }
74         System.out.println("讲课");
75     }
76     public void test()
77     {
78         System.out.println("练习");
79     }
80 }
81 
82 class ExceptionTest {
83 
84     public static void main(String[] args) {
85         Teacher t = new Teacher("毕老师");
86         try 
87         {
88             t.prelect();
89         } 
90         catch (NoPlanException e) 
91         {
92             System.out.println(e.toString());
93             System.out.println("换电脑或者放假");
94         }
95 
96     }
97 
98 }
View Code
原文地址:https://www.cnblogs.com/SH-xuliang/p/7215357.html