【277天】我爱刷题系列(36)

叨叨两句

  1. 履行一个公开的承诺,做一件有价值的事,一直坚持,就会有人看到,加油!

牛客网——java专项练习016

1

下面哪个不对?
正确答案: C 你的答案: 空 (错误)

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

    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 caught

  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 RuntimeException


运行时异常: 都是RuntimeException类及其子类异常,如NullPointerException(空指针异常)、IndexOutOfBoundsException(下标越界异常)等,这些异常是不检查异常,程序中可以选择捕获处理,也可以不处理。这些异常一般是由程序逻辑错误引起的,程序应该从逻辑角度尽可能避免这类异常的发生。
       运行时异常的特点是Java编译器不会检查它,也就是说,当程序中可能出现这类异常,即使没有用try-catch语句捕获它,也没有用throws子句声明抛出它,也会编译通过。 
非运行时异常 (编译异常): 是RuntimeException以外的异常,类型上都属于Exception类及其子类。从程序语法角度讲是必须进行处理的异常,如果不处理,程序就不能编译通过。如IOException、SQLException等以及用户自定义的Exception异常,一般情况下不自定义检查异常。

2

下面的对象创建方法中哪些会调用构造方法 ()?
正确答案: A C 你的答案: A (错误)

  1. new语句创建对象

  2. 调用Java.io.ObjectInputStream的readObject方法

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

  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...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值