您的位置:首页 > 编程语言 > Java开发

自学 java 笔记 day10(继承下)

2016-03-07 13:41 597 查看
知识点:finally、处理语句其他格式、覆盖时的异常特点、异常总结、包package、包与包之间的访问、导入import

finally

异常的统一出口:

不管try块程序是否异常,也不管哪个catch执行,finally块总会执行。

try语句块或会执行的catch语句块使用了JVM系统退出语句例外;//System.exit(1);

try块必须和 catch块或和finally同在,不能单独存在,二者必须出现一个。

不要在finally中使用return 或throw语句,否则将会导致try、catch中的return或throw失效。

finally代码块只在一种情况下不执行:System.exit(0);

/*
finally代码块:定义一定执行的代码。
通常用于关闭资源。

*/
class FuShuException extends Exception
{
FuShuException(String msg)
{
super(msg);
}
}

class Demo
{
int div(int a,int b)throws FuShuException
{
if(b<0)
throw new FuShuException("除数为负数");
return a/b;
}
}

class  ExceptionDemo5
{
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());
return;
//System.exit(0);//系统,退出。jvm结束。
}
finally
{
System.out.println("finally");//finally中存放的是一定会被执行的代码。
}

System.out.println("over");
}
}

class NoException extends Exception
{
}

public void method()throws NoException
{

连接数据库;

数据操作;//throw new SQLException();

关闭数据库;//该动作,无论数据操作是否成功,一定要关闭资源。

try
{

连接数据库;

数据操作;//throw new SQLException();
}
catch (SQLException e)
{
会对数据库进行异常处理;
throw new NoException();

}
finally
{
关闭数据库;
}

}


第一个格式:
try
{

}
catch ()
{
}

第二个格式:
try
{

}
catch ()
{
}
finally
{

}

第三个格式:
try
{

}
finally
{
}
//记住一点:catch是用于处理异常。如果没有catch就代表异常没有被处理过,如果该异常是检测时异常。那么必须声明。

class Demo
{
public void method()
{
try
{
throw new Exception();
}
finally
{
//关资源。
}
}
}

class
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}


异常在子父类覆盖中的体现

/*
异常在子父类覆盖中的体现;
1,子类在覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法,只能抛出父类的异常或者该异常的子类。
2,如果父类方法抛出多个异常,那么子类在覆盖该方法时,只能抛出父类异常的子集。
3,如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常。
如果子类方法发生了异常。就必须要进行try处理。绝对不能抛。

*/

class AException extends Exception
{
}

class BException extends AException
{
}

class CException extends Exception
{
}

/*
Exception
|--AException
|--BException
|--CException
*/
class Fu
{
void show()throws AException
{

}
}

class Test
{
void function(Fu f)
{
try
{
f.show();
}
catch (AException e)
{

}

}
}

class Zi extends Fu
{
void show()throws CException
{

}
}

class
{
public static void main(String[] args)
{
Test t = new Test();
t.function(new Zi());
}
}


异常的长方形圆形面积test

/*
有一个圆形和长方形。
都可以获取面积。对于面积如果出现非法的数值,视为是获取面积出现问题。
问题通过异常来表示。
现有对这个程序进行基本设计。

*/

class NoValueException extends RuntimeException
{
NoValueException(String message)
{
super(message);
}
}

interface Shape
{
void getArea();
}

class Rec implements Shape
{
private int len,wid;

Rec(int len ,int wid)//throws NoValueException
{
if(len<=0 || wid<=0)
throw new NoValueException("出现非法值");

this.len = len;
this.wid = wid;
}

public void getArea()
{
System.out.println(len*wid);
}
}

class Circle implements Shape
{
private int radius;
public static final double PI = 3.14;

Circle(int radius)
{
if(radius<=0)
throw new NoValueException("非法");
this.radius = radius;
}

public void getArea()
{
System.out.println(radius*radius*PI);
}
}

class  ExceptionTest1
{
public static void main(String[] args)
{

Rec r = new Rec(3,4);
r.getArea();

Circle c = new Circle(-8);

System.out.println("over");

}
}


异常总结~

异常:
是什么?是对问题的描述。将问题进行对象的封装。
------------
异常体系:
Throwable
|--Error
|--Exception
|--RuntimeException

异常体系的特点:异常体系中的所有类以及建立的对象都具备可抛性。
也就是说可以被throw和throws关键字所操作。
只有异常体系具备这个特点。

--------------
throw和throws的用法:

throw定义在函数内,用于抛出异常对象。
throws定义在函数上,用于抛出异常类,可以抛出多个用逗号隔开。

