- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有课本上的代码:
public class Question_3_4 {
public static class Inner {
private void doIt() {
System.out.println("doIt()");
}
}
public static void main(String[] args) {
Question_3_4.Inner i = new Inner();
i.doIt();
}
}
好吧,内部类是静态的,所以我假设上面的代码隐式实例化了 Question_3_4 的实例?
Question_3_4.Inner i = new Question_3_4.Inner();
产生与上述代码相同的结果。
所以我认为
Question_3_4.Inner i = new Question_3_4.Inner();
和
Question_3_4.Inner i = new Inner();
是同一件事。
如果我的假设是错误的,我在这里遗漏了什么?
最佳答案
首先,您没有使用正确的术语。
你没有声明一个内部类,而是一个静态嵌套类。
Terminology: Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes.
您可以获得有关术语以及如何以及为何在 Java Oracle Nested Classes tutorial 上使用嵌套类的可访问且有用的信息:
关于你的问题:
Well, Inner class is static, so I assume the above code implicitly instantiates Question_3_4's instance ?
你不需要实例化外部类来实例化一个嵌套类或者也可以说是一个static
嵌套类。
此外,如果需要,编译器不会为您实例化外部类,而是会发出编译错误。
仅在内部类的情况下才需要实例化外部类(没有 static
嵌套类)。
例如,删除嵌套类的 static
修饰符,将根据需要为 Question_3_4.Inner i = new Inner();
行产生编译错误有一个外部类的实例被实例化。
public class Question_3_4 {
public class Inner {
private void doIt() {
System.out.println("doIt()");
}
}
public static void main(String[] args) {
Question_3_4.Inner i = new Inner();
^--- error: non-static variable this cannot be referenced from a static context
i.doIt();
}
}
关于java - ParentClass.NestedClass 是否 nc= new NestedClass();隐式实例化父类实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45499143/
在 K&R 第 1 章中: The statement ++nc presents a new operator, ++, which means increment by one. You coul
我看过一些关于 .ssh/config 和 proxycommand 的博客 现在下一个命令之间有什么区别 ProxyCommand ssh proxyserver -W [%h]:%pProxyCo
这个问题是在这个问题之后:Sockets working in openSUSE do not work in Debian? 在我的 Debian 系统上使用套接字时,我必须使用 nc -l -p
我将用一个小例子来说明我面临的问题。 class TestProtocol(basic.LineReceiver): def lineReceived(self, line):
我需要在屏幕上的远程机器上启动 nc 并开始从屏幕上的另一台远程机器传输文件,我正在尝试使用 bash 脚本通过部署机器(jenkins)运行它 在远程机器 1 上,即 tester1: ssh -t
我在使用 nc 传输文件的奇怪情况上苦苦挣扎了很长时间,我使用以下 bash 脚本将文件从 A 机器传输到 B 机器,脚本似乎没问题,文件正在传输到 B 机器,但是文件的大小是 0 字节并且没有任何内
工具在使用过程中会出现磨损。使用这些工具的机器需要在测量磨损后纠正其 NC(即数控)程序。 我已经创建了一个 XSD 模型并生成了管理这些度量的 JAXB 类,现在我想自动生成 NC 代码来设置机器上
我尝试使用nc从shell获取zookeeper统计信息, 首先调用nc localhost 2181,然后输入:stat有效。 而回显“stat” | nc localhost 2181 什么也不返
所以我想制作一个类似于 Netcat (nc -l -v -p 2121) 的 Python 端口监听器。我环顾四周,却找不到我要找的东西。我只是希望能够通过 CLI 参数提供一个端口并监听该端口(p
问题是,当我使用上面代码中的 nc 语法从 CMD 打开 nc.exe 时,它可以工作并连接到我的服务器。但是当我通过我的程序(使用上面的代码)这样做时,它只是打开 nc.exe 窗口,但它不
我想知道如何从管道命令使用 stdout 然后在 nc 连接中使用它: | \ grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9
我想使用 nc 作为扩展回显服务器来回显十六进制缓冲区(保存在文件/tmp/data.hex 中)。当我说“扩展”时,我的意思是服务器不会在客户端连接后立即回显此缓冲区,而是仅在客户端发送一些信息后才
我得到了这个 .nc 文件。但是,当我这样阅读文件时 ncid = netcdf.open(ncfile) 它只给我一个数字。它应该包含一些数据。我不确定它有什么问题。 谁能提供一些信息? 最佳答案
我如何确定正在为网页提供服务的网络服务器软件(和版本)? 我正在使用 Linux,我需要 nc 的替代品。 最佳答案 尝试 nmap ,注意 -sV 选项。 关于linux - 如何确定哪些网络服务器
当我使用 nc 命令获得 HTTP 响应但结果不正确时。例如 $nc site.it 80 GET / HTTP/1.1 Host: site.it User-Agent: curl/7.54.0 A
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
nc (ncat) Ncat is a feature-packed networking utility which reads and writes data across networks
我的 httpd.conf 中有以下规则 RewriteEngine on RewriteBase / RewriteCond %{QUERY_STRING} pas
我对 bash 很陌生,我遇到了这段代码。 j=0 while ! nc -z "$host" "$port"; do j=$((j+1)) if [ $j -ge 10 ]; then
我正在尝试使用 netcat 发送广播消息。 我打开了防火墙并发送这样的常规消息对我有用: host: nc -l 192.168.1.121 12101 client: echo "hell
我是一名优秀的程序员,十分优秀!