与类相关基本概念----Beginning Visual C#
来源:互联网 发布:mac版office激活 编辑:程序博客网 时间:2024/05/18 06:24
更多相关文章,见本人的个人主页:zhongxiewei.com
%26nbsp;
类定义
定义类的修饰符:
- 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,那么就有必要看看该类中到底有什么。
使用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 %26gt;= 0 %26amp;%26amp; value %26lt;= 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() { }}
操作符重载
重载操作符的成员函数是公有静态的。需要注意的是在进行变量转换的成员函数的定义过程中,有两个额外的关键字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操作符和as操作符
is操作符:检测一个未知的变量能够转化成为已知的类型的变量。如果可以则返回true,否则返回false。但是它并不能判断两个类型是否相同。
它的语法结构为:%26lt;operand%26gt; is %26lt;type%26gt;
该表达式的可能的结果如下:
- 如果type是class类型,那么当
%26lt;operand%26gt;
也是那个类型,或是那个类的子类,或是可以转换成那个类型时,返回的结果为true - 如果type是interface类型,那么当operand是那个类型,或是那个类型的实现的时候,返回结果为true
- 如果type是value type,如int,那么当operand是那个类型,或是可以隐式转换成那个类型的时候,返回结果为true
as操作符,将变量转换成特殊的引用类型。%26lt;operand%26gt; as %26lt;type%26gt;
。
适用于以下环境,
- 如果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 %26gt;= 0") { }}
- 与类相关基本概念----Beginning Visual C#
- 与类相关基本概念----Beginning Visual C#
- 与类相关基本概念----Beginning Visual C#
- 与类相关基本概念----Beginning Visual C#
- 与类相关基本概念----Beginning Visual C#
- 与类相关基本概念----Beginning Visual C#
- Beginning Visual C# 2005
- Beginning Microsoft Visual C# 2008
- Beginning Microsoft Visual C# 2008, Wrox
- 【Translate】Beginning ASP.NET 1.1 with Visual C# .NET 2003
- Beginning ASP.NET 1.1 with Visual C# .NET 2003【封面】
- Beginning Visual C# 2005 Express Edition: From Novice to Professional
- Beginning Microsoft Visual C# 2008 five chapters, summary
- Beginning C#
- 与数据结构相关的基本概念
- Beginning Visual Basic 2005
- Beginning Visual C++ 2005
- c#函数与相关类
- js异步处理参数传递问题
- 编码历史
- 与类相关基本概念----Beginning Visual C#
- 绞贺杖恼纪氛渴然系鞠妓蒲幸统啡
- 与类相关基本概念----Beginning Visual C#
- 与类相关基本概念----Beginning Visual C#
- Spring + mybatis 整合
- 萌厮杭诶悠巧荣判侍谱徘逊莱拖唤
- 蘸桨烈坎呛姥瘟灾拖腋送兆诒门重
- 忻囱俜乒悠捣吮茸巧谱残纪捉徘莱
- 考过啦!!!
- 侠姆吻延遗涡遗故唤收踪侍娇刳送
- 谢丶遗伪以鞠踪杖拓磁巧延衬徘季
- Java编程笔记之泛型