当函数内容有throw抛出异常对象,并未进行try处理。必须要在函数上声明,都在编译失败。
注意,RuntimeException除外。也就说,函数内如果抛出的RuntimeExcpetion异常,函数上可以不用声明。
--------------

如果函数声明了异常,调用者需要进行处理。处理方法可以throws可以try。

异常有两种:
编译时被检测异常
该异常在编译时,如果没有处理(没有抛也没有try),编译失败。
该异常被标识,代表这可以被处理。
运行时异常(编译时不检测)
在编译时,不需要处理,编译器不检查。
该异常的发生,建议不处理,让程序停止。需要对代码进行修正。

--------------
异常处理语句:
try
{
需要被检测的代码;
}
catch ()
{
处理异常的代码;
}
finally
{
一定会执行的代码;
}

有三个结合格式:
1.	try
{

}
catch ()
{
}

2.	try
{

}
finally
{

}

3.	try
{

}
catch ()
{
}
finally
{

}

注意:
1,finally中定义的通常是 关闭资源代码。因为资源必须释放。
2,finally只有一种情况不会执行。当执行到System.exit(0);fianlly不会执行。

--------------

自定义异常:
定义类继承Exception或者RuntimeException
1,为了让该自定义类具备可抛性。
2,让该类具备操作异常的共性方法。

当要定义自定义异常的信息时,可以使用父类已经定义好的功能。
异常异常信息传递给父类的构造函数。
class MyException extends Exception
{
MyException(String message)
{
super(message);
}
}

自定义异常:按照java的面向对象思想,将程序中出现的特有问题进行封装。
--------------

异常的好处:
1,将问题进行封装。
2,将正常流程代码和问题处理代码相分离,方便于阅读。

异常的处理原则:
1,处理方式有两种:try 或者 throws。
2,调用到抛出异常的功能时,抛出几个,就处理几个。
一个try对应多个catch。
3,多个catch,父类的catch放到最下面。
4,catch内,需要定义针对性的处理方式。不要简单的定义printStackTrace,输出语句。
也不要不写。
当捕获到的异常,本功能处理不了时,可以继续在catch中抛出。
try
{
throw new AException();
}
catch (AException e)
{
throw e;
}

如果该异常处理不了,但并不属于该功能出现的异常。
可以将异常转换后,在抛出和该功能相关的异常。

或者异常可以处理,当需要将异常产生的和本功能相关的问题提供出去,
当调用者知道。并处理。也可以将捕获异常处理后,转换新的异常。
try
{
throw new AException();
}
catch (AException e)
{
// 对AException处理。
throw new BException();
}

比如,汇款的例子。

异常的注意事项:
在子父类覆盖时:
1,子类抛出的异常必须是父类的异常的子类或者子集。
2,如果父类或者接口没有异常抛出时,子类覆盖出现异常,只能try不能抛。

参阅
ExceptionTest.java 老师用电脑上课
ExceptionTest1.java 图形面积。

class
{
public static void main(String[] args)
{
int x = 0;
try
{
x = 4;
}
catch ()
{
}
finally
{
System.out.println("x="+x);
}

}
}


习题

注:按Java规范书写程序代码,如果你认为程序有错误,请指出,并说明程序错误原因。

1.
写出程序结果
class Demo
{
public static void func()//throws Exception
{
try
{
throw  new Exception();
}
finally
{
System.out.println("B");
}
}
public static void main(String[] args)
{
try
{
func();
System.out.println("A");
}
catch(Exception e)
{
System.out.println("C");
}
System.out.println("D");
}
}
编译失败:
如果func放上声明了该异常。结果是?B C D

====================================================================
2.
写出程序结果
class Test
{
Test()
{
System.out.println("Test");
}
}
class Demo extends Test
{
Demo()
{
//super();
System.out.println("Demo");
}
public static void main(String[] args)
{
new Demo();
new Test();
}
}
Test
Demo

Test

考的子类的实例化过程。

====================================================================
3.
写出程序结果
interface A{}
class B implements A
{
public String func()
{
return "func";
}
}
class Demo
{
public static void main(String[] args)
{
A a=new B();
System.out.println(a.func());
}
}
编译失败:因为A接口中并未定义func方法。

====================================================================
4.
写出程序结果
class Fu
{
boolean show(char a)
{
System.out.println(a);
return true;
}
}
class Demo extends Fu
{
public static void main(String[] args)
{
int i=0;
Fu f=new Demo();
Demo d=new Demo();
for(f.show('A'); f.show('B')&&(i<2);f.show('C'))
{
i++;
d.show('D');
}
}
boolean show(char a)
{
System.out.println(a);
return false;
}
}
A B

