Java 之综合练习

// 练习一: 写出程序结果
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 所属的 A 接口中没有定义 func 方法
    }
}

// 练习二: 写出程序结果
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')) // 条件为假, for 循环不运行
        {
            i++;
            d.show('D');
        }
    }
    boolean show(char a)
    {
        System.out.println(a);
        return false;
    }
}
// 结果: A B  

// 练习三: 写出程序结果
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() 方法
    }
}
// 练习四: 写出程序结果
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 s) // 带参数构造函数
    {
        System.out.println("C");
        i += 5; // 子类中没有定义 i, 直接访问父类的 i
    }
    public static void main(String[] args)
    {
        int i = 4;
        Super d = new Demo("A");
        System.out.println(d.i);
    }
}
// 输出结果: B C 7

// 练习五: 补足代码, 调用两个函数, 要求用匿名内部类
interface Inter
{
    void show(int a, int b);
    void func();
}

class Demo
{
    public static void main(String[] args)
    {
        // 补足代码, 调用两个函数, 要求用匿名内部类
        Inter in = new Inter()  
        {
            public viod show(int a, int b)
            {}
            public void func()
            {}
        };  // 多态

        in.show(3,4);
        in.func();
    }
}

// 练习六: 写出错误答案错误的原因, 用单行注释的方式
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;} // 不可以, 静态只能覆盖静态

// 练习七:
interface A
{
    void show();
}
interface B
{
    void add(int a, int b);
}
class C implements A,B
{
    // 程序代码
    private int x, y;
    public void add(int x, int y)
    {
        this.x = x; // 将局部变量x, y 存储到堆内存中
        this.y = y;
    }

    public void show()
    {
        System.out.println(this.x + this.y); //调用堆内存中的变量
    }
}

class D
{
    public static void main(String[] args)
    {
        C c = new C();
        c.add(4,2);
        c.show(); // 通过该函数打印以上两个数的和
    }
}

// 练习八: 写出程序结果
class Demo
{
    public static void main(String[] args)
    {
        try
        {
            showExe();
            System.out.println("A");
        }
        catch(Exception e)
        {
            System.out.println("B");
        }
        finally
        {
            System.out.println("C");
        }
        System.out.println("D"); // 异常已经被解决, 最后肯定会输出 D
    }
    public static void showExe()throws Exception
    {
        throw new Exception();
    }
}
// 结果: B C D

// 练习九: 写出程序结果
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[] args)
    {
        Demo d = new Demo("yes"); // 父类中没有空参数构造函数, 编译失败
        System.out.println(d.i);
    }
}

class Super
{
    int i = 0;
    public Super()
    {
        i = 1;
    }
    public Super(String s)
    {
        i = 1;
    }
}
class Demo extends Super
{
    public Demo(String s)
    {
        i = 2;
    }
    public static void main(String[] args)
    {
        Super d = new Demo("yes");
        System.out.println(d.i); //此时,结果为 2
    }
}

// 练习十: 写出程序结果
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");
    }
}

// 练习十一:
class Demo
{
    public void func()
    {
        // 位置一
    }

    class Inner{}

    public static void main(String[] args)
    {
        Demo d = new Demo();
        // 位置二
    }
}
A. 在位置1 写 new Inner(); // 可以
B. 在位置2 写 new Inner(); // 不可以, 因为主函数是静态的, 只能调用静态成员, 所以内部类也必须是 static 的
C. 在位置2 写 new d.Inner(); // new new Demo().Inner(); 格式错误, 正确格式: new Demo().new Inner();
D. 在位置2 写 new Demo.Inner(); // 格式正确, 但是 Inner 必须是静态的.

// 练习十二: 写出程序结果
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, output 是静态全局变量
    }
}


_参考资料_ - [JavaSE 基础视频](https://www.bilibili.com/video/av3101108/#page=5)
原文地址:https://www.cnblogs.com/linkworld/p/7452461.html