注:按照Java规范书写程序代码,如果你认为程序有错误,请指出,并说明错误原因
//=======================================1===================================================
写出程序结果
class Demo
{
publicstatic void func()
try
{
thrownew Exception();//1,抛出了一个编译时异常,既没有catch也没有throws
}
finally
{
System.out.println("B");
}
publicstatic void main(String[] args)
{
try
{
func();
System.out.println("A");//2,try接收到func的异常,直接到catch了,这句不会运行
}
catch(Exception e)
{
System.out.println("C");
}
System.out.println("D");
}
}
编译无法通过
如果func上声明了throws,结果是?
B C D
//=======================================2===================================================
class Test
{
Test()
{
System.out.println("Test");
}
}
class Demo extends Test
{
Demo()
{
//super();这句是隐式的
System.out.println("Demo");
}
publicstatic void main(String[] args)
{
newDemo();
newTest();
}
}
结果
Test
Demo
Test
考察子类的实例化过程
//=======================================3===================================================
interface A
{
}
class B implements A
{
publicString func()
{
return"func";
}
}
class Demo
{
publicstatic void main(String[] args)
{
Aa = new B();
System.out.println(a.func());
}
}
编译失败:A接口中并未定义func方法!!!!自己错过的还没想起来???
//=======================================4===================================================
class Fu
{
boolesnshow(char a)
{
System.out.println(a);
returntrue;
}
}
class Demo extends Fu
{
publicstatic void main(String[] args)
{
inti=0;
Fuf = new Demo();
Demod = new Demo();
for(f.show('A'); f.show('B')&&(i<2); f.show('C'))
{
i++;
d.show('D');
}
}
booleanshow(char a)
{
System.out.println(a);
return false;
}
}
结果
A B
运行时看子类,返回B和false,f.show('B')&&(i<2)不满足,结束
//=======================================5===================================================
interface A
{
}
class B implements A
{
publicString test()
{
return"yes";
}
}
class Demo
{
staticA get()
{
returnnew B();
}
publicstatic void main(String[] args)
{
Aa = get();
System.out.println(a.test());
}
}
编译失败,接口A中没有test方法
//=======================================6==================================================
class Super
{
inti=0;
publicSuper(String a)
{
System.out.println("A");
i=1;
}
publicSuper()
{
System.out.println("B");
i+=2;
}
}
class Demo extends Super
{
publicDemo(String a)
{
//切记:每一个子类的构造函数的第一句都有一个隐式的super();
//所以这里实际上使用的是classFu里面的public Super()函数。所以第一个打印的是B,i+=2;
System.out.println("C");
i=5;
}
publicstatic coid main(String[] args)
{
inti = 4;
Superd=new Demo();
System.out.println(d.i);
}
}
结果
B C 5
//=======================================7==================================================
interface Inter
{
voidshow(int a,int b);
voidfunc();
}
class Demo
{
publicstatic coid main(String[] args)
{
//补足代码,调用两个函数,要求用匿名内部类
Interin = new Inter()
{
publicvoid show(int a,int b)
{}
publicvoid func()
{}
};
in.show(4,5);
in.func;
}
}
//=======================================8==================================================
**********************
class TD
{
inty = 6;
classInner
{
staticint y=3;
voidshow()
{
System.out.println(y);
}
}
}
class TC
{
publicstatic coid main(String[] args)
{
TD.Innerti = new TD().new Inner();
ti.show();
}
}
编译失败:非静态年内部类中不可以定义静态成员
内部类中如果定义了静态成员,该内部类必须被静态修饰
//=======================================9==================================================
选择题写出错误的原因,用单行注释的方式
class Demo
{
intshow(int a,int b){return 0;}
}
下面哪些函数可以存在于Demo的子类中
A. publicint show(int a,int b){return 0;}//可以,覆盖
B. privateint show(int a,int b){return 0;}//不可以,private权限不够
C. privateint show(int a,long b){return 0;}//可以,和父类不是一个函数,int b和long b
D. publicshort show(int a,int b){return 0;}//不可以,同名同输入值,该函数不能和给定函数出现在同意类或者子父类中,JVM不知道用谁
E. staticint show(int a,int b){return 0;}//不可以,静态只能覆盖静态(静态绑定)
//=======================================10=================================================
写出this关键字的含义,final有哪些特点?
this代表本类对象,那个对象调用this所在的函数,this就代表哪个对象
final:
final修饰的类不可以被继承
final修饰的函数不可以被覆盖
final修饰的变量(成员变量,静态变量,局部变量),只能赋值一次
内部类只能局部当中的final修饰变量
//=======================================11=================================================
class Fu
{
intnum = 4;
voidshow()
{
System.out.println("showfu");
}
}
class Zi extends Fu
{
intnum = 5;
voidshow()
{
System.out.println("showzi");
}
}
class T
{
publicstatic coid main(String[] args)
{
Fuf = new Zi();
Ziz = new Zi();
System.out.println(f.num);
System.out.println(z.num);
f.show();
z.show();
}
}
打印结果
4 5 showzi showzi
//=======================================12=================================================
补足程序
interface A
{
voidshow();
}
interface B
{
voidadd(int a,int b);
}
class C implements A,B
{
//程序代码
privateint a;
privateint b;
//privateint sum;
publicvoid show()
{
this.a=a;
this.b=b;
//sum= a+b;
}
publicvoid add()
{
System.out.println(a+n);
//System.out.println(sum);
}
}
class D
{
publicstatic void main(String[] args)
{
Cc = new C();
c.add(4,2);
c.show();//通过该函数打印两数之和
}
}
//=======================================13=================================================
class Demo
{
publicstatic 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");
}
publicstatic void showExce()throws Exception
{
thrownew Exception();
}
}
打印结果
B C D
//=======================================14=================================================
class Super
{
inti = 0;
publicSuper(String s)
{
i=1;
}
}
class Demo extends Super
{
piblicDemo(String s)
{
i=2
}
publicstatic void main(String[] args)
{
Demod = new Demo("yes");
System.out.println(d.i);
}
}
打印结果
编译失败,父类中缺少空参数的构造函数
或者子类中通过super语句指定调用父类中的构造函数
//=======================================15=================================================
class Super
{
publicint get(){return 4;}
}
class Demo15 extends Super
{
publiclong get(){return 5;}
publicstatic void main(String[] args)
{
Supers = new Demo15();
System.out.println(s.get());
}
}
编译失败,子父类中get方法没有覆盖,但是子类调用时不能明确返回值是什么类型
所以这样的函数不可以存在于子父类中
//=======================================16=================================================
class Demo
{
publicstatic void func()
{
try
{
thrownew Exception();//这里这样写表示一定会出问题,那么下面的那句打印就相当于白写了,没用
System.out.ptintln("A");//而13题把抛出异常封装成对对象并声明,表示有可能出问题,意味着打印语句是有可能执行到的,所以能编译
}
catch(Exception e)
{
Sytem.out.println("B");
}
}
publicstatic void main(String[] args)
{
try
{
func();
}
catch(Exception e)
{
System.out.println("C");
}
System.out.println("D");
}
}
//打印结果B C D 错!误!
编译失败,详细解释看func()处的注释
记住:
throw单独存在时,下面不要定义语句,执行不到
//=======================================17=================================================
class Demo
{
publicvoid func()
{
//位置1;
}
classInner
{
}
publicstatic void main(String[] args)
{
Demod=new Demo();
//位置2;
}
}
A. 在位置1写newInner();//可以
B. 在位置2写newInner();//不可以,主函数是静态的,,如果想访问Inner需要被静态修饰
C. 在位置2写newd.Inner();//错误,格式错误, new new Demo().Inner();
D. 在位置2写newDemo.Inner();//错误,Inner不是静态的,不能这么直接访问
//=======================================18=================================================
class Exc0 extends Exception{}
class Exc1 extends Exception{}
class Demo
{
publicstatic void main(String[] args)
{
try
{
thrownew Exc1();
}
catch(Exception e)
{
System.out.println("Exception");
}
catch(Exc0 e)
{
System.out.println("Exc0");
}
}
}
编译失败
多个catch时,父类的catch要放在下面
//=======================================19=================================================
interface Test
{
voidfunc();
}
class Demo
{
publicstatic void main(String[] args)
{
//补足代码(匿名内部类)
newDemo().show(new Test()//show方法不是静态的,建立Demo对象使用
{
publicvoid func();
})
}
voidshow(Test t)
{
t.func();
}
}
//=======================================20=================================================
class Test
{
publicstatic String output=" ";
publicstatic void foo(int i)
{
try
{
if(i==1)
thrownew Exception
output+="1";
}
catch(Exception e)
{
output+="2";
return;
}
finally
{
output+="3";
}
output+="4";
}
publicstatic void main(String[] args)
{
foo(0);
System.out.println(output);
foo(1)
System.out.println(output);
}
}
打印结果
134
13423 //output是共享数据,在foo(1)打印时候,output是在foo(0)执行完的基础上进行的
//=======================================21=================================================
建立一个图形接口,声明一个面积函数。圆形和矩形都实现这个接口,并得出两个图形的面积
注:体现面向对象的特征,对数值进行判断,用异常处理。不合法的数值要出现“这个数值是非法的”提示,不再进行运算
//=======================================22=================================================
补足compare函数内的代码,不许添加其他函数
class Circle
{
privatestatic double pi=3.14;
privatedouble radius;
publicCircle(double r)
{
radius=r;
}
publicstatic double compare(Circle[] cir)
{
//程序代码,其实就是在求数组中的最大值
intmax=0;
for(int i=0; i<cir.length; i++)
{
if(cir[i].radius>cir[i+1].radius)
{
max=i;
}
}
returncir[max].radius
}
}
class TC
{
publicstatic void main(String[] args)
{
Circlecir[]=new Circle[3];
cir[0]=newCircle(1.0);
cir[1]=newCircle(2.0);
cir[2]=newCircle(4.0);
System.out.println("最大的半径值是:"+Circle.compare(cir));
}
}
//=======================================23=================================================
public class Demo
{
privatestatic int j = 0;
privatestatic boolean methodB(int k)
{
j+=k;
rturntrue;
}
publicstatic void methodA(int i)
{
booleanb;
b=i<10| nethodB(4);
b=i<10|| nethoda(8);//记得吗?||左边如果为true,右边就不执行了
}
publicstatic void main(String[] args)
{
methodA(0);
System.out.println(j);
}
}
打印结果
4
//=======================================24=================================================
如果我们在开发一个系统时需要对员工进行建模,员工柏涵3个属性:
姓名,工号以及工资。经理也是员工,除了含有员工属性外,还有奖金属性
请用继承的思想设计出员工类和经理类,
要求提供给必要的方法进行属性访问
//=======================================25=================================================
在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符
如果存在,则返回这个字符在数组中第一次出现的位置(序号从0开始计算)
否则返回-1,要搜索的字符数组和字符都一参数形式传递给该方法
如果传入的数组为null,应抛出IllegalArgumentException异常
在类的main方法中一各种可能出现的情况测试验证该方法编写的是否正确
例如:字符不存在,字符存在,传入数组为null等
加上异常
public int getIndex(char[] arr,char key)
{
for(int x=0; x<arr.length; x++)
{
if(arr[x]==key)
{
returnx;
}
return-1;
}
}
//=======================================26=================================================
补足compare函数内的代码,不许添加其他函数
class Circle
{
privatedouble radius;
publicCircle(double r)
{
radius=r;
}
publicstatic double compare(Circle[] cir)
{
//程序代码
return(this.radius>cir.radius)?this:cir;
}
}
class TC
{
publicstatic void main(String[] args)
{
Circlecir1=new Circle(1.0);
Circlecir2=new Circle(2.0);
Circlecir;
cir=cir1.compare(cir2);
if(cir1==cir)
System.out.println("圆1的半径比较大");
else
System.out.println("圆2的半径比较大");
}
}