JavaSE--异常--练习4

第四题:
package exception;

/**

  • Created by zengjx on 2019/4/26.
    */

class FuTest4
{
boolean show(char a)
{
System.out.println(a);
System.out.println(“父类方法被调用”);
return true;
}
}

public class ExceptionTest4_1 extends FuTest4 {
public static void main(String[] args)
{
int i=0;
FuTest4 f=new ExceptionTest4_1();
ExceptionTest4_1 d=new ExceptionTest4_1();
for(f.show(‘A’); f.show(‘B’)&&(i<2);f.show(‘C’))
{
i++;
System.out.println("----"+i);
d.show(‘D’);
}
}
boolean show(char a)//覆盖了父类方法
{
System.out.println(a);
System.out.println(“子类方法被调用”);
return false;
}
}
// 输出结果:
A
子类方法被调用
B
子类方法被调用

for(f.show(‘A’); f.show(‘B’)&&(i<2);f.show(‘C’)) f.show(‘B’) 执行后返回false 不会进入循环体
5.
package exception;

/**

  • Created by zengjx on 2019/4/26.
    */
    interface A5{
    // public String test();
    }
    class B5 implements A5{
    public String test(){
    return “yes”;
    }
    }
    public class ExceptionTest5 {
    static A5 get(){
    return new B5();
    }
    public static void main(String[] args)
    {
    A5 a=get();
    System.out.println(a.test())
    }
    }

结果:
;//编译错误如果 A5没有 定义方法 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();

}

}
内部类中如果定义了静态成员,该内部类必须被静态修饰。
http://www.cnblogs.com/aademeng/articles/6192954.html
内部类: 定义在一个类内部的类。
外部类:包含内部类的类
内部类访问权限:public protected private
可以声明为abstact 提供给其他内部类或者外部类继承与拓展。
声明为final static
实现特定接口
外部类可以访问内部类的所有方法与属性

创建内部类方法:
OutClass out= new OutClass(); 外部类实例.new 内部类
OutClass.Inner inner =out.new OutClassInner();

OutClass.StaticInner inner = new OutClass.StaticInner();//静态内部类
内部类的this:
内部类的this 指内部类本身。

外部类访问内部类
内部类类似于 外部类的属性。
外部类名.this.xxx’的形式访问外部类的属性与方法

内部类向上转型:
内部类也可以和普通类一样拥有向上转型的特性。将内部类向上转型为基类型,尤其是接口时,内部类就有了用武之地。
如果内部类是private的,只可以被它的外部类问,从而完全隐藏实现的细节。
package inner;
import java.io.OutputStream;
/**

  • Created by zengjx on 2019/4/26.
    */
    interface Out{
    void show();
    }
    public class InnerDemo2 {
    class In1 implements Out{

     @Override
     public void show() {
         System.out.println("Inner1 ");
     }
    

    }
    class In2 implements Out{
    @Override
    public void show() {
    System.out.println("Inner2 ");
    }
    }
    public static void main(String[] args){
    InnerDemo2 innerDemo2 =new InnerDemo2();
    innerDemo2.show();
    }
    public void show(){
    Out n1 =new In1();
    n1.show();
    Out n2=new In2();
    n2.show();
    }
    }
    //输出结果:
    Inner1
    Inner2

方法内部定义类

方法内创建的类(注意方法中也能定义类),不能加访问修饰符。
另外,方法内部的类也不是在调用方法时才会创建的,它们一样也被事先编译了。

public void show_2(){

 class   In3{
     public   void   show(){
         System.out.println("方法内部定义类 ");

     }
 }
 In3   in3  =new In3();
 in3.show();

}
静态内部类 (嵌套类) 前面加上static 修饰符

[1] 要创建静态内部类对象并不需要外围的对象。
[2] 不能从静态内部类访问非静态的外部类对象
不能够从静态内部类的对象中访问外部类的非静态成员。
(可以通过类名访问 InnerDemo1.out_static_num)
[3] 普通内部类不能有static 变量或者方法 // private static int inner_static_num= 10;

[4]在创建静态内部类时不需要将静态内部类的实例绑定在外部类的实例上。普通非静态内部类的 对象是依附在外部类对象之中的,要在一个外部类中定义一个静态的内部类,不需要利用关键字new来创建内部类的实例。
静态类和方法只属于类本身,并不属于 该类的对象,更不属于其他外部类的对象。
[5] 内部类:内部类也会产生.class 文件
名称:外围类$内部类

package inner;
/**

  • Created by zengjx on 2019/4/26.
    */
    public class InnerDemo1 {
    private int out_num=2;
    class Inner {//非静态内部类
    private int num =10;
    public void inner_show(){
    System.out.println(“inner_show”);
    }
    private void inner_private_show(){
    System.out.println(“inner_private_show”);
    }
    private void inner_call_out(){
    System.out.println("内部类访问外部类变量 "+InnerDemo1.this.out_num);
    }
    }
    static class Static_Inner{//静态内部类
    public void static_inner_show(){
    System.out.println(“static inner show”);
    }
    }
    static class Static_Inner2{
    static int num=10;//包含静态变量需要定义为静态内部类
    public static void static_inner_show(){
    System.out.println(“static inner show 2”);
    }
    }
    public static void main(String[] args){
    InnerDemo1 innerDemo1 =new InnerDemo1();
    innerDemo1.out_show();
    // 静态内部类
    InnerDemo1.Static_Inner inner =new InnerDemo1.Static_Inner();
    inner.static_inner_show();
    InnerDemo1.Static_Inner2 inner2 =new InnerDemo1.Static_Inner2();
    inner2.static_inner_show();
    InnerDemo1 innerDemo3=new InnerDemo1();
    InnerDemo1.Inner inner3= innerDemo1.new Inner();
    System.out.println("创建实例:外部类实例.new 内部类 - "+inner3.num);
    }
    public void out_show(){
    InnerDemo1.Inner inner =new InnerDemo1.Inner();
    inner.inner_show();
    inner.inner_private_show();//调用内部类的静态方法
    System.out.println(inner.num);
    inner.inner_call_out();//内部类访问外部类变量
    }
    }

====================================================================
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单独存在,下面不要定义语句,因为执行不到。
Error:(13, 13) java: 无法访问的语句

====================================================================
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不是静态的。
考点:静态内部类 普通法内部类
普通内部类不能在静态方法内调用要静态内部类

写出程序结果
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
}
}
finally :只有在 有调用 System.ext(0); 才不会执行
foo(1)有 return 所以 output+=“4”; 不会执行。

====================================================================
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));
}
}

写出程序结果
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的半径比较大”);
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值