====================================================================
5.
写出程序结果
interface A{}
class B implements A
{
public String test()
{
return "yes";
}
}
class Demo
{
static A get()
{
return new B();
}
public static void main(String[] args)
{
A a=get();
System.out.println(a.test());
}
}
编译失败,因为A接口中没有定义test方法。

====================================================================
6.
写出程序结果:
class Super
{
int i=0;
public Super(String a)
{
System.out.println("A");
i=1;
}
public Super()
{
System.out.println("B");
i+=2;
}
}
class Demo extends Super
{
public Demo(String a)
{
//super();
System.out.println("C");
i=5;
}
public static void main(String[] args)
{
int i=4;
Super d=new Demo("A");
System.out.println(d.i);
}
}
B C 5

====================================================================
7.
interface Inter
{
void show(int a,int b);
void func();
}
class Demo
{
public static void main(String[] args)
{
//补足代码;调用两个函数,要求用匿名内部类
Inter in = new Inter()
{
public void show(int a,int b)
{

}
public void func()
{

}
};

in.show(4,5);
in.func();

}
}

====================================================================
8.
写出程序结果
class TD
{
int y=6;
class Inner
{
static int y=3;
void show()
{
System.out.println(y);
}
}
}
class TC
{
public static void main(String[] args)
{
TD.Inner ti=new TD().new Inner();
ti.show();
}
}
编译失败,非静态内部类中不可以定义静态成员。

内部类中如果定义了静态成员,该内部类必须被静态修饰。

====================================================================
9.
选择题,写出错误答案错误的原因,用单行注释的方式。
class Demo
{
int show(int a,int b){return 0;}
}
下面那些函数可以存在于Demo的子类中。
A.public int show(int a,int b){return 0;}//可以,覆盖。
B.private int show(int a,int b){return 0;}//不可以,权限不够。
C.private int show(int a,long b){return 0;}//可以,和父类不是一个函数。没有覆盖,相当于重载。
D.public short show(int a,int b){return 0;}//不可以,因为该函数不可以和给定函数出现在同一类中,或者子父类中。
E.static int show(int a,int b){return 0;}//不可以,静态只能覆盖静态。

====================================================================
10.
写出this关键字的含义,final有哪些特点?
this:代表本类对象,哪个对象调用this所在函数,this就代表哪个对象。

final:
1,修饰类,变量(成员变量,静态变量,局部变量),函数。
2,修饰的类不可以被继承。
3,修饰的函数不可以被覆盖。
4,修饰的变量是一个常量,只能赋值一次。

====================================================================
11.
写出程序结果:
class Fu
{
int num=4;
void show()
{
System.out.println("showFu");
}
}
class Zi extends Fu
{
int num=5;
void show()
{
System.out.println("showZi");
}
}
class T
{
public static void main(String[] args)
{
Fu f=new Zi();
Zi z=new Zi();
System.out.println(f.num);
System.out.println(z.num);
f.show();
z.show();
}
}

4
5
showZi
showZi

====================================================================
12.
interface A
{
void show();
}
interface B
{
void add(int a,int b);
}
class C implements A,B
{
//程序代码

private int a,b;
//private int sum;
public void add(int a,int b)
{
this.a =a;
this.b = b;

//sum = a+b;
}
public void show()
{
System.out.println(a+b);
//System.out.println(sum);
}

}
class D
{
public static void main(String[] args)
{
C c=new C();
c.add(4,2);
c.show();//通过该函数打印以上两个数的和。
}
}

====================================================================
13.
写出程序结果
class Demo
{
public static void main(String[] args)
{
try
{
showExce();
System.out.println("A");
}
catch(Exception e)
{
System.out.println("B");
}
finally
{
System.out.println("C");
}
System.out.println("D");
}
public static void showExce()throws Exception
{
throw new Exception();
}
}

// B C D

====================================================================
14.
写出程序结果
class Super
{
int i=0;
public Super(String s)
{
i=1;
}
}
class Demo extends Super
{
public Demo(String s)
{

i=2;
}
public static void main(String[] args)
{
Demo d=new Demo("yes");
System.out.println(d.i);
}
}
//编译失败,因为父类中缺少空参数的构造函数。
//或者子类应该通过super语句指定要调用的父类中的构造函数。

====================================================================
15.
写出程序结果
class Super
{
public int get(){return 4;}
}
class Demo15 extends Super
{
public long get(){return 5;}
public static void main(String[] args)
{
Super s=new Demo15();
System.out.println(s.get());
}
}

