struct 不支持继承,所有的struct都继承自System.ValueType.但是需要小心的是struct可以实现接口。
C++中,class是支持public和private继承的。但是C#并不支持private 继承,所有的都是公有继承:
class MyDerivedClass : MyBaseClass,IInterface1,IInterface2
{
}
对于struct,就是这样定义的:
public struct : IInterface1, IInterface2
{
//member
}
当把一个struct作为参数传递给方法时,会生成这个struct的拷贝,如果把类实例传递给方法时,这是传递的是类的reference。参考以下例子:
C# Copy Code
class TheClass
{
public string willIChange;
}
struct TheStruct
{
public string willIChange;
}
class TestClassAndStruct
{
static void ClassTaker(TheClass c)
{
c.willIChange = "Changed";
}
static void StructTaker(TheStruct s)
{
s.willIChange = "Changed";
}
static void Main()
{
TheClass testClass = new TheClass();
TheStruct testStruct = new TheStruct();
testClass.willIChange = "Not Changed";
testStruct.willIChange = "Not Changed";
ClassTaker(testClass);
StructTaker(testStruct);
Console.WriteLine("Class field = {0}", testClass.willIChange);
Console.WriteLine("Struct field = {0}", testStruct.willIChange);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
/* Output:
Class field = Changed
Struct field = Not Changed
*/
虚方法
在C#的父类里定义一个虚方法时(virtual),意味着子类可以重载这个方法,java中没有相应的实现,c#中的虚方法是有方法体的。而且子类中如果要重载这个方法,是要加override这个修饰符的。
同样也可以定义虚的属性,如:
public virtual string ForeName
{
get; set;
}
Abstract Class
同java,abstract class不能被实例化,abstract function不能有具体实现,含有多于一个的abstract function都必须定义为abstract class.
Sealed Classes &Mehods
类似于java中的final. Sealed class 不允许被继承, Sealed method不允许被override. string 就是一个sealed class.
构造函数继承体系
当create一个子类的对象时,首先会生成父类的对象,也就是会调用父类的构造函数,默认的会是父类
的缺省构造函数,指定除外。
看下面的代码:
class Base
{
private string name;
public Base() {
name = "default";
Console.WriteLine("Bsse()");
}
public Base(string name) {
this.name = name;
Console.WriteLine("Base(name)");
}
}
class Child: Base
{
private string id;
public Child(): base() {
id = "0";
Console.WriteLine("Child()");
}
public Child(string id):base(id) {
this.id = id;
Console.WriteLine("Child(id)");
}
}
注意c#里子类的构造函数显示的调用了父类的构造函数。
public Child(): base()//其实这里base()可以省略,因为默认的情况下子类在初始化时
都要调用父类缺省构造函数。也可以特意指定调用父类的哪个构造函数,如:
public Child(string id):base(id) //这里就会调用父类的public Base(string name)。
修饰符
可见性修饰符
Modifier | Applies To | Description |
public | Any types or members | The item is visible to any other code. |
protected | Any member of a type,also any nested type | The item is visible only to any derived type. |
internal | Any member of a type,also any nested type | The item is visible only within its containing assembly. |
private | Any types or members | The item is visible only inside the type to which it belongs. |
protected internal | Any member of a type,also any nested type | The item is visible to any code within its containing assembly and also to any code inside a derived type. |
其他修饰符
Modifier | Applies To | Description |
new | Function members | The member hides an inherited member with the same signature |
static | All members | The member does not operate on a specific instance of the class. |
virtual | Classes and function members only | The member can be overridden by a derived class |
abstract | Function members only | .A virtual member that defines the signature of the member, but doesn ’ t provide an implementation. |
override | Function members only | The member overrides an inherited virtual or abstract member. |
sealed | Classes, methods, and properties | For classes, the class cannot be inherited from. For properties and methods, the member overrides an inherited virtual member, but cannot be overridden by any members in any derived classes. Must be used in conjunction with override . |
extern | Static [DllImport] methods only | The member is implemented externally, in a different language. |