最近看到一个Java题目,题目是这样的;
看题目的解析:
Constructors, static initializers, and instance initializers are not members and therefore are not inherited.(构造器、静态初始化块、实例初始化块不继承)
解析说构造器、静态初始化块、实例初始化块不算类的成员,所以不会被继承,
这个时候就有问题了:按照常理来说,构造器、静态初始化块、实例初始化块也是构成类的部分之一,为什么就不算类的成员呢?
通过查阅搜索引擎后发现:
很好,根本没什么用
但是当我翻阅Java中文书籍文档《Head First Java》:
以及英文博文时:
可以发现类的成员包括实例变量和方法;
而为了弄清楚这些结论的出处以及构造器到底属于什么,只能去看英文官方文档了;
果不其然,在官方文档中找到了答案解析的那段话:
Members of a class that are declared private are not inherited by subclasses of that class. Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared. Constructors, static initializers, and instance initializers are not members and therefore are not inherited.
至此就可以很明确了,java文档中规定的类的成员并不包括构造器、静态初始化块、实例初始化块;
那么,既然它们三个不属于类的成员,到底属于什么呢?
继续翻阅官方文档:
A class body may contain declarations of members of the class, that is, fields, methods, classes, and interfaces. A class body may also contain instance initializers, static initializers, and declarations of constructors for the class.
从这里就可以很明显的看出来了:
类的主体包含类的声明,即字段、方法、类和接口; 类主体还可以包含类的实例初始值设定项、静态初始值设定项和构造函数声明。
也就是说Java规定的类的成员是属性(Field)和方法(Method);
而大家常说的,我们中文理解中的类的成员,其实在Java文档的定义的是类的主体
Java文档:
https://docs.oracle.com/javase/specs/index.html
https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html
[
](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)