- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
假设我们有这个类和它的内部类:
/* Outer.java */
public class Outer {
private static class Inner {
private final Object foo;
public Inner(Object foo) {
this.foo = foo;
}
public Object getFoo() {
return foo;
}
}
Inner inner = parse(/* someMistery */);
// Question: to access foo, which is recommended?
Object bar = inner.getFoo();
Object baz = inner.foo;
}
令我惊讶的是 inner.foo
能正常工作。
因为 foo
是私有(private)的
,所以它只能通过getFoo()
访问,对吧?
最佳答案
Since
foo
is private, it can be accessed only throughgetFoo()
, right?
在这种情况下,Outer
也可以访问它,因为 Inner
是 Outer
的成员。
6.6.1说:
[If] the member or constructor is declared
private
, [then] access is permitted if and only if it occurs within the body of the top level class that encloses the declaration of the member or constructor.
请注意,它被指定为可在包含声明的顶级类的主体内访问。
这意味着,例如:
class Outer {
static class Foo {
private Foo() {}
private int i;
}
static class Bar {{
// Bar has access to Foo's
// private members too
new Foo().i = 2;
}}
}
是否使用 setter/getter 真的是个人喜好问题。这里的重要认识是外部类可以访问其嵌套类的私有(private)成员。
作为推荐,我个人会说:
private
(只有外部类可以访问它),我什至不会费心给它一个 getter,除非 getter 进行计算。它是任意的,其他人可以选择不使用它。如果风格混杂,代码就有模糊性。 (inner.foo
和 inner.getFoo()
真的做同样的事情吗?我们不得不浪费时间检查 Inner
类来找到出。)private
,则使用 getter 以使样式统一。如果你真的想隐藏 private
成员,甚至对外部类隐藏,你可以使用带有本地类或匿名类的工厂:
interface Nested {
Object getFoo();
}
static Nested newNested(Object foo) {
// NestedImpl has method scope,
// so the outer class can't refer to it by name
// e.g. even to cast to it
class NestedImpl implements Nested {
Object foo;
NestedImpl(Object foo) {
this.foo = foo;
}
@Override
public Object getFoo() {
return foo;
}
}
return new NestedImpl(foo);
}
请注意,您的static class Inner {}
在技术上是static 嵌套类,而不是内部类。 class Inner {}
(没有 static
)将是一个内部类。
这是 specifically defined是这样的:
The
static
keyword may modify the declaration of a member typeC
within the body of a non-inner class or interfaceT
. Its effect is to declare thatC
is not an inner class.
关于java - 访问内部类中字段的推荐/正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30200280/
嗨, 我是 Spark 的新手,我正在尝试使用 ML 推荐。 我的代码 df = sqlContext.createDataFrame( [(0, 0, 4.0), (0, 1, 2.0), (1,
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
平台工程是为软件开发人员创建高效生态系统的过程,帮助他们自主执行软件开发生命周期的端到端操作。平台工程旨在减少开发人员的整体认知负荷并消除流程中的瓶颈,让开发团队的体验更佳。平台工程工具通过改善开发
最近在ubuntu系统中使用自带的firefox浏览器,发现有写问题,比如登陆后,书签,历史记录等,原本在windows下同步的数据无法同步,添加书签的功能也无法使用。 经过查询资料后得知,unb
Remax是蚂蚁开源的一个用React来开发小程序的框架,采用运行时无语法限制的方案。整体研究下来主要分为三大部分:运行时原理、模板渲染原理、编译流程;看了下现有大部分文章主要集中在Reamx的运行
实验室拟态存储的项目需要通过lvs-nat模式通过lvs服务器来区隔内外网的服务,所以安全防护的重心则落在了lvs服务器之上。笔者最终选择通过firewalld放行端口的方式来实现需求,由于fire
如今,随着我们身体各类数据的指数级增长,人们需要接受的信息量越来越大,系统必须处理的难度也是越来越高。而这些正是我们需要通过交互式图表和仪表盘,来实现数据可视化的根本原因。在大幅节省用户的时间和精力
vsftpd 是“very secure FTP daemon”的缩写,安全性是它的一个最大的特点。 vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 Linux、
1、实现memcpy 将src所指向的内容拷贝到dst所指向的位置,拷贝len个字节。 memcpy是内存拷贝函数 memcpy在使用的时候不用考虑类型,以字节为单位进行拷贝
现在有3台服务器 s1(主),s2(从), s3(从)需要实现文件实时同步,我们可以安装Nfs服务端和客户端来实现! 1、安装 NFS 服务器所需的软件包:
本文基于Free Code Camp基本算法脚本“查找字符串中最长的单词”。 在此算法中,我们要查看每个单词并计算每个单词中有多少个字母。然后,比较计数以确定哪个单词的字符最多,并返回最长单词的长
I/O简介 I/O是Input/output的缩写,在java中,对于数据的输入和输出以流的方式进行。java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出
目录 docker容器源码部署httpd,用存储卷部署网站 创建一个httpd镜像 部署nfs 挂载 创建容器并映射
python代码如下: import webbrowser as wbimport foliumif __name__ == '__main__': loc = [30.679943, 104.0
近日,微软在 Github 上开源了一个 Python 静态类型检查工具:pyright ,引起了社区内的多方关注。 微软在开源项目上的参与力度是越来越大了,不说收购 Github 这种大的战略野
在编写多线程代码时,经常面临线程安全退出的问题。 一般情况下,选择检查标志位的方式: 在线程的while循环中,执行完例程后,都对标志位进行检查,如果标志位指示继续执行则再次执行例程,如果标志
前言 在程序中我们经常可以看到有很多的加密算法,比如说MD5 sha1等,今天我们就来了解下这下加密算法的吧,在了解之前我们需要知道一个模块嘛就是hashlib,他就是目前Python一个提供字符
java 泛型(generics)是 jdk 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型。 泛型的本质是参数化类型,也就是说所操作的数据类型
在Python中,当我们有两个字典需要合并的时候,可以使用字典的 update 方法,例如: a = {'a': 1, 'b': 2} b = {'x': 3, 'y': 4}
有的时候我们在获取到目标电脑时候如果对方电脑又python 编译环境时可以利用python 反弹shell 主要用到python os库和sokect库 这里的服务端在目标机上运行
我是一名优秀的程序员,十分优秀!