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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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

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

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);
        }
}
编译失败,因为父类中缺少空参数的构造函数
或者子类一个通过super语句指定要调用的父类中的构造函数
======================================================
15.
写结果
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方法没有覆盖。但是子类调用时候不确定返回的值什么类型。
所以这样的函数不可以存在于子父类中
==================================================
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"的输出语句执行不到------------13题
记住:throw单独存在时,下面不要定义语句,因为执行不到----像continue,break,return 都这样
===================================================================
17.
class Demo
{
        public void func()
        {
                //位置1;
        }
        class Inner{}
        public static void main(String[] args)
        {
                Demo d=new Demo();
                //位置2
        }
}
A.在位置1写 new Inner();---ok
B.在位置2写 new Inner();------no,因为主函数是静态的,如果要访问,Inner需要被static修饰
C.在位置2写 new d.Inner();-----no。格式错误------new new Demo().Inner();
D.在位置2写 new Demo.Inner();-----no.因为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)
                {
                        System.out.println("Exception");
                }
                catch(Exc0 e)
                {
                        System.out.println("Exc0");
                }
        }
}
编译失败。多个catch时父类的catch要放在下面
================================================
19.
interface Test
{
        void func();
       
}
class Demo
{
        public static void main(String[] args)
        {
                //补足代码:匿名内部类
        }
        void show(Test t)
        {
                t.func();
        }
}

        public static void main(String[] args)
        {
                //补足代码:匿名内部类
                //show();//不允许-----主函数是静态的,show不是静态的
                new Demo().show(new Test()
                {
                        public void 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);
                foo(1);
                System.out.println(output);
        }
}
134
13423-------------==========================================================================================================
22.
补足compare函数内的代码,不须添加其他函数。
class Circle
{
        private static double pi=3.14;
        private double radius;
        public Circle(double r)
        {
                radius = r;
        }
        public static double compare(Circle[] cir)------如果数组没元素呢?
        {
                //程序代码,其实就是在求数组中最大值
               
                int max = 0;//double max = cir[0].radius;
               
                for(int i=1; i<cir.length; i++)
                {
                        if(cir[max].radius<cir[i].radius)
                                max = i;
                }
                return cir[max].radius;
        }
}
class TC
{
        public static void main(String[] args)
        {
                Circle cir[]=new Circle[3];//创建一个类类型数组
                cir[0]=new Circle(1.0);
                cir[1]=new Circle(2.0);
                cir[2]=new Circle(4.0);
                System.out.println("最大半径值是:"+Circle.compare(cir));
        }
}
=================================================
23.
写结果
public class Demo
{
        private static int j = 0;
        private static boolean methodB(int k)
        {
                j += k;
                return true;
        }
        public static void methodA(int i)
        {
                boolean b;
                b = i < 10 | methodB(4);
                b = i < 10 || methodB(8);
        }
        public static void main(String args[])
        {
                methodA (0);
                System.out.printlln(j);
        }
}
4
=====================================================
24.
假如我们在开发一个系统时需要对员工进行建模,员工包含3个属性:
姓名、工号以及工资。经理也是员工,除了包含员工的属性外,另外还有一个奖金属性。
请使用继承的思想设计出员工类和经理类。要求类中提供必要的方法进行属性访问。


======================================================
25.
在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,
如果存在,则返回这个字符在字符数组中的第一个出现的位置(序号从0开始计算),
否则,返回-1,要搜索的字符数组和字符都以参数形式传递给该方法,
如果传入的数组为null,应抛出IllegalArgumentException异常。
在类的main方法中以各种可能出现的情况测试验证该方法编写的是否正确,
例如,,字符不存在,字符存在,传入的数组为null等。

getIndex(null,'a');------

public int getIndex(char[] arr,char key)
{
        if(arr == null)
                throw new IllegalArgumentException("数组为空");
        for(int i=0; i<arr.length; i++)
        {
                if(arr[i] == key)
                        return i;
        }
        return -1;
}
====================================================
26.
补足compare函数内的代码,不许添加其他函数。
class Circle
{
        private double radius;
        public Circle(double r)
        {
                radius = r;
        }
        public Circle compare(Circle cir)
        {
                //程序代码
                /*
                if(this.radius>cir.radius)
                        return this;
                return cir;
                */
               
                return (this.radius>cir.radius)?this:cir;//----不加this行不?
               
        }
}
class TC
{
        public static void main(String args[])
        {
                Circle cir1=new Circle(1.0);
                Circle cir1=new Circle(2.0);
                Circle cir;
                cir=cir1.compare(cir2);
                if(cir1==cir)
                        System.out.println("圆1的半径比较大");
                else
                        System.out.println("圆1的半径比较大");               
        }
}
=======================================================================

评分

参与人数 1技术分 +5 收起 理由
何伟超 + 5 赞一个!

查看全部评分

4 个回复

倒序浏览
不错啊,帮我大忙了,嘿嘿,谢谢
回复 使用道具 举报
看了,学习了
回复 使用道具 举报
每天都来看一看
回复 使用道具 举报
楼主写的代码我运行有点问题啊。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马