A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© keto 中级黑马   /  2015-6-15 14:32  /  657 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

1.写出程序结果
[java] view plaincopy
<span style="font-size:18px;">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");  
      }  
}</span>  

结果是:编译失败,如果func上声明了该异常,
结果是: B
C
D


2.写出程序结果
[java] view plaincopy
<span style="font-size:18px;">class Test  
{  
    Test()  
    {  
        System.out.println("Test");  
    }  
}  
class Demo extends Test  
{  
    Demo()  
    {  
        System.out.println("Demo");  
    }  
    public static void main(String[] args)   
    {  
        new Demo();  
        new Test();  
    }  
}</span>  

结果是:Test
                      Demo
                      Test
考的是子类的实例化过程


3.写出程序结果
[java] view plaincopy
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");  
    }  
}  

结果是:编译失败,因为A接口中并未定义func()方法。


4.写出程序结果
[java] view plaincopy
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



5.写出程序结果
[java] view plaincopy
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);  
    }  
}  

结果是:编译失败,因为A接口中没有定义Test方法


6.写出程序结果
[java] view plaincopy
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.补足代码
[java] view plaincopy
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.funct();  
    }  
}  


8.写出程序结果
[java] view plaincopy
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.选择题,写出错误答案错误的原因
[java] view plaincopy
class Demo  
{  
    int show(int a,int b)  
    {  
        return 0;  
    }  
}  

下面那些函数可以存在于Demo的子类中。
[java] view plaincopy
A. public int show(int a,int b)  
  {  
    return 0;  
  }  

可以,因为覆盖。
[java] view plaincopy
B.private int show(int a,int b)  
  {  
    return 0;  
  }  

不可以,权限不够。
[java] view plaincopy
C.private int show(int a,long b)  
  {  
    return 0;  
  }  

可以,和父类不是一个函数,没有覆盖,相当于重载。
[java] view plaincopy
D.public short show(int a,int b)  
  {  
    return 0;  
  }  
不可以,因为该函数不可以和给定函数出现在同一类中,或子父类中。
[java] view plaincopy
E.static int show(int a,int b)  
{  
    return 0;  
}  
不可以,因为静态只能覆盖静态。


10.写出this关键字的含义,final有哪些特点?
this 用于区分局部变量和成员变量同名的情况 this代表本类对象,简单的说,哪儿对象在调用this所在的函数,                   this就代表哪个对象。
final:最终。作为一个修饰符
1,可以修饰类,方法,变量。
2,被final修饰的类不可以被继承,为了避免被继承,被子类复写功能。
3,被final修饰的方法不可以被复写(覆盖)。
4,被final修饰的变量是一个常量,只能被赋值一次。既可以修饰成员变量又可以修饰局部变量。描述事物时,
               一些数据的出现值是固定的,那么这时为了增强阅读性,都给这些值起个名字,方便于阅读,而这个值是不                需要改变的,所以加上final修饰。作为常量:书写时所有字母都要大写,如果由多个单词组成,单词间通过_              连接。
5,内部类只能访问被final修饰的局部变量


11.写出程序结果
[java] view plaincopy
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.补足代码
[java] view plaincopy
interface A  
{  
    void show();  
}  
interface B  
{  
    void add(int a,int b)  
}  
class C implements A,B  
{  
    private int a,b;  
    public void add(int a,int b)  
    {  
        this.a=a;  
        this.b=b;  
    }  
    public void show()  
    {  
        System.out.println(a+b);  
    }  
}  
class D  
{  
    public static void main(String[] args)   
    {  
        C c=new C();  
        c.add(4,2);  
        c.show();//通过该函数打印以上两个数的和。  
    }  
}  


13.写出程序结果
[java] view plaincopy
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()throw Exception   
    {  
        throw new Exception();  
    }  
}  

结果是:  B
C
D


14.写出程序结果
[java] view plaincopy
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);  
    }  
}  

结果是:编译失败,因为父类中缺少空参数的构造函数或者子类应该通过Super语句指定要调用的父类中的构造   
      函数



15.写出程序结果
[java] view plaincopy
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方法没有覆盖但是子类调用时候不能明确返回值是什么类型的。
                      所以这样的函数不能存在于子父类中。


4 个回复

倒序浏览
     潜水 !!!!
回复 使用道具 举报
javascript:;
回复 使用道具 举报
yu1016468437 来自手机 中级黑马 2015-6-15 17:52:51
板凳
我是来灌水的
回复 使用道具 举报
以后会看懂的 ...
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马