编译失败,因为子类父类中的get方法没有覆盖。但是子类调用时候不能明确返回的值是什么类型。
所以这样的函数不可以存在子父类中。

====================================================================
16.
写出程序结果:
class Demo
{
public static void func()
{
try
{
throw  new Exception();
System.out.println("A");
}
catch(Exception e)
{
System.out.println("B");
}
}
public static void main(String[] args)
{
try
{
func();
}
catch(Exception e)
{
System.out.println("C");
}
System.out.println("D");
}
}
//编译失败。 因为打印“A”的输出语句执行不到。

记住:throw单独存在,下面不要定义语句,因为执行不到。

====================================================================
17.
class Demo
{
public void func()
{
//位置1;
new  Inner();

}
class Inner{}
public static void main(String[] args)
{
Demo d=new Demo();
// 位置2
new Inner();//不可以,因为主函数是静态的。如果访问inner需要被static修饰。
}
}

A.在位置1写 new Inner();//ok
B.在位置2写 new Inner();
C.在位置2写 new d.Inner();//错误,格式错误。	new new Demo().Inner();
D.在位置2写 new Demo.Inner();//错误,因为inner不是静态的。

====================================================================
18.
写出程序结果
class Exc0 extends Exception{}
class Exc1 extends Exc0{}

class Demo
{
public static void main(String[] args)
{
try
{
throw new Exc1();
}
catch(Exception e)
{
System.out.println("Exception");
}
catch(Exc0 e)
{
System.out.println("Exc0");
}
}
}

//编译失败。
多个catch时,父类的catch要放在下面。

====================================================================
19.

interface Test
{
void func();
}
class Demo
{
public static void main(String[] args)
{
//补足代码;(匿名内部类)

new Demo().show(new Test()
{
public void func(){}
});

}
void show(Test t)
{
t.func();
}
}

====================================================================
20.
写出程序结果
class Test
{
public static String output="";
public static void foo(int i)
{
try
{
if(i==1)
throw new Exception();
output+="1";
}
catch(Exception e)
{
output+="2";
return;
}
finally
{
output+="3";
}
output+="4";
}
public static void main(String args[])
{
foo(0);
System.out.println(output);//134
foo(1);
System.out.println(output); //13423
}
}

====================================================================
21.
建立一个图形接口,声明一个面积函数。圆形和矩形都实现这个接口,并得出两个图形的面积。
注:体现面向对象的特征,对数值进行判断。用异常处理。不合法的数值要出现“这个数值是非法的”提示,不再进行运算。

====================================================================
22.
补足compare函数内的代码,不许添加其他函数。
class Circle
{
private static double pi=3.14;
private double radius;
public Circle(double r)
{
radius=r;
}
public static double compare(Circle[] cir)
{
//程序代码//其实就是在求数组中的最大值。

int max = 0;//double max = cir[0].radius;

for(int x=1; x<cir.length; x++)
{
if(cir[x].radius>cir[max].radius)
max = x;
}

return cir[max].radius;
}
}
class TC
{
public static void main(String[] args)
{
Circle cir[]=new Circle[3];//创建了一个类类型数组。
cir[0]=new Circle(1.0);
cir[1]=new Circle(2.0);
cir[2]=new Circle(4.0);
System.out.println("最大的半径值是:"+Circle.compare(cir));
}
}
====================================================================
23.
写出程序结果
public class Demo
{
private static int j = 0;
private static boolean methodB(int k)
{
j += k;
return true;
}
public static void methodA(int  i)
{
boolean b;
b = i < 10 | methodB (4);
b = i < 10 || methodB (8);
}
public static void main (String args[] )
{
methodA (0);
System.out.println(j); //4
}
}

====================================================================
24.
假如我们在开发一个系统时需要对员工进行建模,员工包含 3 个属性:
姓名、工号以及工资。经理也是员工,除了含有员工的属性外,另为还有一个
奖金属性。请使用继承的思想设计出员工类和经理类。要求类中提供必要的方
法进行属性访问。

====================================================================
25.
在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,
如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),
否则,返回-1。要搜索的字符数组和字符都以参数形式传递传递给该方法,
如果传入的数组为null,应抛出IllegalArgumentException异常。
在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,
例如,字符不存在,字符存在,传入的数组为null等。
getIndex(null,'a');

public int getIndex(char[] arr,char key)
{
if(arr==null)
throw new IllegalArgumentException("数组为null");
for(int x=0; x<arr.length; x++)
{
if(arr[x]==key)
return x;
}
return -1;
}

