【Java练习题】Java程序的输出 | 第十五套(内部类)

简介: 【Java练习题】Java程序的输出 | 第十五套(内部类)

难度级别: 困难

程序

程序一


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

public class Outer
{
  public static int temp1 = 1;
  private static int temp2 = 2;
  public int temp3 = 3;
  private int temp4 = 4;
  public static class Inner
  {
    private static int temp5 = 5;
    private static int getSum()
    {
      return (temp1 + temp2 + temp3 + temp4 + temp5);
    }
  }
  public static void main(String[] args)
  {
    Outer.Inner obj = new Outer.Inner();
    System.out.println(obj.getSum());
  }
}

a) 15

b) 9

c) 5

d) 编译错误



程序二


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

public class Outer
{
  private static int data = 10;
  private static int LocalClass()
  {
    class Inner
    {
      public int data = 20;
      private int getData()
      {
        return data;
      }
    };
    Inner inner = new Inner();
    return inner.getData();
  }
  public static void main(String[] args)
  {
    System.out.println(data * LocalClass());
  }
}

a) 编译错误

b) 运行时错误

c) 200

d) 以上都不是

点此跳转到答案


程序三


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

interface Anonymous
{
  public int getValue();
}
public class Outer
{
  private int data = 15;
  public static void main(String[] args)
  {
    Anonymous inner = new Anonymous()
        {
          int data = 5;
          public int getValue()
          {
            return data;
          }
          public int getData()
          {
            return data;
          }
        };
    Outer outer = new Outer();
    System.out.println(inner.getValue() + inner.getData() + outer.data);
  }
}

a) 25

b) 编译错误

c) 20

d) 运行时错误

点此跳转到答案


程序四


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

public class Outer
{
  private int data = 10;
  class Inner
  {
    private int data = 20;
    private int getData()
    {
      return data;
    }
    public void main(String[] args)
    {
      Inner inner = new Inner();
      System.out.println(inner.getData());
    }
  }
  private int getData()
  {
    return data;
  }
  public static void main(String[] args)
  {
    Outer outer = new Outer();
    Outer.Inner inner = outer.new Inner();
    System.out.printf("%d", outer.getData());
    inner.main(args);
  }
}

a) 2010

b) 1020

c) 编译错误

d) 这些都不是

点此跳转到答案


程序五


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

interface OuterInterface
{
  public void InnerMethod();
  public interface InnerInterface
  {
    public void InnerMethod();
  }
}
public class Outer implements OuterInterface.InnerInterface, OuterInterface
{
  public void InnerMethod()
  {
    System.out.println(100);
  }
  public static void main(String[] args)
  {
    Outer obj = new Outer();
    obj.InnerMethod();
  }
}

a) 100

b) 编译错误

c) 运行时错误

d) 以上都不是

点此跳转到答案


文章后半部分是程序的输出及解析

image.png


输出及解析

程序一输出


答案

(d)

说明

静态内部类不能访问外部类的非静态字段。


程序二输出


答案

(C)

说明

LocalClass() 方法定义了一个局部内部类。此方法创建类 Inner 的对象并返回驻留在其中的变量数据的值。


程序三输出


答案

(b)

说明

方法 getData() 在 Anonymous 类中未定义,导致编译错误。


程序四答案

回答 :

(b)

说明

上面定义的内部类虽然可以访问外部类的私有变量数据,但是在内部类中声明变量数据使其特定于内部类,在变量声明方面没有冲突。


程序五答案

回答 :

(a)

说明:

嵌套接口是在java中定义的。由于两个接口都有 InnerMethod() 的声明,因此实现它一次对 InnerInterface 和 OuterInterface 都有效。


以上就是本篇文章的所有内容了

目录
相关文章
|
3天前
|
Java
Java一分钟之-Java内部类与匿名类
【5月更文挑战第12天】本文介绍了Java的内部类和匿名类,包括成员内部类和局部内部类,以及匿名类的一次性子类实现。通过代码示例展示了它们的使用方法,同时提到了常见问题和易错点,如混淆内部类与嵌套类、匿名类的生命周期管理及内部类的访问权限,并给出了相应的避免策略。理解这些概念有助于提升代码质量。
17 3
|
3天前
|
前端开发 Java 应用服务中间件
【异常解决】java程序连接MinIO报错The request signature we calculated does not match the signature you provided.
【异常解决】java程序连接MinIO报错The request signature we calculated does not match the signature you provided.
17 0
|
3天前
|
Java
Java内部类
Java内部类
10 2
|
3天前
|
Java Linux C语言
一步带你了解java程序逻辑控制
一步带你了解java程序逻辑控制
17 2
|
3天前
|
Java 数据安全/隐私保护
java中程序控制的典例
java中程序控制的典例
13 1
|
3天前
|
存储 Java 数据库连接
使用Java开发桌面应用程序
使用Java开发桌面应用程序
21 0
|
3天前
|
关系型数据库 MySQL Java
通过使用阿里云服务器,搭建Java程序的运行环境
通过使用阿里云服务器,搭建Java程序的运行环境
|
3天前
|
存储 网络协议 Java
本地MinIO存储服务通过Java程序结合cpolar实现远程连接上传文件
本地MinIO存储服务通过Java程序结合cpolar实现远程连接上传文件
|
3天前
|
存储 Java 开发工具
【Java探索之旅】用面向对象的思维构建程序世界
【Java探索之旅】用面向对象的思维构建程序世界
12 0
|
3天前
|
小程序 Java 程序员
【Java探索之旅】我与Java的初相识(二):程序结构与运行关系和JDK,JRE,JVM的关系
【Java探索之旅】我与Java的初相识(二):程序结构与运行关系和JDK,JRE,JVM的关系
29 0