C#2008与.NET 3.5 高级程序设计读书笔记(5)定义封装的类类型

1.关键字this

关键字this表示当前的类实例或者对象的引用

以下是 this 的常用用途:

  1. 限定被相似的名称隐藏的成员
  2. 将对象作为参数传递到其他方法
  3. 声明索引器

示例:

  1.  综合示例。
代码
// this 关键字
// keywords_this.cs
using System;
class Employee
{
private string _name;
private int _age;
private string[] _arr = new string[5];

public Employee(string name, int age)
{
// 使用this限定字段,name与age
this._name = name;
this._age = age;
}

public string Name
{
get { return this._name; }
}

public int Age
{
get { return this._age; }
}

// 打印雇员资料
public void PrintEmployee()
{
// 将Employee对象作为参数传递到DoPrint方法
Print.DoPrint(this);
}

// 声明索引器
public string this[int param]
{
get { return _arr[param]; }
set { _arr[param] = value; }
}

}
class Print
{
public static void DoPrint(Employee e)
{
Console.WriteLine(
"Name: {0}\nAge: {1}", e.Name, e.Age);
}
}

class TestApp
{
static void Main()
{
Employee E
= new Employee("Hunts", 21);
E[
0] = "Scott";
E[
1] = "Leigh";
E[
4] = "Kiwis";
E.PrintEmployee();

for(int i=0; i<5; i++)
{
Console.WriteLine(
"Friends Name: {0}", E[i]);
}

Console.ReadLine();
}
}

/**//*
控制台输出:
Name: Hunts
Age: 21
Friends Name: Scott
Friends Name: Leigh
Friends Name:
Friends Name:
Friends Name: Kiwis
*/

注意点

  1. 由于静态成员函数存在于类一级,并且不是对象的一部分,因此没有 this 指针。在静态方法中引用 this 是错误的。
  2. 索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。

2.关键字base:

base

base 关键字用于从派生类中访问基类的成员:

  1. 调用基类上已被其他方法重写的方法。
  2. 指定创建派生类实例时应调用的基类构造函数。

基类访问只能在构造函数、实例方法或实例属性访问器中进行。

示例:

(1).在派生类中调用基类方法。

代码
// base 关键字
// 访问基类成员
using System;

public class BaseClass
{
protected string _className = "BaseClass";

public virtual void PrintName()
{
Console.WriteLine(
"Class Name: {0}", _className);
}
}

class DerivedClass : BaseClass
{
public string _className = "DerivedClass";

public override void PrintName()
{
//调用基类方法
base.PrintName();
Console.WriteLine(
"This DerivedClass is {0}", _className);
}
}

class TestApp
{
public static void Main()
{
DerivedClass dc
= new DerivedClass();
dc.PrintName();
}
}

/**//*
控制台输出:
The BaseClass Name is BaseClass
This DerivedClass is DerivedClass
*/

(2).在派生类中调用基类构造函数。 

代码
// keywords_base2.cs
using System;
public class BaseClass
{
int num;

public BaseClass()
{
Console.WriteLine(
"in BaseClass()");
}

public BaseClass(int i)
{
num
= i;
Console.WriteLine(
"in BaseClass(int {0})", num);
}
}

public class DerivedClass : BaseClass
{
// 该构造器调用 BaseClass.BaseClass()
public DerivedClass() : base()
{
}

// 该构造器调用 BaseClass.BaseClass(int i)
public DerivedClass(int i) : base(i)
{
}

static void Main()
{
DerivedClass dc
= new DerivedClass();
DerivedClass dc1
= new DerivedClass(1);
}
}

/**//*
控制台输出:
in BaseClass()
in BaseClass(1)
*/

注意点

  1. 从静态方法中使用 base 关键字是错误的。
  2. base 主要用于面向对象开发的对态这方面,在示例2中有体现。

3.关键字static:

(1).静态变量

