一 内部类访问规则
1.内部类的概念
从JDK1.1开始,引入了“内部类”。
可以利用内部类对一些逻辑上相互联系的类进行分组,并可控制一个类在另一个类中的”可见性”。
内部类:将一个类定义在另一个类的里面(“类中有类”,也叫 内置类,嵌套类)。
字节码文件名:外部类名$内部类名.class。
一般分析事物,设计类的时候,发现该事物A描述中还有事物B,并且B还“访问”A的内容,这时就需要把B定义为内部类进行描述。
2.内部类的访问规则(特点)
- 内部类可以直接访问外部类的成员,包括私有成员。(之所以可以访问外部类中的成员,是因为内部类中持有一个外部类的引用,格式 : 外部类名 . this)
- 外部类要访问内部类,必须建立内部类对象
问:类能被private修饰吗?
答:能,普通类是不能被私有的,但是内部类在成员位置上的到时候却可以被私有,因为它在成员位置,具有类中成员的功能.
1
2
3
4
5
6
7
8
9
10
11
12class Outer { private int x = 3; class Inner { int x = 4; void function() { int x = 6; System.out.println("inner :" + x); } } }
上面的代码,输出 6 ;若要输出4,则需写 this.x ; 若要输出3,则需写 Outer.this.x(因为内部类中持有一个外部类的引用)。
3.示例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29class Outer { private int x = 3; class Inner { void function() { System.out.println("inner :" + x); } } void method() { Inner in = new Inner(); in.function(); } } public class InnerClassDemo { public static void main(String[] args) { Outer out = new Outer(); out.method(); //直接访问外部类中的内部类中的成员(非静态内部类) Outer.Inner in = new Outer().new Inner();//这句极少用 in.function(); } }
二 静态内部类
1、当内部类在成员位置上时,就可以被成员修饰符所修饰
- private:将内部类在外部类中进行封装
- static:内部类就具备了静态的特性,当内部类被static修饰后,只能直接访问外部类中的static成员,出现了访问局限
- 在外部其他类中,直接访问static内部类的非静态成员 : new 外部类.内部类().方法
- 在外部其他类中,直接访问static内部类的静态成员 : new 外部类.内部类.方法
2、访问格式
当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中,可以直接建立内部类对象。格式如下:
- 外部类名.内部类名 变量名 = 外部类对象.内部类对象;
- Outer.Inner in = new Outer().new Inner();(外部类直接访问内部类的方式)
3、注意:
- 当内部类中定义了静态成员,该内部类必须是static
- 当外部类中的静态方法,访问内部类时,内部类也必须是 static
三 内部类定义原则
1、类是用来描述事物的,当描述事物时,事物的内部还有事物,该事物用内部类来描述,因为内部事物在使用外部事物的内容。
2、隐藏内部类,提供相应接口
3、内部类定义在局部时:
(1) 不可以被成员修饰符修饰
(2)可以直接访问外部类中的成员,因为还持有外部类中的引用,但是不可以访问它所在的局部中的变量(除非该变量被final修饰)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24class Outer { int x = 3; void method() { System.out.println("outer:" + x); } class Inner { // int x = 4; void function() { // int x = 4; System.out.println("inner:" + x); // System.out.println("inner:"+this.x); // System.out.println("inner:"+Outer.this.x); } } } public class Demo { public static void main(String[] args) { // Outer.Inner in = new Outer().new Inner(); // in.function(); } }
四 匿名内部类
之前内部类都是都定义在外部类成员变量位置上的,
只有定义在成员位置上时,才能被私有或者静态修饰,一般内部类是不会被public修饰的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33class Outer { int x = 3; void method(final int a) { // a++; 错 final int y = 4; class Inner { // 不能再用static,因为static只修饰成员,而已经定义在局部了 void function() { System.out.println(Outer.this.x); System.out.println(y);// 内部类访问局部变量,该局部变量需要被声明为final System.out.println(a); } /* * static void function(){ 这样也是不行的 System.out.println(Outer.this.x); * } */ } new Inner().function();// 有对象调用才会运行 } } public class InnerClassDemo3 { public static void main(String[] args) { Outer out = new Outer(); out.method(7); out.method(8); } }
匿名内部类:
1、匿名内部类其实就是内部类的简写形式
2、定义匿名内部类的前提:内部类必须是继承一个类或者实现接口
3、匿名内部类的格式:new 父类或者接口(){定义子类的内容}
4、其实匿名内部类就是一个匿名子类对象,而且这个对象有点胖,也可以理解为,带内容的对象
5、匿名内部类中定义的方法最好不要超过3个
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28abstract class AbsDemo { abstract void show(); } class Outer { int x = 3; class Inner1 extends AbsDemo { // 继承 void show() { System.out.println("show:" + x); } } public void function() { new Inner1().show(); new AbsDemo() { void show() { System.out.println("x = " + x); } }.show(); } } public class Demo { public static void main(String[] args) { new Outer().function(); } }
1
2
3
4
5new AbsDemo() { void show() { System.out.println("x = " + x); } }
上面这个整体是个对象,是AbsDemo类的子类对象,因为只有子类才能复写它的抽象方法。相当于new Inner()后复写了show()。
“内部类100%都是子类对象”
1
2
3
4
5
6
7
8new AbsDemo() { void show() { System.out.println("x = " + x); } void abc() { System.out.println("abc"); } }.show();
可以定义abc(),但不能同时调用show()和abc()。
1
2
3
4
5
6
7
8
9
10AbsDemo d = new AbsDemo() { void show() { System.out.println("x = " + x); } void abc() { System.out.println("abc"); } }; d.show(); d.abc();//不行,因为多态,只能用父类有定义的方法
还要注意,如果父类有过多方法需要复写,就不要写匿名内部类,因为阅读性会变得很差,一般匿名内部类写1~2个就行了。
只要会写就行了。
留个练习:
1
2
3
4
5
6
7
8
9
10
11
12
13
14interface Inter { void method(); } class Test { // 补足代码,通过匿名内部类 } public class InnerClassTest { public static void main(String[] args) { Test.function().method(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36interface Inter { void method(); } class Test { /*static class Inner implements Inter { public void method() { System.out.println("method run"); } } */ static Inter function() { return new Inter() { public void method() { System.out.println("method run"); } }; } } public class InnerClassTest { public static void main(String[] args) { //Test.function():Test类中有一个静态的方法function //.method():function()这个方法运算后的结果是一个对象,而且是一个Inter类型的对象。 //因为只有是Inter类型的对象,才可以调用method方法 Test.function().method(); //换一种写法就是: Inter in = Test.function(); in.method(); } }
什么时候使用匿名内部类呢?
有一种常见情况:当你使用的方法的参数是接口类型,该接口里面的方法不超过三个,这时候可以用匿名内部类作为参数传递。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17interface Inter { void method(); } public class Demo { public static void main(String[] args) { show(new Inter() { public void method() { System.out.println("method run"); } }); } public static void show(Inter in) { in.method(); } }
面试题:没有父类,也没有接口,还能搞匿名内部类吗?
可以,有Object类这个父类。
1
2
3
4
5
6
7
8
9public class Demo { public static void main(String[] args) { new Object() { void function() { System.out.println("function"); } }.function(); } }
五 异常描述
异常:就是程序在运行时出现的不正常情况。
异常的由来:问题也是现实生活中的一个具体的事物,也可以通过java类的形式进行描述。
并封装成对象。其实就是对java不正常情况进行描述后对象的体现。
把问题封装成对象,就是异常。对于问题划分两种:一种是严重的问题,一种是非严重的。
- 对于严重的。java通过Error类进行描述。Error一般不编写针对性的代码对其进行处理。
- 对于非严重的。java通过Exception类进行描述。Exception可以使用针对性的处理方式处理;比如,数据角标越界等。
无论error或者exception都具有一些共性内容。比如:不正常情况的信息,引发原因等。
抽取出来的父类和体系
Throwable
|-- Error
|-- Exception
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16class ExceptionDemo { int div(int a, int b) { return a / b; } } public class Demo { public static void main(String[] args) { ExceptionDemo ed = new ExceptionDemo(); int x = ed.div(4, 0); // x = ed.div(4,1);//编译不报错,但是运行报错 Exception in thread "main" // java.lang.ArithmeticException: / by zero System.out.println("x=" + x); System.out.println("over"); } }
编译没问题,只有运行时才会去运算。
六 异常 try - catch
Java虚拟机内部有内置的异常处理机制,程序出现问题,它就提前停止了。
Java提供了特有的语句进行处理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15try { 需要被检测的代码; } catch(异常类 变量) { 处理异常的代码;(处理方式) } finally { 一定会执行的语句; }
Finally代码块只有一种情况不会被执行,就是在之前执行了System.exit(0)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22class ExceptionDemo { int div(int a, int b) { return a / b; //次序1:这里产生了 ArithmeticException,并把问题封装成了一个对象 } } public class Demo { public static void main(String[] args) { ExceptionDemo ed = new ExceptionDemo(); try { int x = ed.div(4, 0); //次序2:把异常对象抛到这里了:new ArithmeticException() System.out.println("x=" + x); //因为上边已经发生了异常,所以这一步不会再执行 } catch (Exception e) { // 次序3:Exception e = ArithmeticException(); System.out.println("除零了"); System.out.println(e.toString());// 异常名称:异常信息 System.out.println(e.getMessage());// by zero e.printStackTrace();// 异常名称,异常信息,异常出现的位置 // 其实jvm默认的异常处理机制,就是在调用printStackTrace方法。打印异常的堆栈的跟踪信息。 } System.out.println("over");//次序4:异常在try……catch中已经得到了处理,这里可以继续执行了。 } }
3、对捕获到的异常对象进行常见方法操作。
String getMessage();获取异常信息
System.out.println(e.getMessage());//byzero
System.out.println(e.toString());//异常名称:异常信息。
e.printStackTrace()输出结果:
1
2
3
4
5java.lang.ArithmeticException: / by zero over at ExceptionDemo.div(Demo.java:3) at Demo.main(Demo.java:11)
七 异常声明throws
通过throws关键字,可以声明该方法可能会出现异常,把异常抛给上一级处理,可以避免编译失败。
一直抛到最后main方法都不处理,虚拟机就按默认的操作来处理 —— 出错,停止运行。
注意:在类的方法上throws Exception,对象使用该方法时,必须捕捉或者声明抛出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21class Demo { int div(int a, int b) throws Exception {//在功能上通过throws的关键字声明了该功能有可能出现的问题 return a / b; } } public class ExceptionDemo { public static void main(String[] args) { // 如果不try……catch,就继续throws Exception Demo d = new Demo(); try { int x = d.div(4, 1); } catch (Exception e) { System.out.println(e.toString()); } System.out.println("over"); } }
上面的程序,如果在div方法throws Exception,d.div(4,1);这句不抛出或进行处理,还是会编译失败。
八 多异常处理
对多异常的处理:
1、声明异常时,建议声明具体的异常,(不要仅仅throws Exception),这样处理可以更具体;
2、对方声明几个异常,就对应有几个catch块,不要定义多余的catch块。如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面;
3、建议进行catch处理,要定义具体处理方式,不要仅仅打印异常信息e.printStackTrace()。
4、建议不要在处理完具体的try……catch后,又写throws Exception,因为这样有可能会隐藏掉问题,使程序继续往下执行而你又不知道已经出现异常了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23class ExceptionDemo { int div(int a, int b) throws ArithmeticException, ArrayIndexOutOfBoundsException { // 再功能上通过 throws的关键字声明了该功能有可能会出现问题,可以声明多个异常 int[] arr = new int[a]; System.out.println(arr[3]);// 虽然有多个异常,但是当前面有异常时,该方法块内,异常后面的就不会运行 return a / b; } } public class Demo { public static void main(String[] args) { ExceptionDemo ed = new ExceptionDemo(); try { int x = ed.div(3, 0); System.out.println("x=" + x); } catch (ArithmeticException e) { System.out.println("除零了"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("角标越界了"); } System.out.println("over"); } }
上面的程序,有算数异常和数组角标越界异常。
不会同时发生两个异常,当发生某一个异常的时候,程序就跳转去处理了,不会再继续执行下去了。
例如:该程序如果 arr[2]并且ed.div(3, 1),就会正常运行;
arr[3]并且ed.div(3, 0)会先处理角标越界,ed.div(3, 0)操作不会被执行到。
九 自定义异常
1、因为项目中会出现特有的问题,而这些问题并未被java所表述并封装对象,
所以对于这些特有的问题可以按照java对问题的封装思想,将其进行自定义的异常封装。
(Java自己封装好的异常可以自动抛出,但我们自己定义的异常,要生成异常对象手动抛出。)
2、当在函数内部出现了 throw 抛出异常对象,那么就必须要给对应的处理动作
- 要么在内部 try catch 处理,
- 要么在函数上声明让调用者处理
3、一般情况下,函数内出现异常,函数上需要声明
4、给自定义异常定义异常信息:因为父类中已经把异常信息的操作都完成了,所以子类只要在构造时,
将异常信息传递给父类通过super语句,那么就可以直接通过 getMessage 方法获取自定义的异常信息
5、自定义类必须是自定义类继承 Exception 类。
6、为什么要继承Exception
- 异常体系有一个特点:因为异常类和异常对象都要被抛出,他们都具备可抛性
- 这个可抛性是 Throwable 这个体系中独有特点
- 只有这个体系中的类和对象才可以被 throws 和 throw 操作
继承关系如下:
|--Throwable
|--Error
|--Exception
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40/*需求:在本程序中,对于除数是负数,也视为是错误的,是无法进行运算的。 那么就需要对这个问题进行自定义的描述。*/ class FuShuException extends Exception { private int value; FuShuException(String msg, int value) { super(msg);// 定义异常信息,因为父类中已经把异常信息的操作都完成了,所以子类只要在构造时,将异常信息传递给父类通过super语句,那么就可以直接通过 // getMessage 方法获取自定义的异常信息 this.value = value; } public int getValue() { return value; } } class ExceptionDemo { int div(int a, int b) throws FuShuException { // 函数内出现异常,函数上需要声明 if (b < 0) { throw new FuShuException("除数为负数了", b);//手动通过throw关键字抛出一个自定义异常对象 } return a / b; } } public class Demo { public static void main(String[] args) { ExceptionDemo ed = new ExceptionDemo(); int x = 0; try { x = ed.div(3, -1); } catch (FuShuException e) { System.out.println(e.toString()); System.out.println("该负数是:" + e.getValue()); } System.out.println("x=" + x); System.out.println("结束"); } }
输出:
P1.FuShuException: 除数为负数了
该负数是:-1
x=0
结束
十 throw和throws的区别
1、throws 使用在函数上,throw 使用在函数内
2、throws 后面跟的异常类,可以跟多个,用逗号隔开,throw 后面跟的是异常对象。
十一 RuntimeException
首先来看下面这个程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28class Demo { int div(int a, int b) { /*if (b == 0) { throw new ArithmeticException("除数为零了。");//这种情况不需要声明异常 }*/ /*if (b == 0) { throw new Exception("除数为零了。");//这种情况要声明异常 }*/ return a / b; } } public class ExceptionDemo { public static void main(String[] args) { Demo d = new Demo(); int x = d.div(4, 0); System.out.println("x=" + x); System.out.println("over"); } }
上面程序,throw的两种情况会产生不同的效果,为什么呢?
因为ArithmeticException是RuntimeException的子类,
而RuntimeException及其子类在函数中抛出了,函数上就不需要再声明了。
另外,如果你在int div()处throws ArithmeticException,你在main方法那里不需要再try...catch或者throws。
Exception 中有一个特殊的子类异常 —— RuntimeException(运行时异常)。
- 如果在函数内容中抛出该异常,函数上可以不用声明,编译时一样通过。
- 如果在函数上声明了该异常,调用者可以不用进行处理(throwtry catch),编译一样通过。
之所以不用在函数上声明,是因为不需要让调用者处理。
当该异常发生,希望程序停止,因为在运行时,出现了无法继续运算的情况。
希望停止程序后,由程序员对程序代码进行修改或修正。
自定义异常时:如果该异常的发生,无法再继续运行运算,就让自定义异常继承RuntimeException。
对于异常,分为两种:
1、编译时被检测的异常 (如Exception)
2、编译时不被检测的异常 (运行时异常,RuntimeException以及其子类)
十二 异常练习
毕老师用电脑上课。
上课过程中可能出现的问题,例如:电脑蓝屏,电脑冒烟;
对问题进行描述,封装成对象。
可是当冒烟发生后,出现讲课进度无法继续。
出现了讲师的问题,课时计划无法完成。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76class LanPingException extends Exception { LanPingException(String message) { super(message); } } class MaoYanException extends Exception { MaoYanException(String message) { super(message); } } class noPlanException extends Exception { noPlanException(String message) { super(message); } } class Computer { private int state = 3;// 1为正常状态, // Computer(){ // } public void run() throws LanPingException, MaoYanException { if (state == 2) { throw new LanPingException("蓝屏了"); } else if (state == 3) { throw new MaoYanException("冒烟了"); } System.out.println("电脑运行"); } public void reset() { state = 1; System.out.println("电脑重启"); } } class Teacher { private String name; private Computer cmpt; Teacher(String name) { this.name = name; cmpt = new Computer(); } public void prelect() throws noPlanException { try { cmpt.run(); } catch (LanPingException e) { cmpt.reset(); } catch (MaoYanException e) { test(); throw new noPlanException("课时无法继续," + e.getMessage()); } System.out.println(name + "老师上课"); } public void test() { System.out.println("做练习"); } } class Demo { public static void main(String[] args) { Teacher t = new Teacher("毕老师"); try { t.prelect(); } catch (noPlanException e) { System.out.println(e.getMessage()); System.out.println("换老师或者换电脑或者放假"); } } }
最后
以上就是调皮康乃馨最近收集整理的关于6. 面向对象(第九天)一 内部类访问规则二 静态内部类三 内部类定义原则四 匿名内部类五 异常描述六 异常 try - catch七 异常声明throws八 多异常处理九 自定义异常十 throw和throws的区别十一 RuntimeException十二 异常练习的全部内容,更多相关6.内容请搜索靠谱客的其他文章。
发表评论 取消回复