当描述事物的时候,事物的内部还有事物,该事物用内部类来描述
因为内部事务在使用外部事物的内容,
class Body
{
private class XinZang
{
}
public void show()
{
new XinZang();
}
}
内部类定义在局部的时候
1.不可以被成员修饰符修饰
2.可以直接访问外部类的成员,因为还持有外部类中的引用,但是不可以访问它所在的局部中的变量,只能访问被final修饰的局部变量
class Outer
{
int x=3;
void method()
{
class Inner
{
void function()
{
System.out.println(Outer.this.x);
}
}
new Inner().function();
}
}
class InnerClassDemo03
{
public static void main(String[]args)
{
new Outer().method();
}
}
匿名内部类
1.匿名内部类其实就是内部类的简写格式
2.定义匿名内部类的前提,内部类必须继承一个类或者是实现接口,
3.匿名内部类的格式:new 父类或者是接口(){定义子类的内容}
4.其实匿名内部类就是一个匿名的子类对象,而且这个对象有点胖,而且理解为带内容的对象
5.匿名内部类中定义的方法最好不要超过三个
abstract class AbsDemo
{
astract void show();
}
class Outer
{
int x=3;
// class Inner extends AbsDemo
// {
// void method()
// {
// System.out.println("method"+x);
// }
// }
public void function()
{
//AbsDemo a=new Inner(
;// new Inner().method();
//Inner in=new Inner();
//in.show();
//in.abc();
AbsDemo d=new AbsDemo()
{
void show()
{
System.out.println("x==="+x);
}
void abc()
{
System.out.println("haha");;
}
};
//d.show();
//d.abc();//只能使用父类的方法
new AbsDemo()
{void show()
{
System.out.println("x=="+x);
}
}
void abc()
{
System.out.println("haha");
}.abc();
new AbsDemo()
{void show()
{
System.out.println("x=="+x);
}
}
void abc()
{
System.out.println("haha");
}.show();
new AbsDemo()
{
void show()
{
System.out.println("x="+x);
}
}
}
}
public class InnerClassDemo4
{
public static void main(String[]args)
{
new Outer().function();
}
}
interface Inter
{
void method();
}
class Test
{static class Inner implements Inter
{
public void method()
{
System.out.println("method rum");
}
}
//补足代码,通过匿名内部类
static Inter function()
{
return new Inner()
{public void method()
{
System.out.println("method run");}}
}
}
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();
show(new Inter()
{public void method()
{
System.out.println("method show run");
} });
}
public static void show(Inter in)
{
in.method();
}
}
class InnerTest
{ static class Inner
{
void function()
{
}
}
public static void main(String[]args)
{new Object()
{
public void function()
{
}
}.function()
}
}
异常
异常的体系
异常就是程序在运行的时候出现的不正常的情况
异常的由来:
问题也是现实生活中一个具体的事物,也可以通过JAVA的类形式进行描述,并封装成对象,
其实就是JAVA对不正常的情况进行描述后的对象的体现,
对于问题的划分分为两种,一种是严重的问题,另一种是非严重的问题
对于严重的问题,JAVA通过Error类进行描述
对于Error一般不编写针对性的代码对其进行处理,
对于非严重的,JAVA通过Exception类进行描述,
对于Exception可以使用针对性的处理方式进行处理,
无论Error或者是Exception都具有一些共性的内容
比如,不正常的情况的信息,引发的原因等
Trowable
Error
Exception
class Demo
{
int div(int a,int b)
{
return a/b;
}
}
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");
}
}
异常的处理
JAVA提供了特有的语句进行处理
try
{
}
catch(异常类 变量)
{处理异常的代码:(处理方式)}
finally
{一定会执行的语句;}
//3.对捕获到的异常对象进行常见的方法操作
String getMessage();//获取异常的信息
对于多异常的处理:
1.声明异常的时候,建议声明更为具体的一场,这种处理的可以更具体
2.对方声明几个以撑,就对应有几个catch块,不要对应多语句的catch块
如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面
建立在进行catch处理时,catch中一定要定义具体处理方式,不要简单定义一句
e.printStackTrace();也不要简单的就书写一条输出语句
class Demo
{
int div(int a,int b)
{
return a/b;//new AritchmeticException()
}
}
class ExceptionDemo
{
public static void main(String[]args) //throws Exception;
{
Demo d=new Demo();
try
{
int x=d.div(4, 0) //throws Exception,ArrayIndexOutOfBoundException;
System.out.println("x="+x);
}
catch(Exception e){//Exception e=new ArithmeticException();
System.out.println("初零啦");
System.out.println(e.getMessage());//异常名称: 异常信息
e.printStackTrace();//异常名称 异常信息 异常出现的位置
//其实JVM默认的异常处理机制,就是在调用printStackTrace方法,打印异常的堆栈中的跟踪信息
}
catch (ArrayIndexOutOfBoundException e)
{
System.out.println(e.toString());
}
catch(ArithmeticException e)
{
System.out.println(e.tString());
}
catch(Exception e)
{
System.out.println("hahah:"+e.toString());
}
System.out.println("over");
}
}
因为项目中会出现特有的问题
而这些问题并未被JAVA所描述并封装对象所以对于这些特有的问题可以按照JAVA的对问题的封装的思想,将特有的问题进行自定义的异常封装
自定义异常:
需求:在本程序中,对于除数是-1,也视为是错误的是无法进行运算的,那么就需要对这个问题进行自定义的描述
当在函数的内部出现了trow抛出异常对象,那么就必须要给出对应的处理动作,
要么在内部try catch处理
要么在函数上声明让调用者处理,一般情况下函数内出现异常,函数上需要声明,
发现打印的结果中只有异常的名称,没有异常的信息,因为自定义的异常并未定义信息,
如何来定义异常的信息呢?
因为父类中已经把亦称信息的操作都完成了,所以子类只要在构造时,将异常信息传递给父类,通过super语句,那么就可以直接通过getMessage方法获取自定义的异常信息
自定义异常:
必须是自定义类继承Exception,
继承Exception的原因:
异常体系有一个特点,因为异常类和异常对象都需要被抛出,他们都具备可抛性,
这个可抛性是Trowable这个体系中独有的特点,
只有这个体系中的类和对象才可以被throws和throw操作
class FuShuException extends Exception //getMessage();;;
{
private String msg;
private int value;
FuShuException(String msg)
{
// this.msg=msg;
//
// }
// public String getMessage()
// {
// return msg;
// }
super(msg);
}
FuShuException(String msg,int value)
{
super(msg);
this.value=value;
}
public int getValue()
{
return value;
}
}
class Demo{
int div(int a,int b) throws FuShuException
{
if(b<0)
throw newFuShuException("出现了除数是负数的情况/byfuhu");//手动通过throw关键字抛出一个自定义的的异常对象
return a/b;
}
}
public class ExceptionDemo03
{
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("over");
}}
class Throwable
{private String message;
Throwable(String message)
{
this.message=message;
}
public String getMessage()
{
return message;
}
}
class Exception extends Throwable
{
Exception(String message)
{
super(message);
}
}
//class Person
//{
// String name;
// Person(String name)
// {
// this.name=name;
// }
// public String getName()
// {
// return name;
// }
//}
//class Student extends Person
//{
// Student(String name)
// {
// super(name);
// }
//}
//new Student("lisa").getName();