====================================================================
26.
补足compare函数内的代码,不许添加其他函数。
class Circle
{
private double radius;
public Circle(double r)
{
radius=r;
}
public Circle compare(Circle cir)
{
//程序代码
/*
if(this.radius>cir.radius)
return this;
return cir;
*/

return (this.radius>cir.radius)?this: cir;

}
}
class TC
{
public static void main(String[] args)
{
Circle cir1=new Circle(1.0);
Circle cir2=new Circle(2.0);
Circle cir;
cir=cir1.compare(cir2);
if(cir1==cir)
System.out.println("圆1的半径比较大");
else
System.out.println("圆2的半径比较大");
}
}


包 package

*对类文件进行分类管理

*给类提供多层命名空间

*写在程序文件的第一行

*类名的全称 包名.类名

*包也是一种封装形式

classpath

*给JVM提供的一个环境变量

*指定类或者包所在的路径

*classpath变量值得最后有分号无分号区别

包与包之间的访问

*被访问的包中的类权限必须是public

*类中成员权限:public或者protected

*protected是为其他包中的子类提供的一种权限

四中权限



import

*简化类名

*一个程序文件中只有一个package,可以有多个import

*用来导入包中的类,不导入包中的包

*通常写 import mypack.Demo;

Jar 包

Java的压缩包

*方便携带的项目

*方便于使用,只要在classpath设置jar路径即可

*数据库驱动,SSH框架等都是以jar包体现的



package pack;

/*
为了简化类名的书写,使用一个关键字,import.
import 导入的是包中的类。
建议,不要写通配符 * ,需要用到包中的哪个类,就导入哪个类。

c:\myclass

c:\myclass\packb\DemoA.class
c:\myclass\packb\haha\DemoZ.class

import packb.*;
import packb.haha.*;

建立定包名不要重复,可以使用url来完成定义,url是唯一的。

www.itcast.cn

package cn.itcast.demo
package cn.itcast.test

*/

import packb.haha.hehe.heihei.*;
import packa.*;
import packb.*;

class  PackageDemo
{
public static void main(String[] args)
{

DemoC c = new DemoC();

//		packa.DemoA d = new packa.DemoA();
//		d.show();

//		packb.DemoB d = new packb.DemoB();
//		d.method();
}
}

/*

PackageDemo.java:8: 找不到符号
符号: 类 DemoA
位置: 类 pack.PackageDemo
DemoA d = new DemoA();
^
PackageDemo.java:8: 找不到符号
符号: 类 DemoA
位置: 类 pack.PackageDemo
DemoA d = new DemoA();
^
2 错误

错误原因:类名写错。

因为类名的全名是:包名.类名

PackageDemo.java:8: 软件包 packa 不存在
packa.DemoA d = new packa.DemoA();
^
PackageDemo.java:8: 软件包 packa 不存在
packa.DemoA d = new packa.DemoA();
^
2 错误

错误原因:packa包不在当前目录下
需要设置classpath,告诉jvm去哪里找指定的packa包。

PackageDemo.java:8: packa.DemoA 在 packa 中不是公共的;无法从外部软件包中对其进
行访问
packa.DemoA d = new packa.DemoA();
^
PackageDemo.java:8: packa.DemoA 在 packa 中不是公共的;无法从外部软件包中对其进
行访问
packa.DemoA d = new packa.DemoA();
^
2 错误

错误原因:有了包,范围变大,一个包中的类要被访问,必须要有足够大的权限。
所以被访问的类要被public修饰。

PackageDemo.java:9: show() 在 packa.DemoA 中不是公共的;无法从外部软件包中对其进
行访问
d.show();
^
1 错误

错误原因:类公有后,被访问的成员也要公有才可以被访问。

总结:
包与包之间进行访问,被访问的包中的类以及类中的成员,需要public修饰。

不同包中的子类还可以直接访问父类中被protected权限修饰的成员。

包与包之间可以使用的权限只有两种,public  protected。

public   protected       default     private
同一个类中     ok         ok             ok          ok
同一个包中     ok         ok             ok
子类           ok         ok
不同包中       ok

java.lang : java的核心包 jdk1.2版本以后,该包中的类自动导入。
java.awt: 用于制作图形界面。
java.io:input output  用于操作设备上的数据。
java.util : 这里定义是java的工具类。集合,日期。
java.net:用于网络通讯的。
java.applet:  application  let        server  let    servlet      java server page   jsp

class haha implements Servlet

class hehe extends HttpServlet

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: