span.kw { color: #007020; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #40a070; }
code > span.fl { color: #40a070; }
code > span.ch { color: #4070a0; }
code > span.st { color: #4070a0; }
code > span.co { color: #60a0b0; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #06287e; }
code > span.er { color: #ff0000; font-weight: bold; }
-->

更多相关文章,见本人的个人主页:zhongxiewei.com

类定义

定义类的修饰符:

  • internal (default访问修饰符),当前项目中的代码对其有访问权限
  • public(访问修饰符),公开其可访问性
  • abstract,说明该类是抽象类,不能实例化,只能被继承,可以拥有抽象成员
  • sealed,不能被继承

关于类的修饰符需要注意的是:

  • 没有private和protected修饰符
  • public类不能够继承自internal类,如 public class MyClass : MyBase {} // MyBase is a internal class,不过internal可以继承自public类
  • 继承抽象类时,除非当前类也是抽象的,就必须对所有抽象成员初始化
  • 继承时,不允许多重继承
  • 类定义中,父类和接口同时存在是,父类必须位于冒号后面的第一个位置,如:public class MyClass : MyBase, IMyInterface {}

接口定义

定义接口的修饰符:

  • internal (default访问修饰符),当前项目中的代码对其有访问权限
  • public(访问修饰符),公开其可访问性

需要注意的是:

  • 没有sealed,abstract修饰符
  • 允许接口的多重继承
  • 当接口被类继承时,接口中的所有函数都必须实现

System.Object

当类没有继承对象时,默认继承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
}

类成员定义

修饰关键字:

  • public,任何代码都能够访问
  • private,类内部的代码才能够访问
  • internal,项目内部的代码才能够访问
  • protected,继承的子类中的代码能够访问
  • static,属于类的成员,而不单属于某个对象

只针对成员变量的修饰符:

  • readonly,只读变量,在声明或构造函数中进行初始化

只针对成员函数的修饰符:

  • virtual,能够被重载的函数
  • abstract, 必须被重载的函数,只能在abstract类中存在
  • override,重载基类中的函数
  • extern,函数的定义在其他的地方
  • sealed,函数无法被进一步重载

属性定义

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() {}

Collection

可以使用现有的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()
{
}
}

操作符重载

重载操作符的成员函数是公有静态的。需要注意的是在进行变量转换的成员函数的定义过程中,有两个额外的关键字explicitimplicit

// 二元操作符+
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操作符和as操作符

is操作符:检测一个未知的变量能够转化成为已知的类型的变量。如果可以则返回true,否则返回false。但是它并不能判断两个类型是否相同。

它的语法结构为:<operand> is <type>

该表达式的可能的结果如下:

  • 如果type是class类型,那么当<operand>也是那个类型,或是那个类的子类,或是可以转换成那个类型时,返回的结果为true
  • 如果type是interface类型,那么当operand是那个类型,或是那个类型的实现的时候,返回结果为true
  • 如果type是value type,如int,那么当operand是那个类型,或是可以隐式转换成那个类型的时候,返回结果为true

as操作符,将变量转换成特殊的引用类型。<operand> as <type>

适用于以下环境,

  • 如果operand的类型与type相同。
  • 如果operand能够隐式转换成type类型
  • 如果operand能够无损的强制得转换成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")
{
}
}

最新文章

  1. Spring bean依赖注入、bean的装配及相关注解
  2. B树、B-树、B+树、B*树
  3. 学习opencv之路(一)
  4. 测试RAC的功能
  5. Java过滤器与SpringMVC拦截器之间的关系与区别
  6. js获取客户端操作系统
  7. android layoutparams应用指南(转)
  8. bundle install rake-10.4.2
  9. 锋利的JQuery-Jquery选择器
  10. 新浪微博 iOS SDK获得用户信息
  11. Activity Threa创建Window和View分析
  12. Android Developers:两个视图渐变
  13. Markdown 入门教程
  14. hpuoj回文串问题(manacher+kmp)
  15. poj 3897 Maze Stretching 二分+A*搜索
  16. OAF开发概念和案例总结(项目总结)
  17. Cassadra &amp; presto 集群部署
  18. Linux_常用命令简单介绍(netstat,awk,top,tail,head,less,more,cat,nl)
  19. IIS7发布asp.net mvc提示404.0
  20. 【WebDriver】WebDriver 常用操作

热门文章

  1. win10调用局域网内xp系统上的打印机
  2. java面向对象_构造器
  3. css确定元素水平居中和垂直居中
  4. SSH学习笔记
  5. 混搭.NET技术
  6. 一个基于Orchard的开源CRM --coevery简介
  7. Nova PhoneGap框架 第二章 理解index.html
  8. [Hadoop大数据]——Hive初识
  9. error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“MTd_StaticDebug”
  10. WCF 客户端代理生成 通过SvcUtil.exe