类的变量,但是不属于任何一个类的具体对象.也就是说,对于该类的任何一个具体的对象来说,静态变量是一个公共的存储单元,任何一个类的对象在访问这个存储单元时,都会获得一个同样的数值,同样,任何一个类在修改这个存储单元时,也都会完成相同的操作。这样我们就可以理解成对象共享了静态变量。

(2)静态方法

静态方法属于整个类的,静态方法只能访问静态变量.然而非静态方法可以使用静态变量和非静态变量,因为静态数据对类型的所有实例都是可用的.

代码
public class Person
{
public int age = 10;
public static int money = 0;
public void Say()
{
//非静态方法可以访问静态成员和非静态成员,因为静态数据对类的所有实例都是可用的.
age = age++;
money
++;
Sleep();
}
public static void Sleep()
{
}
public static void AddMoney()
{
//静态方法只能访问静态成员
money++;
Sleep();
//错!静态方法不能访问非静态成员
//age++;
//Say();
//静态方法可以访问其他类的实例的静态方法和非静态方法
Animal animal = new Animal();
animal.SayHello();
Animal.Sleep();
}
}
public class Animal
{
public void SayHello()
{
}
public static void Sleep()
{
}
}

 (3)静态构造函数 

代码
public class SavingsAccount
{
public double currBalance;
public static double currInterest;
public SavingsAccount(double balance)
{
currBalance
= balance;
//每次新建对象的时候,值会重置
currInterest = 0.04;
}
//静态构造函数初始化静态数据
static SavingsAccount()
{
currInterest
= 0.04;
}
}

(4).静态类

静态类是一种声明为 static 类型的,且仅包含静态成员的类。不能使用 new 关键字创建静态类的实例。

 静态类的主要特点如下:  
      它们仅包含静态成员。 
      它们不能被实例化。 
      它们是密封的。 
      它们不能包含实例构造函数.

静态类和非静态类的区别:
            非静态类是储存有状态的一类操作过程,例如语言包的非静态类,声明了之后,可能获取语言的种类,语言元素和一些附加的东西
            静态类可以视为类库里都是单一的过程,不存在“状态”的概念,就可以使用静态类。
            非静态类可以包含静态方法,但静态类不能包含非静态方法。

代码
//通过使用关键字static定义的类将导致C#编译器将该类型同时标志为abstract和sealed。
//另外,编译器不会在类型中生成实例构造器方法。
using System;

public static class AStaticClass{

public static void AstaticMethod(){}

public static string AstaticProperty{

get { return s_AStaticField;}

set { s_AStaticField = value;}

}

private static String s_AStaticField;

public static event EventHandler AStaticEvent;

}

 (5).C#访问限制符

C# Access Modifier Meaning in Life
public Marks a member as accessible from an object variable as well as any
derived classes.
private Marks a method as accessible only by the class that has defined the
method. In C#, all members are private by default.
protected Marks a method as usable by the defining class, as well as any derived
classes. Protected methods, however, are not accessible from an object
variable.
internal Defines a method that is accessible by any type in the same assembly,
but not outside the assembly.
protected internal Defines a method whose access is limited to the current assembly or
types derived from the defining class in the current assembly.
代码
class Program
{
static void Main(string[] args)
{
// Make an object and attempt to call members.
SomeClass c = new SomeClass();
c.PublicMethod();
c.InternalMethod();
c.ProtectedInternalMethod();
c.PrivateMethod();
// Error!
c.ProtectedMethod(); // Error!
c.SomeMethod(); // Error!
}
// Member visibility options.
class SomeClass
{
// Accessible anywhere.
public void PublicMethod() { }
// Accessible only from SomeClass types.
private void PrivateMethod() { }
// Accessible from SomeClass and any descendent.
protected void ProtectedMethod() { }
// Accessible from within the same assembly.
internal void InternalMethod() { }
// Assembly-protected access.
protected internal void ProtectedInternalMethod() { }
// Unmarked members are private by default in C#.
void SomeMethod() { }
}
}
原文地址:https://www.cnblogs.com/engine1984/p/1767774.html