/**
* 多态中成员变量、非静态函数、静态函数的特点演示
* @author hy
*/
class Person
{
String name;
int age=25;//父类对年龄初始化
Person(String name)
{
this.name=name;
}
public static void show()
{
System.out.println("speak");
}
public void study()
{
System.out.println("study");
}
}
class Student extends Person//继承父类
{
int age=18;//子类对年龄初始化
Student(String name)
{
super(name);
}
public static void show()//复写父类的静态函数
{
System.out.println("Student speak");
}
public void study()//复写父类的非静态函数
{
System.out.println("Student study");
}
}
public class DuoTai
{
public static void main(String[] args)
{
Person p=new Student("zhangsan");
System.out.println(p.age);//输出结果为25,父类的属性值
p.show();//输出结果为speak,调用了父类的静态函数
p.study();//输出结果为Student study,调用了子类的非静态函数
}
}
interface Inter
{
void method();
}
class Test
{
//用匿名内部类补足代码
}
public class Demo {
public static void main(String[] args)
{
Test.function().method();
}
}
/**
* 分析:1.Test.function()可知function()为静态方法
* 2.function().method()可知function()返回inter类型的对象
*/
完整代码为:
interface Inter
{
void method();
}
class Test
{
static Inter function()
{
return new Inter()
{
public void method()
{
System.out.println("haha");
}
};
}
}
public class Demo {
public static void main(String[] args)
{
Test.function().method();
}
}
2. 其实异常就是java通过面向对象的思想将问题封装成了对象,用异常类对其进行描述。
3. 不同的问题用不同的类进行具体的描述 。比如角标越界、空指针异常等等。
4. 问题很多,意味着描述的类也很多,将其共性进行向上抽取,形成了异常体系。
(1)Throwable
|---Error //通常出现重大问题如:运行的类不存在或者内存溢出等。一般不编写针对性的代码对其进行处理。
|---Exception //运行时出现的一些情况。可以使用针对性的处理方式进行处理。
|---R untimeException //特殊异常类,抛时不需要声明
(2)特点:1.异常体系中的所有类以及建立的对象都具备可抛性。
2. 也就是说可以被throw和throws关键字所操作。
3.只有异常体系具备这个特点。
三.异常处理
(1)格式:
try{需要被检测的代 码
}
catch(异常类 变量){处理异常的代码;(处理方式)
}
finally{ 一定会执行的语句;
}
注:try可单独与catch组合;try也可单独与finally组合。特殊:当执行到System.exit(0);fianlly不会执行。
(2)throw和throws的用法:
throw定义在函数内,用于抛出异常对象。
throws定义在函数上,用于抛出异常类,可以抛出多个用逗号隔开。
当函数内容有throw抛出异常对象,并未进行try处理。必须要在函数上声明,否则编译失败。
注意:RuntimeException除外。也就说,函数内如果抛出的RuntimeExcpetion异常,函数上可以不用声明。
(3)常见的操作方法:
1. getMessage():获取异常信息,返回字符串。
2. toString():获取异常类名和异常信息,返回字符串。
3. printStackTrace():获取异常类名和异常信息,以及异常出现在程序中的位置,返回值void。
4. printStackTrace(PrintStream s):通常用该方法将异常内容保存在日志文件中,以便查阅。
四.自定义异常
(1)可以自定义出的问题称为自定义异常。
(2)自定义异常时,要么继承Exception,要么继承RuntimeException。
五.异常的处理原则
(1)函数内容如果抛出需要检测的异常,那么函数上必须要声明。否则,必须在函数内用try/catch捕捉,否则编译失败。
(2)如果调用到了声明异常的函数,那么try/catch,要么throws,否则编译失败。
(3)什么时候catch,什么时候throws呢?功能内容可以解决,用catch。 解决不了,用throws告诉调用者,由调用者解决。
(4)一个功能如果抛出了多个异常,那么调用时,必须有对应多个catch进行针对性处理。
内部有几个需要检测的异常,就抛几个异常,抛出几个,就catch几个。
六.异常实例
/**
毕老师用电脑上课。
问题领域中涉及两个对象。
毕老师,电脑。
分析其中的问题。
比如电脑蓝屏,冒烟等。
*/
class LanPingException extends Exception{
LanPingException(String msg){
super(msg);
}
}
class MaoYanException extends Exception{
MaoYanException(String msg){
super(msg);
}
}
class NoPlanException extends Exception{
NoPlanException(String msg){
super(msg);
}
}
class Computer{
private int state = 1;//0 2
public void run() throws LanPingException,MaoYanException{
if(state == 1)
throw new LanPingException("电脑蓝屏啦!");
if(state == 2)
throw new MaoYanException("电脑冒烟啦!");
System. out.println("电脑运行");
}
public void reset(){
state = 0;
System.out.println("电脑重启");
}
}
class Teacher{
private String name ;
private Computer comp ;
Teacher(String name){
this.name = name;
comp = new Computer();
}
public void prelect() throws NoPlanException{
try{
comp.run();
System. out.println(name + "讲课");
} catch(LanPingException e){
System.out.println(e.toString());
comp.reset();
prelect();
} catch(MaoYanException e){
System. out.println(e.toString());
test();
//可以对电脑进行维修
throw new NoPlanException("课时进度无法完成,原因:" + e.getMessage());
}
}
public void test(){
System.out.println("大家练习");
}
}
class ExceptionDemo{
public static void main(String[] args){
Teacher t = new Teacher("毕老师");
try{
t.prelect();
} catch(NoPlanException e){
System.out.println(e.toString() + "......." );
System.out.println("换人");
}
}
}