定义类的修饰符:
关于类的修饰符需要注意的是:
public class MyClass : MyBase {} // MyBase is a internal class
,不过internal可以继承自public类public class MyClass : MyBase, IMyInterface {}
定义接口的修饰符:
需要注意的是:
当类没有继承对象时,默认继承System.Object,那么就有必要看看该类中到底有什么。
Method | Description |
---|---|
Object() | 构造函数 |
~Object() | 析构函数 |
virtual bool Equals(object) | this和other对象进行比较 |
static bool Equals(object,object) | 比较输入的两个对象,内部调用上一个函数,true all null |
static bool ReferenceEquals(object,object) | 比较输入的两个对象是不是同一个对象的引用 |
virtual string ToString() | 默认返回类类型的名字 |
object MemberwiseClone() | 构建新的object实例,并对成员变量进行copy,引用类型的成员变量仍然引用同一个变量 |
System.Type GetType() | 返回对象的类型 |
virtual int GetHashCode() | 返回对象的hash值 |
使用GetType的一个示例:
if (myObj.GetType() == typeof(MyComplexClass)){ // myObj is an instance of the class MyComplexClass}
修饰关键字:
只针对成员变量的修饰符:
只针对成员函数的修饰符:
private int myInt;public int MyIntProp{ get { return myInt; } set { if (value >= 0 && value <= 10) myInt = value; else throw(...); }}
很容易想到的实现方式如下:
public class MyBaseClass{ public void DoSomething() { // base implementation }}public class MyDerivedClass : MyBaseClass{ public void DoSomething() { // Derived class implementation, hides base implementation }}
但是上面的代码在编译的过程中会出现警告,说MyDerivedClass中的DoSomething函数隐藏了基类中的DoSomething函数,请使用new关键字,即public new void DoSomething() {}
。
可以使用现有的System.Collections.ArrayList实现。这里要讲的是自己定义Collections。可以采用如下的方式来实现,如:
public class Animals : CollectionBase{ // Add功能 public void Add(Animal newAnimal) { List.Add(newAnimal); } // Remove功能 public void Remove(Animal oldAnimal) { List.Remove(oldAnimal); } // 实现MyAnimal[0]的访问方式 public Animal this[int animalIndex] { get { return (Animal)List[animalIndex]; } set { List[animalIndex] = value; } } public Animals() { }}
重载操作符的成员函数是公有静态的。需要注意的是在进行变量转换的成员函数的定义过程中,有两个额外的关键字explicit和implicit。
// 二元操作符+public class Class1{ public int val; // 二元操作符 public static Class1 Operator +(Class1 op1, Class1 op2) { Class1 returnVal = new Class1(); returnVal.val = op1.val + op2.val; return returnVal; } // 一元操作符,- public static Class1 operator -(Class1 op1) { Class1 returnVal = new Class1(); returnVal.val = -op1.val; return returnVal; }}
is操作符:检测一个未知的变量能够转化成为已知的类型的变量。如果可以则返回true,否则返回false。但是它并不能判断两个类型是否相同。
它的语法结构为:<operand> is <type>
该表达式的可能的结果如下:
<operand>
也是那个类型,或是那个类的子类,或是可以转换成那个类型时,返回的结果为trueas操作符,将变量转换成特殊的引用类型。<operand> as <type>
。
适用于以下环境,
如果不能够转换,则返回的结果为null。
BaseClass obj1 = new BaseClass();DerivedClass obj2 = obj1 as DerivedClass; // return null 不会抛出异常,如果使用 ... = (DerivedClass)obj1; 则会抛出异常// 如果以下面的方式进行转换则是可以的DerivedClass derivedClass = new DerivedClass();BaseClass baseClass = derivedClass;DerivedClass newDerivedClass = baseClass as DerivedClass; // not null
直接采用System.Object对象内的MemberwiseClone()当遇到类含有引用变量的时候,就不能够实现深层的拷贝,如:
public class Content{ public int Val;}public class Cloner{ public Content MyContent = new Content(); public Cloner(int newVal) { MyContent.Val = newVal; } public object GetCopy() { return MemberwiseClone(); }}// 具体调用Cloner mySource = new Cloner(5);Cloner myTarget = (Cloner)mySource.GetCopy();mySource.MyContent.Val = 2; // 这将导致myTarget.MyTarget.Val也相应的改变成了2
要实现深度的拷贝,可以实现ICloneable接口。
public class Cloner : ICloneable{ public Content MyContent = new Content(); public Cloner(int newVal) { MyContent.Val = newVal; } public object Clone() { Cloner clonedCloner = new Cloner(MyContent.Val); return clonedCloner; // 或是 // Cloner clonedCloner = new Cloner(); // clonedCloner.MyContent = MyContent.Clone(); // return clonedCloner; }}
自定义的过程中,只要继承Expection
类并进行实现即可。如:
public class AgeBelowZeroException : Exception{ public AgeBelowZeroException() : base("The age must >= 0") { }}
新闻热点
疑难解答