/*
内部类,内部类的访问规则。匿名内部类,异常,自定义异常,RuntimeException
*/
内部类:
class Outer
{
int x = 3;
class Inner//内部类
{
int x = 4;
void function()
{
int x = 6;
System.out.println("inner:"+Outer.this.x);//3
}
}
void method(){
Inner in = new Inner();
in.function();
}
}
class
{
public static void main(String[] args)
{
//Outer out = new Outer();
//out.method();
//访问内部类。
Outer.Inner in = new Outer().new Inner();
in.function();
}
}
内部类的访问规则:
1.内部类可以直接访问外部类中的成员,包括私有。
之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部
类的引用。该引用写法:外部类名.this.x
2.外部类要访问内部类,必须建立内部类对象。
3.内部类也能被私有修饰。 private
访问格式:
当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中,
可以直接建立内部类对象。格式:
Outer.Inner in = new Outer().new Inner();
当内部类定义在外部类的成员位置上,就可以被成员修饰符修饰,
比如private.将内部类在外部类中进行封装。
内部类可以被静态修饰,就具备静态特性。只能直接访问外部类中的静态成员。
在外部其他类中,如何访问静态内部类。new Outer.Inner().function();
在外部其他类中,如何访问静态内部类的非静态成员呢?
new Outer.Inner().function();
在外部其他类中,如何访问静态内部类的静态成员呢?
Outer.Inner.function();
注意,当内部类定义了静态成员,该内部类必须是静态的。
当外部类中的静态方法访问内部类时,内部类也必须是static的。
当描述事物时,事物的内部还有事物,该事物用内部类来描述。
因为内部事务在使用外部事物的内容。
方法中的内部类,定义在局部时。
1.不可以被成员修饰符修饰,比如:static
2.可以直接访问外部类中的成员,因为还持有外部类中的引用。
但是不可以访问局部它所在的局部中的变量,只能访问被final修饰的局部变量。
匿名内部类:
1.匿名内部类其实就是内部类的简写格式。
2.定义匿名内部类的前提:
内部类必须继承一个类或者实现接口。
3.匿名内部类的格式:new 父类或者接口(){定义子类的内容}
4.其实匿名内部类就是一个匿名子类对象。而且这个对象有点胖,带内容的对象。
5.匿名内部类中定义的方法最好不要超过3个,为了简洁。
abstract class AbsDemo
{
abstract void show();
}
class Outer
{
int x = 3;
/*
class Inner extends AbsDemo
{
void show()
{
System.out.println("X:"+x);
}
}*/
void function()
{
//new Inner().show();
new AbsDemo(){
void show(){
System.out.println("x="+x);
}
}.show();
}
}
class InnerClassDemo
{
public static void main(String[] args)
{
new Outer().function();
}
}
练习:补足代码
interface Inter
{
void method();
}
class Test
{
//补足代码,通过匿名内部类
static Inter function(){
return new Inter(){
void method(){
System.out.println("x1111");
}
};
}
}
class InnerClassTest
{
public static void main(String[] args)
{
//Test.function():Test类中有一个静态方法function.
//.method:function这个方法运算后的结果是一个Inter对象。
//因为只有是Inter类型的对象,才能调用method()方法。
Test.function().method();
}
}
异常:就是程序在运行时出现的不正常情况.
异常由来:问题也是现实生活中的一个具体事物,也可以通过java的类进行描述,
并封装成对象。其实就是java对不正常情况进行描述后的对象体现。
对于问题的划分:两种,一种是严重的问题,一种非严重的问题
对于严重的,java通过error类来描述。
对于Error一般不编写针对性的代码对其进行处理。
对于非严重的,java通过Exception类进行描述。
对于Exception 可以使用针对性的处理方式进行处理。
Error或者Exception都具有一些共性内容
比如:不正常情况的信息,引发原因等
Throwable 类:所有错误或异常的超类。Error和Exception
异常体系:
2.异常处理
try{
需要被检测的代码
} catch (异常类 变量){
处理异常的代码,处理方式
} finally{一定会执行语句}//finally可不写
3.对捕获到的异常对象进行常见方法操作
String getMessage();//异常信息
toString();//异常名称和异常信息
e.printStackTrace();//异常名字,信息,位置。
//其实java默认的异常处理机制就是调用 printStactTrace().
异常声明:throws。提高程序的安全性。
多异常的处理。
1.声明异常时,建议声明更为具体的异常,这样处理的可以更具体。
2.一个try可以接多个catch,但是前面catch的异常不能太大。按顺序处理的。
多个catch块中的异常出现继承关系,父类异常catch放在下面。
建议在进行catch处理时,catch中一定要定义具体处理方式。
不要简单的定义一句e.printStackTrace(),也不要简单的一句输出语句。
自定义异常:
因为项目中会出现特有的问题,而这些问题并未被java而所描述并封装对象。
将特有的问题,进行自定义异常封装?
当在函数内部出现了throw抛出异常对象,那么就必须给出对应的处理动作。
要么在内部try catch处理,要么在函数上声明让调用者处理。
一般情况下,函数内出现异常,函数上需要声明。
如何自定义异常信息呢?
因为父类已经把异常信息的操作都完成了
所以子类只要在构造时,将异常信息传递给父类。通过super();
那么就可以直接通过getMessage()获取异常信息了。
自定义异常必须是自定义类继承 Exception.
为什么一定要继承Exception,异常体系有一个特点,因为异常类和异常对象都被抛出。
他们都具备可抛性,这个可抛性是Throwable这个体系中独有特点
只有这个体系中的类和对象才可以被throws和throw操作。
class FuShuException extends Exception
{
/*
private String msg;
FuShuExcepton(String msg)
{
this.msg = msg;
}
public String getMessage()
{
return msg;
}
*/FuShuException(String msg,int value)
{
super(msg);//父类已经做了。
this.value = value;
}
//自定义一个特殊方法
private int value;
public int getValue()
{
return value;
}
}
class Demo
{
int div(int a,int b)throws FuShuException
{
if (b<0)
{
throw new FuShuException("by fushu",b);//手动抛出自定义异常对象
}
return a/b;
}
}
class ExceptionDemo3
{
public static void main(String[] args)
{
Demo d = new Demo();
try
{
int x = d.div(4,1);
System.out.println("x="+x);
}
catch (FuShuException e)
{
System.out.println(e.toString());
System.out.println("除数出现负数了");
System.out.println("负数是"+e.getValue());
}
System.out.println("over");
}
}
throw 和 throws 区别
throws 使用在函数上。throw 使用在函数内
throws 后面跟异常类,可以跟多个,用,隔开。
throw 后面跟异常对象。
RuntimeException 这个异常很特殊。在函数内抛出了,函数上可以不声明。
在函数上声明了该异常,调用者可以不处理。(运行时异常),编译一样通过。
之所以不用在函数上声明,是因为不需要让调用者处理。
当该异常发生,希望程序停止,因为在运行时,出现了无法继续运算的情况,
希望停止程序后,对代码进行修正。
自定义异常时,如果该异常的发生无法再进行运算。
就让自定义异常继承 RuntimeException 。就是让程序停下来。
对于异常分两种;
1.编译时被检测的异常。(必须处理)
2.编译时不被检测的异常(运行时异常,RuntimeException及子类)(可以不声明或不处理)
练习:
/*
毕老师用电脑上课。
开始思考上课中出现的问题。
比如问题是
电脑蓝屏。
电脑冒烟。
要对问题进行描述,封装成对象。
可是当冒烟发生后,出现讲课进度无法继续。
出现了讲师的问题:课时计划无法完成。
*/
class LanPingException extends Exception
{
LanPingException(String message)
{
super(message);
}
}
class MaoYanException extends Exception
{
MaoYanException(String message)
{
super(message);
}
}
class NoPlanException extends Exception
{
NoPlanException(String msg)
{
super(msg);
}
}
class Computer
{
private int state = 3;
public void run()throws LanPingException,MaoYanException
{
if(state==2)
throw new LanPingException("蓝屏了");
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("讲课");
}
public void test()
{
System.out.println("练习");
}
}
class ExceptionTest
{
public static void main(String[] args)
{
Teacher t = new Teacher("毕老师");
try
{
t.prelect();
}
catch (NoPlanException e)
{
System.out.println(e.toString());
System.out.println("换老师或放假");
}
}
day09内部类,内部类的访问规则。匿名内部类,异常,自定义异常,RuntimeException
最新推荐文章于 2025-05-21 10:30:00 发布