【277天】我愛刷題系列(36)

叨叨兩句

  1. 履行一個公開的承諾,作一件有價值的事,一直堅持,就會有人看到,加油!html

牛客網——java專項練習016

1

下面哪一個不對?
正確答案: C 你的答案: 空 (錯誤)java

  1. RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.oracle

    1. method is not required to declare in its throws clause any subclasses of RuntimeExeption that might be thrown during the execution of the method but not caughtapp

  2. An RuntimeException is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.函數

  3. NullPointerException is one kind of RuntimeExceptionui

運行時異常: 都是RuntimeException類及其子類異常,如NullPointerException(空指針異常)、IndexOutOfBoundsException(下標越界異常)等,這些異常是不檢查異常,程序中能夠選擇捕獲處理,也能夠不處理。這些異常通常是由程序邏輯錯誤引發的,程序應該從邏輯角度儘量避免這類異常的發生。
       運行時異常的特色是Java編譯器不會檢查它,也就是說,當程序中可能出現這類異常,即便沒有用try-catch語句捕獲它,也沒有用throws子句聲明拋出它,也會編譯經過。 
非運行時異常 (編譯異常): 是RuntimeException之外的異常,類型上都屬於Exception類及其子類。從程序語法角度講是必須進行處理的異常,若是不處理,程序就不能編譯經過。如IOException、SQLException等以及用戶自定義的Exception異常,通常狀況下不自定義檢查異常。

2

下面的對象建立方法中哪些會調用構造方法 ()?
正確答案: A C 你的答案: A (錯誤)spa

  1. new語句建立對象指針

  2. 調用Java.io.ObjectInputStream的readObject方法code

  3. java反射機制使用java.lang.Class或java.lang.reflect.Constructor的newInstance()方法orm

  4. 調用對象的clone()方法

題目的四個選項是構造方法new,序列化對象,反射,克隆分別建立一個對象的方法,,只有new和反射用到了構造方法

3

下列對繼承的說法,正確的是( )
正確答案: A 你的答案: B (錯誤)

  1. 子類能繼承父類的全部成員

  2. 子類繼承父類的非私有方法和狀態

  3. 子類只能繼承父類的public方法和狀態

  4. 子類只能繼承父類的方法

使用反射能夠看出子類是繼承了父類的私有方法的(無論是不是final),只是直接調用父類的私有方法是不能夠的,可是利用反射的方式能夠調用。字段同理。
package work.litao;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
class Parent{
    Parent() {
        System.out.println("調用父類構造方法!");
    }
    private static void staticParent() {
        System.out.println("調用父類靜態方法");
    }
    private final  void finalParent() {
        System.out.println("調用父類final方法");
    }
    private void printParent(){
        System.out.println("調用父類私有方法");
    }
}
class Child extends Parent {
    public void printChild(){
        System.out.println("調用子類公有方法");
    }
}
public class Test {
    public static void main(String[] args) throws Exception {
        //獲取子類
        Class clazz = Class.forName("work.litao.Child");
        //獲得父類
        Class superClass = clazz.getSuperclass();
        //獲得父類非繼承的因此方法
        Method[] methods = superClass.getDeclaredMethods();
        //設置私有方法能夠被訪問
        AccessibleObject.setAccessible(methods,true);
        for (Method m:methods) {
            System.out.println();
            System.out.println("子類調用方法"+m.getName()+"()的調用結果:" );
            m.invoke(new Child());
        }

    }
}

運行結果:

  1. subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. [子類從其父類繼承全部成員(字段,方法和嵌套類)。 構造函數不是成員,因此它們不被子類繼承,可是能夠從子類調用超類的構造函數。]

來自Oracle官方文檔https://docs.oracle.com/javas...

相關文章
相關標籤/搜索