(40)异常、多态、|与||的分析结果题目
①考查对catch和finally的理解
public static void func()
{
try
{
throw new Exception();/*类内写编译异常对象,要么有trycatch,要么方法上声明,
* 此处都没有,所以编译异常
*/
}
finally
{
System.out.println("B");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
/*Rectangle re=new Rectangle(-3,2);
re.area();
}
*/
try {
func();
System.out.println("A");
}
catch(Exception e){
System.out.println("C");
}
System.out.println("D");
}
//若代码修改后(public static void func()throws Exception
),运行发生异常,执行结果:BCD(注意A不会执行)
}
②继承中构造函数执行问题(子类的实例化过程)
public class Test {
Test(){
System.out.println("Test");
}
}
public class Demo extends Test {
Demo(){
super();//若不写,则默认调用,也可以显式的写出
System.out.println("Demo");
}
public static void main(String[] args) {
new Demo();
new Test();
}
}
结果为:Test Demo Test
③接口指向子类对象
public interface A {
}
public class Demo {
public static void main(String[] args) {
A a=new Test();//接口类型指向子类对象(多态),必须是接口有的方法才能使用
System.out.println(a.func);
}
}
编译失败!! 因为A接口中并未定义func方法,func方法是子类特有的。
③多态父类指向子类对象
覆盖
public class Test {
boolean show(char a) {
System.out.println(a);
return true;
}
}
public class Demo extends Test {
boolean show(char a) {
System.out.println(a);
return false;
}
public static void main(String[] args) {
int i=0;
Test t=new Demo();
Demo d=new Demo();
for(t.show('A');t.show('B')&&(i<2);t.show('C')) {
i++;
d.show('D');
}
}
}
输出:A B
因为t.show('B'),调用的是Demo的方法返回值为false
④接口指向子类对象
public interface A {
}
public class Test implements A {
public String test() {
return "yes";
}
}
public class Demo {
static A get() {
return new Test();
}
public static void main(String[] args) {
A a=get();//A a=new Test();
System.out.println(a.test());
}
}
编译不通过!修改方法:①在接口中定义public String test();方法②将a强制转换为Test对象:Test t=(Test)a; System.out.println(t.test());
⑤子类构造函数默认执行父类的某个构造函数
public class Test implements A {
int i=0;
public Test(String a) {
System.out.println("A");
i=1;
}
public Test() {
System.out.println("B");
i+=2;
}
}public class Demo extends Test {
public Demo(String a) {
//super();默认
System.out.println("C");
i=5;
}
public static void main(String[] args) {
int i=4;//根本没有用到
Test t=new Demo("A");
System.out.println(t.i);
}
}
main中的i和对象中的i不是同一个i
分析:当执行Test t=new Demo("A");创建子类对象时,执行构造函数时,先执行父类的public Test()构造函数,输出B,此时i=2
然后执行子类的构造函数,输出C i=5
输出:B C 5
多态中成员变量无论编译还是运行,都参考左边(引用型变量所属的类)
⑥覆盖和重载
public class Test {
int show(int a,int b) {
return 0;
}
}
子类中可以有以下的方法吗?
public int show(int a,int b) //可以,因为覆盖
{
return 0;
}
private int show(int a,int b)//不可以,子类覆盖父类,必须保证子类函数权限大于等于父类函数权限。此处private权限小
{
return 0;
}
private int show(int a,long b)//可以,重载,没有覆盖
{
return 0;
}
public short show(int a,int b) //The return type is incompatible with Test.show(int, int),既不是重载,也不是
覆盖,造成了二义性
{
return 0;
}
static int show(int a,int b) //不可以,静态只能覆盖静态
{
return 0;
}
⑦
多态成员变量特点:
无论编译和运行,都参考左边(引用型变量所属的类)
public class Test {
int num=4;
}
public class Demo extends Test {
int num=5;
void show () {
System.out.println("showzi");
}
public static void main(String[] args) {
Test t=new Demo();
System.out.println( t.num);
}
}
输出:4
⑦补充代码
public interface A {
public abstract void show();
}
public interface B {
public abstract void add(int a,int b);
}
public class Test implements A,B {
//补充代码,实现Demo中的功能
}
public class Demo {
public static void main(String[] args) {
Test t=new Test();
t.add(3, 4);
t.show();//输出两个数相加的和
}
}
思路一:因为要让一个没有参数的函数输出两个数的和,可以将这两个数设为这个类中的私有变量,那么成员方法都都可以访问私有变量。再由t.add(3,4)可知,应该通过这个函数调用来给这个对象赋值,即替代构造函数的作用。实现代码如下:
private int a,b;
public void show() {
System.out.println(a+b);
}
public void add(int a,int b) {
this.a=a;
this.b=b;
}
思路二:对思路一一个拓展,让sum来存储a+b,设置sum私有成员,在show中打印sum即可。
private int a,b,sum;
public void show() {
System.out.println(sum);
}
public void add(int a,int b) {
this.a=a;
this.b=b;
sum=a+b;
}
⑧子类构造函数执行过程
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语句指定要调用的父类中的构造函数。
⑨对覆盖、重载理解,在继承中不要出现二义性
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方法没有覆盖。但是子类调用时候不能明确返回的值是什么类型。
所以这样的函数不可以存在子父类中。
10.
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 单独存在,下面不要定义语句,因为执行不到。例如:return下不能定义语句
与下面的代码作区分:
class Demo
{
public static void main(String[] args)
{
try
{
showExce(); //调用这个方法可能出现问题,当不出现问题时,可打印“A”
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();
}
}
11.静态变量+异常处理
public class Demo {
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+="4";不会执行
}
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
}
考点①output是String类型,output+=“1”《==》output=output+“1”;是字符串相连,而不是相加
考点②output静态,当调用foo(0),已经将output修改成134了,全局局静态变量修改了。
12.求圆数组的半径的最大值
public class Circle {
private static double pi=3.14;
private double radius;
public Circle(double r) {
radius=r;
}
public static double compare(Circle [] cir) {
//求数组的最大值
double max=cir[0].radius;
for(int i=0;i<cir.length;i++) {
if(cir[i].radius>max)
max=cir[i].radius;
}
return max;
}
}
public class Demo {
public static void main(String[] args) {
Circle cir[]=new Circle[3];
cir[0]=new Circle(1.0);
cir[1]=new Circle(8.0);
cir[2]=new Circle(3.0);
System.out.println("最大的半径为:"+Circle.compare(cir));
}
}
13|和||区别
|:第一个表达式无论真假,第二个表达式均会执行
||:当第一个表达式为真时,第二个表达式不执行。
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(4);
}
public static void main(String[] args) {
methodA(0);
System.out.println(j);
}
}
输出:4