// 【【【【练习题】】】】
//注:按照java规范书写程序代码,如果你认为程序有错误,请支出,并说明程序错误原因
//=============================================================================
/*1
写出程序结果:【a】修正异常抛出后,正确结果是:bcd。
*/
public class D_Exp_test2 {
public static void func() throws Exception //对应【a2】后可编译
{
try{
throw new Exception();//错误【a1】
}
finally{
System.out.println("B");
}
}
public static void main(String[] args) {
try{
func();//【a3】异常抛出
System.out.println("A");
}
catch(Exception e){// 【a4】接收异常
System.out.println("C");
}
System.out.println("D");
}
}
//=============================================================================
/*2
写出结果:
Test
Demo
Test
*/
class Test{
Test(){
System.out.println("Test");
}
public void func() {
// TODO Auto-generated method stub
}
}
class Demo extends Test{
Demo(){
super();
System.out.println("Demo1");
}
public static void main(String []args){
new Demo();
new Test();
}
}
//=============================================================================
/*3.
*写出程序结果:
*编译异常 :A中未定义func()
*
*(在非静态时,多态编译看左边,运行看右边,a调用了func()方法,明显A中是没有的
*想要编译通过那么在接口中还需要定义:String func();)
*/
interface A{
}
class B implements A{
public String func(){
return "func";
}
}
class Demo1{
public static void main(String []args){
A a=new B();//这个叫接口对象指向子类对象
//System.out.println(a.func());//后期注释,为了下面的题通过
}
}
//=============================================================================
/*4
* 写出结果:AB
* 设计到覆盖问题,而循环题中,首先执行a部分,打印,然后执行到b部分,打印,
* 然后返回一个false,这个时候循环结束(条件式中,必须是真才行)
*/
class Fu{
boolean show(char a){
System.out.println(a);
return true;
}
}
class Demo2 extends Fu{
public static void main(String []args){
int i=0;
Fu f=new Demo2();
Demo2 d=new Demo2();
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;
}
}
//=============================================================================
/*5
* 写出程序结果: 编译出错
* A1接口中没有定义test()方法。
*/
interface A1{}
class B1 implements A1{
public String test(){
return "yes";
}
}
class Demo3{
static A1 get(){
return new B1();
}
public static void main(String []ages){
A1 a=get();
/*指向一个get()方法,get()方法中又返回的结果是一个B1子类对象,
而输出的时候又想输出父类对象中的方法,但是父类中没有,所以出错
*/
//System.out.println(a.test());//为了下面的题能继续,我们后期手动注释掉该行
}
}
//=============================================================================
/*6
* 写出程序结果:
* B
* C
* 5
*/
class Super{
int i=0;
public Super(String a){
System.out.println("A");
i=1;
}
public Super(){
System.out.println("B");
i+=2;//i=0+2
}
}
class Demo4 extends Super{
public Demo4(String a){
//这里有一句默认super(); 调用空参数构造函数
System.out.println("C");
i=5;
/*
* new Demo4("A");//的时候进入到该方法
* 而该方法中第一行默认有一句super(); 方法。
*/
}
public static void main(String []args){
int i=4;
Super d=new Demo4("A");
System.out.println(d.i);
}
}
//=============================================================================
/*7
* 补齐代码:调用两个函数,要求匿名内部类
*/
interface Inter{
void show(int a,int b);
void func();
}
class demo5{
public static void main (String []args)
{
//补齐代码
Inter in=new Inter(){
public void show(int a,int b){
System.out.println("老巫婆的真实年纪"+a+b);
}
public void func(){
System.out.println("老巫婆变变变魔鬼身材美女");
}
};//.show(3,8);//匿名构造函数搞定,不过却只能调用一个函数,所以,申明一个变量来接收
in.show(12, 7);
in.func();
}
}
//=============================================================================
/*8
* 写出结果:编译出错:【非静内部态类】中不可以定义静态成员,
* 除非是带final修饰的静态类如【a】
*/
class TD{
int y=6;
class Inner{
//static int y=3;// 原题
static final int y=3;//【a】修正后
void show(){
System.out.println();
}
}
}
class TC{
public static void main(String []args){
TD.Inner ti=new TD().new Inner();
ti.show();
}
}
//=============================================================================
/*9
* 选择题,写出错误答案错误原因,并用单行注释的方式
*/
class Demo6{
int show(int a, int b){return 0;}
}
//下面那些函数可以存在与Demo6的子类中。
/*
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、修饰的变量是一个常量,只能赋值一次
* 5、内部类只能访问局部当中的final变量
*
* static final修饰的成员变量可成为全局常量
*
*/
//=============================================================================
/*写出程序结果:
* 4---5------shouFu------ZiFu
* 成员变量不存在覆盖,随着类中,所以只看左边,f.show();z.show(); 覆盖所以
*/
class Fu1{
int num=4;
void show(){
System.out.println("shouFu");
}
}
class Zi1 extends Fu1{
int num=5;
void show(){
System.out.println("ZiFu");
}
}
class T{
public static void main(String []args){
Fu1 f=new Zi1();
Zi1 z=new Zi1();
System.out.println(f.num);
System.out.println(z.num);
f.show();
z.show();//非静态多态编译看左,运行看右。
}
}
//=============================================================================
/*12
* 补齐代码
*/
interface A2{
void show();
}
interface B2{
void add(int a ,int b);
}
class C2 implements A2,B2{
//根据主函数需求:补齐代码
private int num;
//purivate a,b;//方法二
public void show(){
System.out.println(num);
//System.out.println(a+b);//方法二
}
public void add(int a ,int b){
num=a+b;
//this.a=a;//方法二
//this.b=b;//方法二
}
}
class D2{
public static void main(String []args){
C2 c =new C2();
c.add(4,2);
c.show();//通过该函数打印以上两个数的和
}
}
//=============================================================================
/*13
* 写出程序结果:BCD
* try里调用showExce();的时候抛出异常,程序跳入catch。
*/
class Demo7{
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();
}//把抛出异常封装到方法当中
}
//=============================================================================
/*14
* 写出程序结果:编译失败,因为父类中缺少空参数构造函数或者子类通过super();指定父类中的构造函数
*/
class Super2{
int i=0;
Super2(){}//后期添加,看题时请删除该行。
public Super2(String s){
i=1;
}
}
class Demo8 extends Super2{
public Demo8(String s){
//super();默认进入 空参数构造函数,然后返回子构造函数,所以i=2
i=2;
}
public static void main(String []args){
Demo8 d =new Demo8("yes");
System.out.println(d.i);
}
}
//=============================================================================
/*15
* 写出程序结果:编译失败
* 因为子父类中的get方法没有覆盖,但是,子类调用的时候不能明确返回的值是什么类型
* 所以这样的函数不可以存在 于字符类中
*
*/
class Super3{
public int get(){
return 4;
}
}
class Demo9 extends Super3{
//public long get(){return 5;}//该行出错,后期注释。
public static void main(String []agrs)
{
Super3 s=new Demo9();
System.out.println(s.get());
}
}
//=============================================================================
/*16
* 写出程序结果:编译失败[A]执行不到。
* throw 都是结束语句,后面是不可以跟语句的(return,continue都是结束语句)
* 如果去掉该语句,结果是:B,d
*/
class Demo10{
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");
}
}
//=============================================================================
//17 选择题: A 、D
class Demo11{
public void func(){
//位置1
new Inner3();
}
class Inner3{}
public static void main(String []args){
Demo11 d=new Demo11();
//位置2
}
}
/*
A、在位置1写: new Inner3();//Yes
B、在位置2写: new Inner3();//不可以,以newi主函数是静态的,想调用Inner3必须被static修饰
C、在位置2写: new d.Inner3();//格式错误
D、在位置2写: new Demo11.Inner3();//错误,因为Inner3不是静态的。
*/
//=============================================================================
/*18
* 写出程序结果:编译失败
* 多个catch时,父类的catch要放在下面
* 这段代码不止这点错误。不知道如何表达。
*/
/*
class Exco1 extends Exception{}
class Exco2 extends Exco1{}
class Demo12{
public static void main(String []args){
try{
throw new Exc1();
}
catch(Exception e){
System.out.println("E");
}
catch(Exco0 e){
System.out.println("0");
}
}
}
*/
//=============================================================================
//19
interface Test1{
void func();
}
class Demo13{
void show(Test1 t){
t.func();
}
public static void main(String []args){
//补齐代码代码:(匿名内部类)
new Demo13().show(
new Test1(){
public void func(){
System.out.println("匿名内部类");
}});
/*1、调用showv();>>>>new Demo13().show(null);
*2、用匿名函数替换null即可
*/
}
}
//=============================================================================
/*20 国际考题
* 写出结果 :
* 134
* 13423
* 我写的是
* 134
* 234
* 为什么错呢,
* 1、因为我忽略了output是字符串,是字符串连接
* foo(0)执行完了以后,output="134" 这个时候在执行foo(1) 肯定就变成了 "134234"
* 2、return 语句是结束语句,output+="4";是不执行的,
*/
class Test4{
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"+output);
foo(1);
System.out.println(output);
}
}
//=============================================================================
/*21
* 建立一个图形接口,声明一个面积函数。圆形和矩形都实现这个接口,并得出两个图形的面积。
* 注:体现面向对象特征,对数值判断。用异常处理,不合法的数值要出现”非法“提示,不再进行计算
*/
//=============================================================================
/*22
* 补足compare函数内的代码,不允许添加其他函数。 考的是最大值
*/
class Circle{
private static double pi=3.14;
private double radius;
public int radiusr2;
public Circle(double r){
radius=r;
}
public static double compare(Circle [] cir){
//补齐代码,其实就是求数组中最大值。
int max=0;//double max=cir[0].radius;
for(int i=0;i<cir.length;i++){
if(cir[i].radius>cir[max].radius){//(cir[i].radius>max)
max=i; //max=cir[i].radius
}
}
return max;
}
}
class TC1{
public static void main(String []args){
Circle cir[]=new Circle[3];//对象类型数组 ,里面都是对象,而求的是对象的r。
cir[0]=new Circle(1.0);//赋值
cir[1]=new Circle(2.0);
cir[2]=new Circle(4.0);
System.out.println("最大的半径值是:"+Circle.compare(cir));//求的是最大半径注意
}
}
/*首先main方法中,new的是一个对象类型的数组,里面存的都是对象,要求比较的是对象里的值
* 所以在函数中,写循环的时候一定要注意。注释中是2种方法,都可以。
*/
//=============================================================================
/*23
* 写出运行结果:4
*/
class Demo14{
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);//这句话methodB(8)没有执行,因为b = i < 10为真,后面就不需要执行了。
}
public static void main (String []args){
methodA(0);
System.out.println(j);
}
}
//=============================================================================
/*24
* 假如我们在开发一个系统时,需要对员工进行建模,员工包含3个属性:姓名、工号、工资
* 经理也是员工,除了好友员工的属性外,另外还有一个奖金属性,请使用继承的思想设计出员工类和经理类
* 要求类中提供必要的方法进行属性的访问。
*/
//=============================================================================
/*25
* 在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,
* 如果存在,则返回这个字符在字符串中第一次出现的位置,(序号从0开始计算,)
* 否则,返回-1,要搜索的字符数组和字符都以参数的形式传递给该方法,
* 如果传入的数组为null,应该抛出IllegalArgumentException异常
* 在类main方法中以各种可能出现的情况测试验证方法验证该方法编写是否正确。
* 例如,字符不存在、字符存在、传入数组为null等。
*/
//=============================================================================
/*26
* 补足代码compare 函数内的代码,不添加其他函数
考的是this
*/
class Circle2{
private double radius2;
public Circle2(double r){
radius2=r;
}
public Circle2 compare (Circle2 cir){
// 补足代码
/* 过度思维
if(this.radius2>cir.radius2)
return this;
return cir;
*/
return ((this.radius2>cir.radius2)?this:cir) ;
}
}
class TC2{
public static void main (String []args){
Circle2 cir1=new Circle2(3.0);
Circle2 cir2=new Circle2(5.0);
Circle2 cir;
cir=cir1.compare(cir2);
if(cir1==cir)
System.out.println("1半径大");
else
System.out.println("2半径大");
}
}
黑马官网: http://edu.csdn.net/heima