A class-declaration is a type-declaration (§16.5) that declares a new class.
class-declaration:
attributesopt class-modifiersopt class identifier class-baseopt class-body
;opt
A class-declaration consists of an optional set of attributes (§24),
followed by an optional set of class-modifiers
(§17.1.1), followed by the keyword class and an identifier that names the
class, followed by an optional classbase
specification (§17.1.2), followed by a class-body (§17.1.3), optionally
followed by a semicolon.
17.1.1 Class modifiers
A class-declaration may optionally include a sequence of class modifiers:
class-modifiers:
class-modifier
class-modifiers class-modifier
class-modifier:
new
public
protected
internal
private
abstract
sealed
It is a compile-time error for the same modifier to appear multiple times
in a class declaration.
The new modifier is permitted on nested classes. It specifies that the
class hides an inherited member by the same
name, as described in §10.2.2. It is a compile-time error for the new
modifier to appear on a class declaration that
is not a nested class declaration.
The public, protected, internal, and private modifiers control the
accessibility of the class. Depending
on the context in which the class declaration occurs, some of these
modifiers may not be permitted (§10.5.1).
The abstract and sealed modifiers are discussed in the following sections.
17.1.1.1 Abstract classes
The abstract modifier is used to indicate that a class is incomplete and
that it is intended to be used only as a
base class. An abstract class differs from a non-abstract class in the
following ways:
? An abstract class cannot be instantiated directly, and it is a
compile-time error to use the new operator on an
abstract class. While it is possible to have variables and values whose
compile-time types are abstract, such
variables and values will necessarily either be null or contain references
to instances of non-abstract classes
derived from the abstract types.
? An abstract class is permitted (but not required) to contain abstract
members.
C# LANGUAGE SPECIFICATION
210
? An abstract class cannot be sealed.
When a non-abstract class is derived from an abstract class, the
non-abstract class must include actual
implementations of all inherited abstract members, thereby overriding those
abstract members. [Example: In the
example
abstract class A
{
public abstract void F();
}
abstract class B: A
{
public void G() {}
}
class C: B
{
public override void F() {
// actual implementation of F
}
}
the abstract class A introduces an abstract method F. Class B introduces an
additional method G, but since it
doesn?t provide an implementation of F, B must also be declared abstract.
Class C overrides F and provides an
actual implementation. Since there are no abstract members in C, C is
permitted (but not required) to be nonabstract.
end example]
17.1.1.2 Sealed classes
The sealed modifier is used to prevent derivation from a class. A
compile-time error occurs if a sealed class is
specified as the base class of another class.
A sealed class cannot also be an abstract class.
[Note: The sealed modifier is primarily used to prevent unintended
derivation, but it also enables certain runtime
optimizations. In particular, because a sealed class is known to never have
any derived classes, it is possible
to transform virtual function member invocations on sealed class instances
into non-virtual invocations. end note]
17.1.2 Class base specification
A class declaration may include a class-base specification, which defines
the direct base class of the class and the
interfaces (§20) implemented by the class.
class-base:
: class-type
: interface-type-list
: class-type , interface-type-list
interface-type-list:
interface-type
interface-type-list , interface-type
17.1.2.1 Base classes
When a class-type is included in the class-base, it specifies the direct
base class of the class being declared. If a
class declaration has no class-base, or if the class-base lists only
interface types, the direct base class is assumed
to be object. A class inherits members from its direct base class, as
described in §17.2.1.
[Example: In the example
class A {}
class B: A {}
class A is said to be the direct base class of B, and B is said to be
derived from A. Since A does not explicitly
specify a direct base class, its direct base class is implicitly object.
end example]
Chapter 17 Classes
211
The direct base class of a class type must be at least as accessible as the
class type itself (§10.5.4). For example, it
is a compile-time error for a public class to derive from a private or
internal class.
The direct base class of a class type must not be any of the following
types: System.Array,
System.Delegate, System.Enum, or System.ValueType.
The base classes of a class are the direct base class and its base classes.
In other words, the set of base classes is
the transitive closure of the direct base class relationship. [Note:
Referring to the example above, the base classes
of B are A and object. end note]
Except for class object, every class has exactly one direct base class. The
object class has no direct base class
and is the ultimate base class of all other classes.
When a class B derives from a class A, it is a compile-time error for A to
depend on B. A class directly depends on
its direct base class (if any) and directly depends on the class within
which it is immediately nested (if any).
Given this definition, the complete set of classes upon which a class
depends is the transitive closure of the
directly depends on relationship.
[Example: The example
class A: B {}
class B: C {}
class C: A {}
is in error because the classes circularly depend on themselves. Likewise,
the example
class A: B.C {}
class B: A
{
public class C {}
}
results in a compile-time error because A depends on B.C (its direct base
class), which depends on B (its
immediately enclosing class), which circularly depends on A. end example]
A class does not depend on the classes that are nested within it. [Example:
In the example
class A
{
class B: A {}
}
B depends on A (because A is both its direct base class and its immediately
enclosing class), but A does not
depend on B (since B is neither a base class nor an enclosing class of A).
Thus, the example is valid. end example]
It is not possible to derive from a sealed class. [Example: In the example
sealed class A {}
class B: A {} // Error, cannot derive from a sealed class
class B results in a compile-time error because it attempts to derive from
the sealed class A. end example]
17.1.2.2 Interface implementations
A class-base specification may include a list of interface types, in which
case the class is said to implement the
given interface types. Interface implementations are discussed further in §2
0.4.
17.1.3 Class body
The class-body of a class defines the members of that class.
class-body:
{ class-member-declarationsopt }
分享到:
相关推荐
Produce C++ class implementations given class declarations Produce a recursive algorithm Background Typical online auction systems display advertised items for sale in categories. Description
public class CustomButton extends UIComponent { private var _label:String; [Bindable] public function get label():String { return _label; } public function set label(value:String):void { _...
该资源希望对大家能有帮助!! The skills: Use STL sorting functions Use STL find functions Use of appropriate STL functions Produce C++ class implementations given class declarations
declarations of private instance variables prototypes of private methods }; ``` 类的定义文件通常包含类的接口。在包含类定义的程序能够被编译和执行之前,你必须为每个类的方法提供实现,通常是在一个相关...
The database represents internal program structures, locations of function declarations, contents of class declarations, and relationships between program components. Source-Navigator graphical ...
com.sun.mirror.util.Declarations.class com.sun.mirror.util.SimpleDeclarationVisitor.class com.sun.mirror.util.SimpleTypeVisitor.class com.sun.mirror.util.SourceOrderDeclScanner.class ...
"Placing const in Declarations by Dan Saks"这篇文章很可能是深入探讨了如何在声明中有效地使用`const`。 1. `const`的基本概念: - `const`关键字用来定义一个只读变量,一旦赋值后就不能再次更改。 - 它可以...
: npm install css-declarations用import { parse , stringify } from 'css-declarations'var values = parse ( ` color:/*red*/purple; -webkit-border-radius: 3px !important;;` )// => {color: 'purple', ...
IDEA中的.VUE文件报错 Export declarations are not supported by current JavaScript version 和Export declarations are not supported by current JavaScript version报错都是一个解决办法 js文件报错 第一步,...
C++ requires a type specifier for all declarations 声明、初始化与赋值的区别: 声明:int a; 初始化:int a = 2;(在声明的时候顺带赋值叫做初始化) 赋值:a = 2; 只有定义(int a;)才分配存储空间,初始化...
declarations, that can be there to make a class reloadable * e.g. * @Reloadable(module={Id}) * public class xx {} */ static final int MOUDLE_COMMAND = 1 ; JReloader jReloader = new JReloader (); ...
class-dump is a command-line utility for examining the Objective-C segment of Mach-O files. It generates declarations for the classes, categories and protocols. This is the same information provided ...
public class CustomClass extends EventDispatcher { [Bindable] public var myProperty:Object; // 其他方法和属性... } ``` 在这里,`myProperty`被标记为`Bindable`,这意味着当其值改变时,所有依赖于...
克拉克 JVM的一种简单的,静态类型的编程语言。 该编译器使用1000行以下的Java语言实现,非常适合于学习JVM字节码。... // Files contain class declarations. class Hello; // Classes contain method declara
选择器可以是HTML元素名,如`h1`,也可以是更复杂的结构,如类(class)选择器 `.class`,ID选择器 `#id`,或者组合选择器,如`h1, h2`,用于同时选择多个元素。 2.1.2 声明和关键字 声明(declarations)由一系列...
TDBGrid3D = class(TDBGrid) private { Private declarations } protected { Protected declarations } public { Public declarations } published function DoMouseWheel(Shift: TShiftState; ...
clarifications.com.ua有用的链接设计: : 将扫描的声明数字化的表格: : 我们的众包框架将用于众包数字化流程: : 各种脚本来清理数据: : R生成分析的先决条件首先安装R 3.1或更高版本然后运行R并安装以下软件包...
在“类(Classes)”章节中,涉及了类的属性、类名(Classnames)、类成员(Classmembers)、联合体(Unions)、局部类声明(Local class declarations)、派生类(Derived classes)、成员名称查找(Member name ...