黑马程序员------毕老师视频笔记第十天------面向对象(面向对象练习题上)

时间:2022-06-03 12:03:26

---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------

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

1.写出程序结果

class Demo 
{
public static void func()
{
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抛出了一个编译时异常,没有处理。

如果func方法上声明了异常 throws Exception

结果是:

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

结果:编译失败,父类引用不能调用子类特有功能

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

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

这里有一点需要看一下,不是说静态函数只能调用静态成员吗,为什么main中可以调用show?注意了,这里main中并没有直接调用show,而是创建了对象,调用对象的成员函数show。

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

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

结果:编译失败,接口引用调用子类特有功能。

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

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)
{
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)
{
//补足代码;调用两个函数,要求用匿名内部类
}
}

结果:

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(1,2);
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

{

         intshow(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){return0;} //可以,相当于重载

D. public short show(int a, int b){return0;} //不可以,会出现调用不明确

E. static int show(int a, int b){return 0;}//不可以,静态只能覆盖静态

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

10.写出this关键字的含义,final有哪些特点?

答:this代表本类对象,哪个对象调用this所在的函数,this就代表哪个对象

final:

1.修饰类,变量(成员变量、静态变量、局部变量),函数

2.修饰的类不可以被继承

3.修饰的函数不可以被覆盖

4.修饰的变量是一个常量,只能被赋值一次

5.局部内部类只能访问局部中final修饰的变量

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

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
{
//补足程序代码
}
class D
{
public static void main (String [] args)
{
C c = new C();
c.add(4,2);
c.show();//通过该函数打印以上两个数的和
}
}

答案:

interface A
{
void show();
}
interface B
{
void add(int a, int b);
}
class C implements A,B
{
//补足程序代码
private int sum;
public void show()
{
System.out.println(sum);
}
public void add(int a, int b)
{
this.sum = a + b;
}
}
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;
Super(){}
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);
}
}

结果:

2

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

15.写出程序结果

class Super
{
public int get(){return 4;}
}
class Demo extends Super
{
public long get(){return 5;}
public static void main (String [] args)
{
Super s = new Demo();
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的输出语句始终无法执行到的,就像return语句单独使用的时候,下面不能有语句一个道理。

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



---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------