内容:
第三章: C#中创建类型
- 类
- 继承
- object类型
- struct
- 访问修饰符
- 接口
- 枚举
- 嵌套类
- 泛型
1. Classes
典型的class定义:
class YourClassName
{ }
除此之外:
位置 | 内容 |
---|---|
class前 | Attributes and class modifiers, 非嵌套类modifiers包括: public,internal, abstract, sealed, static, unsafe, partial |
类名之后 | 泛型类,基类,接口 |
大括号内 | 类成员,包括: methods, properties, indexers, events, fields, constructors,overloaded operators, nested types, and a finalizer |
1.1 字段
a.字段修饰符:
名称 | 修饰符 |
---|---|
Static modifier | static |
Access modifiers | public internal private protected |
Inheritance modifier | new |
Unsafe code modifier | unsafe |
Read-only modifier | readonly |
Threading modifier | volatile |
b.readonly修饰符
static readonly int legs = 8, eyes = 2; // 使用逗号定义多个
1.2 方法
a.方法修饰符:
名称 | 修饰符 |
---|---|
Static modifier | static |
Access modifiers | public internal private protected |
Inheritance modifier | new virtual abstract override sealed |
Partial method modifier | partial |
Unmanaged code modifiers | unsafe extern |
Asynchronous code modifier | async |
b.Expression-bodied method
当只包含一条语句的时候:
int Foo (int x) => x * 2;
void Foo (int x) => Console.WriteLine (x);
c.方法重载
函数签名:函数名以及它对应的参数类型顺序(参数名不算, params也不算, 但是ref/out算)
当函数签名不同的时候,就可以重载; 重载中,参数是值传递还是引用传递也可以重载,比如:
Foo(int)
可以和Foo(ref int)
或者Foo(out int)
共存,但是 Foo(ref int)
和Foo(out int)
不可以共存。
d.local method
局部 函数只能被嵌套的函数看到。局部函数可以出现在 setter, 构造器, lambda表达式中。
不能使用static
修饰局部方法,如果外部方法是static的,那么局部函数就会自动成为static
。
void WriteCubes()
{
Console.WriteLine (Cube (3));
Console.WriteLine (Cube (4));
Console.WriteLine (Cube (5));
int Cube (int value) => value * value * value;
}
1.3 构造器
class或者struct都有构造器。
a.构造器的修饰符:
名称 | 修饰符 |
---|---|
Access Modifiers | public internal private protected |
unmanged code modifiers | unsafe extern |
从C#7.0开始,构造器也可以使用 expression-bodied:
public Panda (string n) => name = n;
b.调用其他构造器
this
关键字和base
关键字。被调用的会先执行
public class Wine{
public Wine (decimal price) { Price = price; }
public Wine (decimal price, int year) : this (price) { Year = year; } //被调用的会先执行
}
传入构造器的参数可以是表达式,但是不能调用this
,因为此时instance自身都没有创建;不过没有使用static方法。
c.隐式无参public构造器
d.字段初始化顺序
字段的初始化是比构造器初始化更早的,并且按照他们在class中的定义顺序
e.非public构造器
1.4 Deconstructor(C# 7.0)
把一个对象的结果返回给多个数据。Deconstructor也可以重载
class Rectangle
{
public readonly float Width, Height;
public Rectangle (float width, float height)
{
Width = width;
Height = height;
}
///deconstructor
public void Deconstruct (out float width, out float height)
{
width = Width;
height = Height;
}
}
var rect = new Rectangle (3, 4);
(float width, float height) = rect; // Deconstruction
相当于:
float width, height;
rect.Deconstruct (out width, out height);
1.5 Object Initializers
Bunny b1 = new Bunny { Name="Bo", LikesCarrots=true, LikesHumans=false };
//相当于
Bunny temp1 = new Bunny(); // temp1 is a compiler-generated name
temp1.Name = "Bo";
temp1.LikesCarrots = true;
temp1.LikesHumans = false;
Bunny b1 = temp1;
编译产生了一个临时变量,这就保证初始化中如果出现错误,不会存在一个初始化了一半的对象。
1.6 this
1.7 Properties
a.修饰符
名称 | 修饰符 |
---|---|
Static modifier | static |
Access modifiers | public internal private protected |
Inheritance modifiers | new virtual abstract override sealed |
Unmanaged code modifiers | unsafe extern |
b.read-only property
c. expression-bodied properties
decimal currentPrice, sharesOwned;
public decimal Worth => currentPrice * sharesOwned; //get
public decimal Worth
{
get => currentPrice * sharesOwned;
set => sharesOwned = value / currentPrice;
}
d.自动属性
编译器自动会产生一个我们无法访问到的字段。
public class Stock
{
...