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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李培根 金牌黑马   /  2012-12-2 20:27  /  1184 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

写出程序运行的结果。
1.
interface A{}
class B implements A{
        public String func(){
                return "func";
        }
}
public class Test {
        public static void main(String[] args) {
                A a = new B();
                System.out.println(a.func());//编译失败,因为a所属的A接口中没有定义func方法。
        }
}
-----------------------------------------------------
2.
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
-----------------------------------------------------
3.
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();//相当于 A a = new B();
                System.out.println(a.test());//编译失败:A接口中没有定义test方法。
        }
}
-----------------------------------------------------
4.
class Super{
        int i = 0;
        public Super(String a){
                System.out.println("A");
                i = 1;
        }
        public Super(){
                System.out.println("B");//3.输出B
                i+=2;//4.i = 2
        }
}
class Demo extends Super{
        public Demo(String a){
                //super();//2.父类空参数构造函数
                System.out.println("C");//5.打印C
                i+=5;//6.i=7;
        }
        public static void main(String[] args){
                int i = 4;
                Super d = new Demo("A");//1.构造函数Demo
                System.out.println(d.i);//7.输出7
        }
}
//打印结果:B  C  7
-----------------------------------------------------
5.
interface Inter{
        void show(int a,int b);
        void func();
}
class Demo{
        public stati void main(String[] args){
                //补足代码:调用两个函数,要求用匿名内部类。
                Inter in = new Inter(){//匿名内部类,其实是匿名子类对象。
                        public void show(int a,int b){}
                        public void func(){}
                }
                in.show(3,4);
                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{
        int show(int a,int b 0){
                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){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:代表对象,当前对象。
        this就是所在函数所属对象的引用。
        简单说:那个对象调用了this所在的函数,this就代表那个对象。               
        this也可以用于在构造函数中调用其他构造函数。
        注意:只能定义在构造函数的第一行,因为初始化动作要先执行。
final关键字:
        1.final是一个修饰符,可以修饰类,方法,变量。
        2.final修饰的类不可以被继承。
        3.final修饰的方法不可以被覆盖。
        4.final修饰的变量是一个常量,只能赋值一次。
-----------------------------------------------------

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

2 个回复

倒序浏览
本帖最后由 李培根 于 2012-12-2 20:29 编辑

11.写出程序结果:
class Fu{
        int num = 4;
        void show(){
                System.out.println("showFu"):
        }
}
class Zi extends Fu{
        int num = 5;
        void show(){
                System.out.println("show Zi");
        }
}
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 Cimplements A,B{
        //程序代码
        private int sum;
        public void add(int a,int b){
                sum = a+b;                       
        }       
        void show(){
                System.out.println(sum);
        }
       
}
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;
        public Super(String s){
                i = 1;
        }
}
class Demo extends Super{
        public Demo(String s){
                i = 2;
        }
        public static void main(String[] void){
                Demo d = new Demo("yes");
                System.out.println(d.i);
        }
}
编译失败,父类中没有空参数的构造函数。
-----------------------------------------------------
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());
        }
}
覆盖失败,编译失败。
-----------------------------------------------------
16.写出程序结果。
class Demo{
        public static void func(){
                try{
                        throw new Excception();
                        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.
class Demo{
        public void func(){
                //位置1:new Inner();
        }
        class Inner{}
        public static void main(String[] args){
                        Demo d = new Demo();
                        //位置2:new Inner(); 不可以。因为主函数是静态的,只能调用静态成员,所以内部类也必须是static的。
                        new d.Inner();//new new Demo().Inner();格式错误  new Demo().new Inner();
                        new Demo.Inner();//格式正确的。但是Inner必须是静态的。                         
        }               
}
A.在位置1写 new Inner();
B.在位置2写 new Inner();
C.在位置2写new d.Inner();
D.在位置2写new Demo.Inner();
-----------------------------------------------------
18.写出程序结果
class Exc0 extends Exception{}
class Exc1 extends exc0{}
class Demo{
        public static void main(String[] args){
                try{
                        throw new Exc1();
                }
                catch(Exception e){//多catch是,父类的catch放在最下面。
                        System.out.println("Exception");
                }
                catch(Exc0 e){
                        System.out.println("Exc0");
                }
        }
}
编译失败。
-----------------------------------------------------
19.
interface Test{
        void func();
}
class Demo{
        public static void main(String[] args){
                //补足代码:(匿名内部类)调用show方法。
                new Demo().show(new Test(){
                        public void func(){}
                });
        }
        void show(Test t){
                t.func();
        }       
}
-----------------------------------------------------
20.写出程序结果
class Test{
        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);//134
                foo(1);
                System.out.println(output);//13423
        }
}
回复 使用道具 举报
总结了下,一直在屋里坐着,看时间长都头疼,不知道大家有什么好的学习办法
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马