C# 的继承体系

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)。

 

修饰符

可见性修饰符

ModifierApplies To Description
publicAny types or membersThe item is visible to any other code.
protectedAny member of a type,also any nested typeThe item is visible only to any derived type.
internalAny member of a type,also any nested typeThe 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 internalAny member of a type,also any nested typeThe item is visible to any code within its containing assembly and also to any code inside a derived type.

其他修饰符

ModifierApplies To Description
newFunction membersThe member hides an inherited member with the same signature
staticAll membersThe member does not operate on a specific instance of the class.
virtualClasses and function members onlyThe member can be overridden by a derived class
abstractFunction members
only
.A virtual member that defines the signature of the member,
but doesn ’ t provide an implementation.
overrideFunction members
only
The member overrides an inherited virtual or abstract
member.
sealedClasses, 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 .
externStatic [DllImport]
methods only
The member is implemented externally, in a different
language.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值