黑马程序员_JAVA基础联系题

来源:互联网 发布:windows bat date 编辑:程序博客网 时间:2024/06/07 12:21

------- android培训、java培训、期待与您交流! ----------

1.

public class Joker {

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) {

// TODO: handle exception

System.out.println("C");

}

System.out.println("D");

}

}

//编译失败。因为func抛出了异常而函数上没有申明。

如果func()上抛出了Exception 结果会是BCD。

2.public class Joker extends Test{

Joker() {

super();

System.out.println("Joker");

}

public static void main(String[] args) {

new Joker();

new Test();

}

}

class Test {

Test() {

System.out.println("Test");

}

}

结果是Test,Joker,Test(这题考察的是子类的实例化过程)

3.public class Joker{

public static void main(String[] args) {

A a = new B();

System.out.println(a.func());

}

}

interface A{}

class B implements A {

public String func() {

return "func";

}

}

结果是:编译失败。父类中并没有定义func()方法。

4.

public class Joker extends Fu{

public static void main(String[] args) {

int i = 0;

Fu f = new Joker();

Joker j = new Joker();

for(f.show('A');f.show('B')&&(i < 2);f.show('C')) {

i++;

j.show('D');

}

}

boolean show(char a) {

System.out.println(a);

return false;

}

}

class Fu {

boolean show(char a) {

System.out.println(a);

return true;

}

}

结果为:A.B因为在for里面第一次判断show()的时候由于子类重写了了父类的方法返回false了。所以退出了for循环。

5.

public class Joker extends Fu{

public static void main(String[] args) {

A a = get();

System.out.print(a.test());

}

static A get() {

return new B();

}

}

interface A {}

class B implements A {

public String test() {

return "yes";

}

}//编译失败。

6.

public class Joker extends Super{

public static void main(String[] args) {

int i = 4;

Super d = new Joker("A");

System.out.println(d.i);

}

public Joker(String a) {

System.out.println("C");

i = 5;

}

}

class Super {

int i = 0;

public Super(String a) {

System.out.println("A");

i = 1;

}

public Super() {

System.out.println("B");

i+=2;

}

}//结果为B C 5S执行uper d = new Joker("A");的时候会读Joker父类的构造函数Super();

7.public class Joker{

public static void main(String[] args) {

//补足代码;调用两个函数,要求用匿名内部类.

}

}

interface Inter {

void show(int a ,int b);

void func();

}

答案:

public class Joker{

public static void main(String[] args) {

//补足代码;调用两个函数,要求用匿名内部类.

Inter ii = new Inter() {

public void show(int a,int b) {

}

public void func() {

}

};

ii.show(0, 0);

ii.func();

}

}

interface Inter {

void show(int a ,int b);

void func();

}

8.

public class Joker{

public static String output="";

public static void foo(int i) {

try {

if(i == 1) {

throw new Exception();

}

output += "1";

}catch (Exception e) {

// TODO: handle exception

output += "2";

return;

}finally {

output+="3";

}

output+="4";

}

public static void main(String args[]) {

foo(0);

System.out.println(output);

foo(1);

System.out.println(output);

}

}//结果为123  13423

 

原创粉丝点击