📜  Java程序的输出|第 23 集(继承)

📅  最后修改于: 2022-05-13 01:56:11.133000             🧑  作者: Mango

Java程序的输出|第 23 集(继承)

先决条件: Java的继承

1) 以下程序的输出是什么?

Java
public class A extends B
{
    public static String sing()
    {
        return "fa";
    }
    public static void main(String[] args)
    {
        A a = new A();
        B b = new A();
    System.out.println(a.sing() + " " + b.sing());
    }
}
class B
{
    public static String sing()
    {
        return "la";
    }
}


Java
class Building
{
    Building()
    {
        System.out.println("Geek-Building");
    }
    Building(String name)
    {
        this();
        System.out.println("Geek-building: String Constructor" + name);
    }
}
public class House extends Building
{
    House()
    {
        System.out.println("Geek-House ");
    }
    House(String name)
    {
        this();
        System.out.println("Geek-house: String Constructor" + name);
    }
    public static void main(String[] args)
    {
        new House("Geek");
    }
}


Java
class Base
{
    final public void show()
    {
        System.out.println("Base::show() called");
    }
}
class Derived extends Base
{
    public void show()
    {
        System.out.println("Derived::show() called");
    }
}
class Main
{
    public static void main(String[] args)
    {
        Base b = new Derived();
        b.show();
    }
}


Java
public class EH
{
    public static void main(String args[])
    {
        int divisor =0;
        int dividend = 11;
     
        try
        {
            int result=dividend/divisor;
            System.out.println("The result is "+result);
        }
        catch(Exception e)
        {
            System.out.println("An exception occured");
        }
        catch(ArithmeticException ae)
        {
            System.out.println("Division by zero");
        }
        finally
        {
            System.out.println("We are done!");
        }
    }
}


Java
abstract class Vibrate
{
    static String s = "-";
    Vibrate()
    {
        s += "v";
    }
}
public class Echo extends Vibrate
{
    Echo() 
    {
        this(7);
        s += "e";
    }
    Echo(int x)
    {
        s += "e2";
    }
    public static void main(String[] args)
    {
        System.out.print("made " + s + " ");
    }
    static
    {
        Echo e = new Echo();
        System.out.print("block " + s + " ");
    }
}


输出: fa la

解释: B b = new A(); b 是 B 类型的对象,因此 b.sing() 指的是 B 类的方法 sing 参见运行时多态性

2) 以下程序的输出是什么?



Java

class Building
{
    Building()
    {
        System.out.println("Geek-Building");
    }
    Building(String name)
    {
        this();
        System.out.println("Geek-building: String Constructor" + name);
    }
}
public class House extends Building
{
    House()
    {
        System.out.println("Geek-House ");
    }
    House(String name)
    {
        this();
        System.out.println("Geek-house: String Constructor" + name);
    }
    public static void main(String[] args)
    {
        new House("Geek");
    }
}

输出:

Geek-Building
Geek-House 
Geek-house: String ConstructorGeek

说明:构造函数调用它们的超类默认构造函数,它首先执行,并且构造函数可以被重载。
由于 this(),第一个带有一个参数的 House 构造函数被调用,并且流程转移到 house 类的无参数构造函数。从这里开始,由于超类默认构造函数,构建的无参数构造函数被调用。因此显示的顺序。有关详细信息,请参阅 - Java的构造函数

3) 以下程序的输出是什么?

Java

class Base
{
    final public void show()
    {
        System.out.println("Base::show() called");
    }
}
class Derived extends Base
{
    public void show()
    {
        System.out.println("Derived::show() called");
    }
}
class Main
{
    public static void main(String[] args)
    {
        Base b = new Derived();
        b.show();
    }
}

输出:编译器错误

说明:最终方法不能被覆盖。有关详细信息,请参阅 final 关键字。但是,如果我们删除关键字 final,则输出将是

Derived::show() called

.
4) 以下程序的输出是什么?

Java

public class EH
{
    public static void main(String args[])
    {
        int divisor =0;
        int dividend = 11;
     
        try
        {
            int result=dividend/divisor;
            System.out.println("The result is "+result);
        }
        catch(Exception e)
        {
            System.out.println("An exception occured");
        }
        catch(ArithmeticException ae)
        {
            System.out.println("Division by zero");
        }
        finally
        {
            System.out.println("We are done!");
        }
    }
}

输出:编译器错误

说明:显示异常 ArithmeticException 已被捕获。终端
catch 块的顺序很重要 更具体/子类 (ArithmeticException) 需要更早出现,更通用/超类 (Exception) 需要稍后编写。如果算术异常和一般异常的顺序互换,程序将正确执行。

5) 以下程序的输出是什么?

Java

abstract class Vibrate
{
    static String s = "-";
    Vibrate()
    {
        s += "v";
    }
}
public class Echo extends Vibrate
{
    Echo() 
    {
        this(7);
        s += "e";
    }
    Echo(int x)
    {
        s += "e2";
    }
    public static void main(String[] args)
    {
        System.out.print("made " + s + " ");
    }
    static
    {
        Echo e = new Echo();
        System.out.print("block " + s + " ");
    }
}

输出: block -ve2e made -ve2e

说明:静态初始化块是创建 Echo 实例的唯一地方。然后创建 Echo 实例,Echos no-arg 构造函数调用其 1-arg 构造函数,然后调用 Vibrates 构造函数(然后秘密调用 Objects 构造函数)。那时,各种构造函数都会执行,从 Objects 构造函数开始,然后返回到 Echos 无参数构造函数。查看静态关键字