黑马程序员---java基础之day10-11
2013-12-08 22:01
447 查看
-------
android培训、java培训、期待与您交流! ----------
接口:
笔记本电脑使用。
为了扩展笔记本的功能,但日后出现什么功能设备不知道。
定义一个规则,只要日后出现的设备都符合这个规则就可以了。
规则在java中就是接口。
多态:
定义:某一事物的多种存在形式。
·例:动物中的猫,狗。
·猫这个对象对应的类型是猫类型
·猫 x=new 猫();
·同时猫也是动物中的一种,也可以把猫称为动物。
·动物 y=new 猫();
·动物是猫和狗具体事物中抽取出来的父类型。
·父类型引用指向了子类对象
对象的多态性。
class 动物
{}
class 猫extends动物
{}
class 狗extends动物
{}
猫 x = new 猫();
动物 x = new 猫();//一个对象,两种形态。
猫这类事物即具备者猫的形态,又具备着动物的形态。
这就是对象的多态性。
简单说:就是一个对象对应着不同类型.
多态在代码中的体现:
父类或者接口的引用指向其子类的对象。
多态的好处:
提高了代码的扩展性,前期定义的代码可以使用后期的内容。
多态的弊端:
前期定义的内容不能使用(调用)后期子类的特有内容。
多态的前提:
1,必须有关系,继承,实现。
2,要有覆盖。
多态时,
成员的特点:
1,成员变量。
编译时:参考引用型变量所属的类中的是否有调用的成员变量,有,编译通过,没有,编译失败。
运行时:参考引用型变量所属的类中的是否有调用的成员变量,并运行该所属类中的成员变量。
简单说:编译和运行都参考等号的左边。哦了。
作为了解。
2,成员函数(非静态)。
编译时:参考引用型变量所属的类中的是否有调用的函数。有,编译通过,没有,编译失败。
运行时:参考的是对象所属的类中是否有调用的函数。
简单说:编译看左边,运行看右边。
因为成员函数存在覆盖特性。
3,静态函数。
编译时:参考引用型变量所属的类中的是否有调用的静态方法。
运行时:参考引用型变量所属的类中的是否有调用的静态方法。
简单说,编译和运行都看左边。
其实对于静态方法,是不需要对象的。直接用类名调用即可
内部类:
内部类访问特点:
1,内部类可以直接访问外部类中的成员。
2,外部类要访问内部类,必须建立内部类的对象。
一把用于类的设计。
分析事物时,发现该事物描述中还有事物,而且这个事物还在访问被描述事物的内容。
这时就是还有的事物定义成内部类来描述。
匿名内部类:
匿名内部类。就是内部类的简写格式。
必须有前提:
内部类必须继承或者实现一个外部类或者接口。
匿名内部类:其实就是一个匿名子类对象。
格式:new 父类or接口(){子类内容}
通常的使用场景之一:
当函数参数是接口类型时,而且接口中的方法不超过三个。
可以用匿名内部类作为实际参数进行传递
父类初始化完毕后才执行子类的内容
第11天,面向对象:异常
/*
异常:是在运行时期发生的不正常情况。。
在java中用类的形式对不正常情况进行了描述和封装对象。
描述不正常的情况的类,就称为异常类。
以前正常流程代码和问题处理代码相结合,
现在将正常流程代码和问题处理代码分离。提高阅读性.
其实异常就是java通过面向对象的思想将问题封装成了对象.
用异常类对其进行描述。
不同的问题用不同的类进行具体的描述。比如角标越界。空指针等等。
问题很多,意味着描述的类也很多,
将其共性进行向上抽取,形成了异常体系。
最终问题(不正常情况)就分成了两大类。
Throwable:无论是error,还是异常,问题,问题发生就应该可以抛出,让调用者知道并处理。
//该体系的特点就在于Throwable及其所有的子类都具有可抛性。
可抛性到底指的是什么呢?怎么体现可抛性呢?
其实是通过两个关键字来体现的。
throws throw ,凡是可以被这两个关键字所操作的类和对象都具备可抛性.
|--1,一般不可处理的。Error
特点:是由jvm抛出的严重性的问题。
这种问题发生一般不针对性处理。直接修改程序
|--2,可以处理的。Exception
该体系的特点:
子类的后缀名都是用其父类名作为后缀,阅读性很想。
自定义异常演示:
对于自定义的异常必须对其进行捕捉或声明
对于角标是整数不存在,可以用角标越界表示,
对于负数为角标的情况,准备用负数角标异常来表示。
负数角标这种异常在java中并没有定义过。
那就按照java异常的创建思想,面向对象,将负数角标进行自定义描述。并封装成对象。
这种自定义的问题描述成为自定义异常。
注意:如果让一个类称为异常类,必须要继承异常体系,因为只有称为异常体系的子类才有资格具备可抛性。
才可以被两个关键字所操作,throwsthrow
异常的分类:
1,编译时被检测异常:只要是Exception和其子类都是,除了特殊子类RuntimeException体系。
这种问题一旦出现,希望在编译时就进行检测,让这种问题有对应的处理方式。
这样的问题都可以针对性的处理。
2,编译时不检测异常(运行时异常):就是Exception中的RuntimeException和其子类。
这种问题的发生,无法让功能继续,运算无法进行,更多是因为调用者的原因导致的而或者引发了内部状态的改变导致的。
那么这种问题一般不处理,直接编译通过,在运行时,让调用者调用时的程序强制停止,让调用者对代码进行修正。
所以自定义异常时,要么继承Exception。要么继承RuntimeException。
throws 和throw的区别。
1,throws使用在函数上。
throw使用在函数内。
2,throws抛出的是异常类,可以抛出多个,用逗号隔开。
throw抛出的是异常对象。
异常的捕捉:
异常处理的原则:
1,函数内容如果抛出需要检测的异常,那么函数上必须要声明。
否则必须在函数内用trycatch捕捉,否则编译失败。
2,如果调用到了声明异常的函数,要么trycatch要么throws,否则编译失败。
3,什么时候catch,什么时候throws呢?
功能内容可以解决,用catch。
解决不了,用throws告诉调用者,由调用者解决。
4,一个功能如果抛出了多个异常,那么调用时,必须有对应多个catch进行针对性的处理。
内部又几个需要检测的异常,就抛几个异常,抛出几个,就catch几个。
毕老师用电脑上课。
问题领域中涉及两个对象。
毕老师,电脑。
分析其中的问题。
比如电脑蓝屏啦。冒烟啦。
异常的注意事项:
1,子类在覆盖父类方法时,父类的方法如果抛出了异常,
那么子类的方法只能抛出父类的异常或者该异常的子类。
2,如果父类抛出多个异常,那么子类只能抛出父类异常的子集。
简单说:子类覆盖父类只能抛出父类的异常或者子类或者子集。
注意:如果父类的方法没有抛出异常,那么子类覆盖时绝对不能抛,就只能try .
Exception
|--A
|--B
|--C
android培训、java培训、期待与您交流! ----------
接口:
笔记本电脑使用。
为了扩展笔记本的功能,但日后出现什么功能设备不知道。
定义一个规则,只要日后出现的设备都符合这个规则就可以了。
规则在java中就是接口。
interface USB//暴露的规则。 { publicvoid open(); publicvoid close(); } class BookPC { publicstatic void main(String[] args) { useUSB(newUPan());//功能扩展了。 useUSB(newUsbMouse()); } //使用规则。 publicstatic void useUSB(USB u)//接口类型的引用,用于接收(指向)接口的子类对象。//USB u=new UPan(); { if(u!=null) { u.open(); u.close(); } } } //一年后。------------------------------ //实现规则。 //这些设备和电脑的耦合性降低了。 class UPan implements USB { publicvoid open() { System.out.println("upanopen"); } publicvoid close() { System.out.println("upanclose"); } } class UsbMouse implements USB { publicvoid open() { System.out.println("UsbMouseopen"); } publicvoid close() { System.out.println("UsbMouseclose"); } } /* class Mouse { } class BookPC { public static void main(String[] args) { useMouse(new Mouse()); } public static void useMOuse(Mouse m) { m.open(); } public static void useMOuse(Mouse m) { m.open(); } } */
多态:
定义:某一事物的多种存在形式。
·例:动物中的猫,狗。
·猫这个对象对应的类型是猫类型
·猫 x=new 猫();
·同时猫也是动物中的一种,也可以把猫称为动物。
·动物 y=new 猫();
·动物是猫和狗具体事物中抽取出来的父类型。
·父类型引用指向了子类对象
对象的多态性。
class 动物
{}
class 猫extends动物
{}
class 狗extends动物
{}
猫 x = new 猫();
动物 x = new 猫();//一个对象,两种形态。
猫这类事物即具备者猫的形态,又具备着动物的形态。
这就是对象的多态性。
简单说:就是一个对象对应着不同类型.
多态在代码中的体现:
父类或者接口的引用指向其子类的对象。
多态的好处:
提高了代码的扩展性,前期定义的代码可以使用后期的内容。
多态的弊端:
前期定义的内容不能使用(调用)后期子类的特有内容。
多态的前提:
1,必须有关系,继承,实现。
2,要有覆盖。
abstract class Animal { abstractvoid eat(); } class Dog extends Animal { voideat() { System.out.println("啃骨头"); } voidlookHome() { System.out.println("看家"); } } class Cat extends Animal { voideat() { System.out.println("吃鱼"); } voidcatchMouse() { System.out.println("抓老鼠"); } } class Pig extends Animal { voideat() { System.out.println("饲料"); } voidgongDi() { System.out.println("拱地"); } } class DuoTaiDemo { publicstatic void main(String[] args) { // Cat c = new Cat(); // c.eat(); // c.catchMouse(); Animala = new Cat(); //自动类型提升,猫对象提升了动物类型。但是特有功能无法s访问。 //作用就是限制对特有功能的访问。提高扩展性 //专业讲:向上转型。将子类型隐藏。就不用使用子类的特有方法。 // a.eat(); //如果还想用具体动物猫的特有功能。 //你可以将该对象进行向下转型。 // Cat c = (Cat)a;//向下转型的目的是为了使用子类中的特有方法。 // c.eat(); // c.catchMouse(); // 注意:对于转型,自始自终都是子类对象在做着类型的变化。 // Animal a1 = new Dog(); // Cat c1 =(Cat)a1;//ClassCastException /* Cat c = new Cat(); // Dog d = new Dog(); // c.eat(); method(c); // method(d); // method(new Pig()); */ method(new Dog()); } publicstatic void method(Animal a)//Animal a = new Dog(); { a.eat(); if(ainstanceof Cat)//instanceof:用于判断对象的具体类型。只能用于引用数据类型判断 // //通常在向下转型前用于健壮性的判断。 { Catc = (Cat)a; c.catchMouse(); } elseif(a instanceof Dog) { Dogd = (Dog)a; d.lookHome(); } else { } } /* public static void method(Cat c) { c.eat(); } public static void method(Dog d) { } */ } /* 毕老师和毕姥爷的故事。 */ class 毕姥爷 { void讲课() { System.out.println("管理"); } void钓鱼() { System.out.println("钓鱼"); } } class 毕老师 extends 毕姥爷 { void讲课() { System.out.println("Java"); } void看电影() { System.out.println("看电影"); } } class DuoTaiDemo2 { publicstatic void main(String[] args) { // 毕老师 x = new毕老师(); // x.讲课(); // x.看电影(); 毕姥爷 x = new 毕老师(); x.讲课(); x.钓鱼(); 毕老师 y = (毕老师)x;//ClassCastException y.看电影(); } }
多态时,
成员的特点:
1,成员变量。
编译时:参考引用型变量所属的类中的是否有调用的成员变量,有,编译通过,没有,编译失败。
运行时:参考引用型变量所属的类中的是否有调用的成员变量,并运行该所属类中的成员变量。
简单说:编译和运行都参考等号的左边。哦了。
作为了解。
2,成员函数(非静态)。
编译时:参考引用型变量所属的类中的是否有调用的函数。有,编译通过,没有,编译失败。
运行时:参考的是对象所属的类中是否有调用的函数。
简单说:编译看左边,运行看右边。
因为成员函数存在覆盖特性。
3,静态函数。
编译时:参考引用型变量所属的类中的是否有调用的静态方法。
运行时:参考引用型变量所属的类中的是否有调用的静态方法。
简单说,编译和运行都看左边。
其实对于静态方法,是不需要对象的。直接用类名调用即可
class Fu { // int num = 3; voidshow() { System.out.println("fushow"); } staticvoid method() { System.out.println("fustatic method"); } } class Zi extends Fu { // int num = 4; voidshow() { System.out.println("zishow"); } staticvoid method() { System.out.println("zistatic method"); } } class DuoTaiDemo3 { publicstatic void main(String[] args) { Fu.method(); Zi.method(); Fuf = new Zi();// // f.method();//f是父类的引用,调用的是父类的静态方法,静态方法调用不涉及对象,不存在多态性。 // f.show(); // System.out.println(f.num); // Zi z = new Zi(); // System.out.println(z.num); } }
内部类:
内部类访问特点:
1,内部类可以直接访问外部类中的成员。
2,外部类要访问内部类,必须建立内部类的对象。
一把用于类的设计。
分析事物时,发现该事物描述中还有事物,而且这个事物还在访问被描述事物的内容。
这时就是还有的事物定义成内部类来描述。
class Fu { // int num = 3; voidshow() { System.out.println("fushow"); } staticvoid method() { System.out.println("fustatic method"); } } class Zi extends Fu { // int num = 4; voidshow() { System.out.println("zishow"); } staticvoid method() { System.out.println("zistatic method"); } } class DuoTaiDemo3 { publicstatic void main(String[] args) { Fu.method(); Zi.method(); Fuf = new Zi();// // f.method();//f是父类的引用,调用的是父类的静态方法,静态方法调用不涉及对象,不存在多态性。 // f.show(); // System.out.println(f.num); // Zi z = new Zi(); // System.out.println(z.num); } }
匿名内部类:
匿名内部类。就是内部类的简写格式。
必须有前提:
内部类必须继承或者实现一个外部类或者接口。
匿名内部类:其实就是一个匿名子类对象。
格式:new 父类or接口(){子类内容}
abstract class Demo { abstractvoid show(); } class Outer { intnum = 4; /* class Inner extends Demo { void show() { System.out.println("show..."+num); } } */ publicvoid method() { //newInner().show(); newDemo()//匿名内部类。 { voidshow() { System.out.println("show........"+num); } }.show(); } } class InnerClassDemo4 { publicstatic void main(String[] args) { newOuter().method(); } } interface Inter { voidshow1(); voidshow2(); } class Outer { /* class Inner implements Inter { public void show1() { } public void show2() { } } */ publicvoid method() { // Inner in = new Inner(); // in.show1(); // in.show2(); Interin = new Inter() { publicvoid show1() { } publicvoid show2() { } }; in.show1(); in.show2(); } }
通常的使用场景之一:
当函数参数是接口类型时,而且接口中的方法不超过三个。
可以用匿名内部类作为实际参数进行传递
classInnerClassDemo5 { classInner { } publicstatic void main(String[] args) { System.out.println("HelloWorld!"); /* show(new Inter() { public void show1(){} public void show2(){} }); */ // new Inner();//主函数是静态的,不能访问非静态成员 } publicvoid method() { newInner(); } publicstatic void show(Inter in) { in.show1(); in.show2(); } } classOuter { voidmethod() { Objectobj = new Object() { publicvoid show() { System.out.println("showrun"); } }; obj.show();//因为匿名内部类这个子类对象被向上转型为了Object类型。 //这样就不能在使用子类特有的方法了。 } } classInnerClassDemo6 { publicstatic void main(String[] args) { newOuter().method(); } } interface Inter { voidshow(); } class Outer { //补足代码 (建议用匿名内部类) staticInter method() { returnnew Inter() { publicvoid show(){} }; } } /* class haha implementsInter { public void show(){} } */ class InnerClassTest { publicstatic void main(String[] args) { Outer.method().show(); } }
父类初始化完毕后才执行子类的内容
第11天,面向对象:异常
/*
异常:是在运行时期发生的不正常情况。。
在java中用类的形式对不正常情况进行了描述和封装对象。
描述不正常的情况的类,就称为异常类。
以前正常流程代码和问题处理代码相结合,
现在将正常流程代码和问题处理代码分离。提高阅读性.
其实异常就是java通过面向对象的思想将问题封装成了对象.
用异常类对其进行描述。
不同的问题用不同的类进行具体的描述。比如角标越界。空指针等等。
问题很多,意味着描述的类也很多,
将其共性进行向上抽取,形成了异常体系。
最终问题(不正常情况)就分成了两大类。
Throwable:无论是error,还是异常,问题,问题发生就应该可以抛出,让调用者知道并处理。
//该体系的特点就在于Throwable及其所有的子类都具有可抛性。
可抛性到底指的是什么呢?怎么体现可抛性呢?
其实是通过两个关键字来体现的。
throws throw ,凡是可以被这两个关键字所操作的类和对象都具备可抛性.
|--1,一般不可处理的。Error
特点:是由jvm抛出的严重性的问题。
这种问题发生一般不针对性处理。直接修改程序
|--2,可以处理的。Exception
该体系的特点:
子类的后缀名都是用其父类名作为后缀,阅读性很想。
class ExceptionDemo { publicstatic void main(String[] args) { int[]arr = new int[1024*1024*800];//java.lang.OutOfMemoryError: Java heap space // arr = null; // System.out.println(arr[3]); // // // sleep(-5); } publicstatic void sleep2(int time) { if(time<0) { // 处理办法。 // 处理办法。 // 处理办法。 // 处理办法。 // 处理办法。 } if(time>100000) { // 处理办法。 // 处理办法。 // 处理办法。 // 处理办法。 // 处理办法。 // 处理办法。 } System.out.println("我睡。。。"+time); // sleep(-5); } publicstatic void sleep(int time) { if(time<0) { // 抛出new FuTime();//就代码着时间为负的情况,这个对象中会包含着问题的名称,信息,位置等信息。 } if(time>100000) { // 抛出new BigTime(); } System.out.println("我睡。。。"+time); } } /* class FuTime { } class BigTime { } */ /* 数组异常演示 */ class Demo { publicint method(int[] arr,int index) { if(arr==null) thrownew NullPointerException("数组的引用不能为空!"); if(index>=arr.length) { thrownew ArrayIndexOutOfBoundsException("数组的角标越界啦,哥们,你是不是疯了?:"+index); } if(index<0) { thrownew ArrayIndexOutOfBoundsException("数组的角标不能为负数,哥们,你是真疯了!:"+index); } returnarr[index]; } } class ExceptionDemo2 { publicstatic void main(String[] args) { int[]arr = new int[3]; Demod = new Demo(); intnum = d.method(null,-30); System.out.println("num="+num); System.out.println("over"); } }
自定义异常演示:
对于自定义的异常必须对其进行捕捉或声明
对于角标是整数不存在,可以用角标越界表示,
对于负数为角标的情况,准备用负数角标异常来表示。
负数角标这种异常在java中并没有定义过。
那就按照java异常的创建思想,面向对象,将负数角标进行自定义描述。并封装成对象。
这种自定义的问题描述成为自定义异常。
注意:如果让一个类称为异常类,必须要继承异常体系,因为只有称为异常体系的子类才有资格具备可抛性。
才可以被两个关键字所操作,throwsthrow
异常的分类:
1,编译时被检测异常:只要是Exception和其子类都是,除了特殊子类RuntimeException体系。
这种问题一旦出现,希望在编译时就进行检测,让这种问题有对应的处理方式。
这样的问题都可以针对性的处理。
2,编译时不检测异常(运行时异常):就是Exception中的RuntimeException和其子类。
这种问题的发生,无法让功能继续,运算无法进行,更多是因为调用者的原因导致的而或者引发了内部状态的改变导致的。
那么这种问题一般不处理,直接编译通过,在运行时,让调用者调用时的程序强制停止,让调用者对代码进行修正。
所以自定义异常时,要么继承Exception。要么继承RuntimeException。
throws 和throw的区别。
1,throws使用在函数上。
throw使用在函数内。
2,throws抛出的是异常类,可以抛出多个,用逗号隔开。
throw抛出的是异常对象。
class FuShuIndexException extends Exception { FuShuIndexException() {} FuShuIndexException(Stringmsg) { super(msg); } } class Demo { publicint method(int[] arr,int index)//throwsNullPointerException//FuShuIndexException { if(arr==null) thrownew NullPointerException("数组的引用不能为空!"); if(index>=arr.length) { thrownew ArrayIndexOutOfBoundsException("数组的角标越界啦,哥们,你是不是疯了?:"+index); } if(index<0) { thrownew FuShuIndexException("角标变成负数啦!!"); } returnarr[index]; } } class ExceptionDemo3 { publicstatic void main(String[] args) //throws FuShuIndexException { int[]arr = new int[3]; Demod = new Demo(); intnum = d.method(null,-30); System.out.println("num="+num); System.out.println("over"); } } /* class Person { private String name; Person(String name) { this.name = name; } public String getName() { return name; } } class Student extendsPerson { Student(String name) { super(name); } } */
异常的捕捉:
/* 异常处理的捕捉形式: 这是可以对异常进行针对性处理的方式。 具体格式是: try { //需要被检测异常的代码。 } catch(异常类变量)//该变量用于接收发生的异常对象 { //处理异常的代码。 } finally { //一定会被执行的代码。 }
异常处理的原则:
1,函数内容如果抛出需要检测的异常,那么函数上必须要声明。
否则必须在函数内用trycatch捕捉,否则编译失败。
2,如果调用到了声明异常的函数,要么trycatch要么throws,否则编译失败。
3,什么时候catch,什么时候throws呢?
功能内容可以解决,用catch。
解决不了,用throws告诉调用者,由调用者解决。
4,一个功能如果抛出了多个异常,那么调用时,必须有对应多个catch进行针对性的处理。
内部又几个需要检测的异常,就抛几个异常,抛出几个,就catch几个。
class FuShuIndexException extends Exception { FuShuIndexException() {} FuShuIndexException(Stringmsg) { super(msg); } } class Demo { publicint method(int[] arr,int index)//throwsNullPointerException,FuShuIndexException { if(arr==null) thrownew NullPointerException("没有任何数组实体"); if(index<0) thrownew FuShuIndexException(); returnarr[index]; } } class ExceptionDemo4 { publicstatic void main(String[] args) { int[]arr = new int[3]; Demod = new Demo(); try { intnum = d.method(null,-1); System.out.println("num="+num); } catch(NullPointerExceptione) { System.out.println(e.toString()); } catch(FuShuIndexException e) { System.out.println("message:"+e.getMessage()); System.out.println("string:"+e.toString()); e.printStackTrace();//jvm默认的异常处理机制就是调用异常对象的这个方法。 System.out.println("负数角标异常!!!!"); } /* catch(Exception e)//多catch父类的catch放在最下面。 { } */ System.out.println("over"); } } Finally的使用: class Demo { publicint show(int index)throws ArrayIndexOutOfBoundsException { if(index<0) thrownew ArrayIndexOutOfBoundsException("越界啦!!"); int[]arr = new int[3]; returnarr[index]; } } class ExceptionDemo5 { publicstatic void main(String[] args) { Demod = new Demo(); try { intnum = d.show(-1); System.out.println("num="+num); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(e.toString()); // return ; // System.exit(0);//退出jvm。 } finally//通常用于关闭(释放)资源。 { System.out.println("finally"); } System.out.println("over"); } }
/* 连接数据库 查询。Exception 关闭连接。 */ /* try catch finally 代码块组合特点: 1, try catch finally 2, try catch(多个)当没有必要资源需要释放时,可以不用定义finally。 3, try finally 异常无法直接catch处理,但是资源需要关闭。 void show()throwsException { try { //开启资源。 throw new Exception(); } finally { //关闭资源。 } /* catch(Exception e) { } */ } */
毕老师用电脑上课。
问题领域中涉及两个对象。
毕老师,电脑。
分析其中的问题。
比如电脑蓝屏啦。冒烟啦。
class LanPingException extends Exception { LanPingException(Stringmsg) { super(msg); } } class MaoYanException extends Exception { MaoYanException(Stringmsg) { super(msg); } } class NoPlanException extends Exception { NoPlanException(Stringmsg) { super(msg); } } class Computer { privateint state = 2; publicvoid run()throws LanPingException,MaoYanException { if(state==1) thrownew LanPingException("电脑蓝屏啦!!"); if(state==2) thrownew MaoYanException("电脑冒烟啦!!"); System.out.println("电脑运行"); } publicvoid reset() { state= 0; System.out.println("电脑重启"); } } class Teacher { privateString name; privateComputer comp; Teacher(Stringname) { this.name= name; comp= new Computer(); } publicvoid 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 e; thrownew NoPlanException("课时进度无法完成,原因:"+e.getMessage()); } } publicvoid test() { System.out.println("大家练习"); } } class ExceptionTest { publicstatic void main(String[] args) { Teachert = new Teacher("毕老师"); try { t.prelect(); } catch(NoPlanException e) { System.out.println(e.toString()+"......"); System.out.println("换人"); } } } /* class NoAddExceptionextends Exception {} void addData(Datad)throws NoAddException { 连接数据库 try { 添加数据。出现异常SQLException(); } catch(SQLException e) { //处理代码。 throw new NoAddException(); } fianlly { 关闭数据库。 } } */
异常的注意事项:
1,子类在覆盖父类方法时,父类的方法如果抛出了异常,
那么子类的方法只能抛出父类的异常或者该异常的子类。
2,如果父类抛出多个异常,那么子类只能抛出父类异常的子集。
简单说:子类覆盖父类只能抛出父类的异常或者子类或者子集。
注意:如果父类的方法没有抛出异常,那么子类覆盖时绝对不能抛,就只能try .
interface Inter { voidfunction(); } class D implements Inter { publicvoid function()//throws Exception {} } class A extends Exception { } class B extends A { } class C extends Exception { }
Exception
|--A
|--B
|--C
class Fu { voidshow()throws A {} } class Test { voidmethod(Fu f)//Fu f = new Zi(); { try { f.show(); } catch(A a) { } } } class Zi extends Fu { voidshow()throws C { } } class { publicstatic void main(String[] args) { Testt = new Test(); t.show(newZi()); } }
相关文章推荐
- 黑马程序员—11、JAVA基础&网络编程
- 黑马程序员_java基础11-GUI
- 黑马程序员_java基础day10
- 黑马程序员 11 Java基础教学 - 11 - 集合框架总结
- 黑马程序员_Java基础11
- 黑马程序员--Java基础加强--11.【MyEclipse使用快捷键提取方法的要点】【个人总结】
- 黑马程序员__11java基础字符串
- 黑马程序员_java基础学习笔记11_反射
- 黑马程序员—11—java基础:有关线程通信的学习笔记和学习心得体会
- 【黑马程序员】Java基础11:菜单体系
- 黑马程序员_java基础自学11
- 黑马程序员_Java基础[11]_单列设计模式
- 黑马程序员_Java基础_多线程_11
- 黑马程序员-Java基础11交通灯调度系统
- 黑马程序员_java基础笔记(11)...反射
- 黑马程序员-Java基础总结11——IO流(二)
- 黑马程序员_java基础笔记(11)...反射
- 黑马程序员-(11)Java基础之面向对象(六)异常
- 黑马程序员--Java基础--11网络编程
- 黑马程序员 Java基础学习笔记11 异常