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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 吴涛 中级黑马   /  2014-3-4 12:37  /  801 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

学得快,忘得快。做笔记,还是有好处的。。。。。。

1、
写出程序结果
class Demo
{
        public static void func()//throws Exception
        {
                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方法上声明了该异常。结果是?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());
        }
}
编译失败:因为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
===================================================
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());
        }
}
编译失败,因为A接口中没有定义test方法
=========================================
6.
写出程序结果----------------i的范围
class Super
{
        int i=0;
        public Super(String a)
        {
                System.out.println("A");
        }
        public Super()
        {
                System.out.println("B");
                i+=2;
        }
}
class Demo extends Super
{
        public Demo(String a)
        {//super();
                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)
        {
                //补足代码:调用两个函数,要求用匿名内部类
        }
}
class Demo
{
        public static void main(String[] args)
        {
                Inter in =new Inter()
                {
                        public void show(int a,int b)//权限足够大
                        {
                       
                        }
                        public void func()
                        {
                       
                        }
                }//.show();
                in.show();
                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){ 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就代表哪个对象。

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.printlln("showZi");
        }
}
class T
{
        public static void main(String[] args)
        {
                Fu f=new Zi();
                Zi z= new Zi();
                System.out.println(f.num);
                System.out.printlln(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 implemments A,B
{
        //程序代码
}
class D
{
        public static void main(String[] args)
        {
                C c=new C();
                c.add(4,2);
                c.show();//通过该函数打印以上两个数的和。
        }
}

class C implemments A,B
{
        //程序代码
        private int a,b;
        //private int sum;
        public void add(int a,int b)
        {
                this.a = a;
                this.b = b;
                //sum = a+b;
        }
        public void show()
        {
                System.out.println(a+b);
                //System.out.println(sum);
        }
       
}
====================================
13.
写结果
class Demo
{
        public static void main(String[] args)
        {
                try
                {
                        showExce();
                        //throw new Exception;-----一定发生异常,下面输出肯定执行不到----异常----------16题
                        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//15题。。把抛出异常封装起来了。。。有可能出问题
        {
                throw new Exception();
        }
}
B C D
=============================================


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马