c#继承

1使用new运算符隐藏基类成员:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace InhirateTest

{

    class BaseClass

    {

        public string fielda = "in baseclass";

        public static int identity = 100;

        public static void baseTest()

        {

            Console.WriteLine("test base of BaseClass");

        }

        public void sayHi()

        {

            Console.WriteLine("the sayHi of BaseClass");

        }

    }

}

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace InhirateTest

{

    class ChildClass:BaseClass

    {

        new public string fielda = "in childClass";

        new public static int identity = 200;

        new public void sayHi()

        {

            Console.WriteLine(base.fielda);

           

            Console.WriteLine("the sayHi of ChildClass");

        }

        

    }

}

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace InhirateTest

{

    class Test

    {

        static void Main(string[] args)

        {

            ChildClass childClass = new ChildClass();

            Console.WriteLine(childClass.fielda);

            Console.WriteLine(ChildClass.identity);

            

            childClass.sayHi();

        }

    }

}

 

如果不添加new修饰符,程序仍然能够编译并可以正确运行,但是编译器会引发一个警告,提示是否是有意隐藏基类的成员。

2在派生类类中指定基类的构造函数形式为

 Public childClass(paramtype param):baseClass(paramtype param){

}

可以使用base指定基类中的构造函数,基类中的构造函数在重载的情况下。

使用this指定使用当前类构造函数。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ThisTest

{

    class BaseClass

    {

        public BaseClass()

        {

            Console.WriteLine("construtor in baseClass");

        }

        public BaseClass(string str)

        {

            Console.WriteLine("construtor in baseClass {0}",str);

        }

    }

}

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ThisTest

{

    class ChildClass:BaseClass

    {

        public ChildClass()

        {

            Console.WriteLine("construtor in childClass");

        }

        public ChildClass(string str) : base(str) 

        {

            Console.WriteLine("construtor in childClass {0}",str);

        }

        public ChildClass(string str, int val) : this(str) 

        {

            Console.WriteLine("construtor in ChildClass {0}, {1}",str,val);

        }

    }

}

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ThisTest

{

    class ThisTest

    {

        static void Main(string[] args)

        {

            ChildClass childClass = new ChildClass("hell world",100);

           

        }

    }

}

 

 

3程序集即相对于java中的包是类的物理组织形式,命名空间是类的逻辑组织形式,只有指定类的程序集,命名空间才能找到该类。不加任何修饰符的情况下类的声明为内部的只能有同一程序集中的类型访问 internal

4跨程序集的继承时必须先在该项目中添加对于该类的引用以后才能在另外的程序集中成功出现对该类的被选择提示 using namespace;否则只能创建该类型

5使用sealed关键字可以把类声明为密封类,即该类不可以被继承相当于java中的final

6静态类不能使用sealedabstract修饰符

必须直接继承system.object类不能是任何类的派生类

静态类只能包含静态成员

不能被实例化

是密封函数不能被继承

可以包含静态构造函数,不能包含实例构造函数

静态类中的所有方法都必须为静态方法,使用静态类目的就是为了使用其中的静态方法,实际上就是使用其中的静态工具方法。使用工具方法执行其他的操作。

7扩展方法的使用

扩展方法必须是静态方法,必须定义于静态类中

扩展方法第一修饰符以this为前缀,后跟要扩展的目标类型,和形参

扩展方法只针对实例调用,目标类不能为静态类

如果扩展方法和被扩展类中某个方法签名相同那么扩展方法永远都不能被调用。

使用扩展类可以实现不修改类的前提下添加类的功能而且扩展方法可以在其他类的对象上像调用实例方法一样调用,虽然扩展方法是静态方法但是却可以应用于其他类对象上像调用成员方法一样被调用。

包含扩展方法的静态 类实际上就是一种静态工具类,其作用就是为其他类提供一个静态扩展方法,添加新功能。使用形式如下:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ExtendMethodTest

{

    class Program

    {

        static void Main(string[] args)

        {

            string word = "to count the number of the words";

            Console.WriteLine(word.WordCount());

            Console.WriteLine(word.ToString());

        }

    }

    public static class MyExt

    {

        ///<summary> 

        ///static method extend to count the number of word

        ///</summary>

        public static int WordCount(this string str) 

        {

            return str.Split(new char[]{' ','.','?'},StringSplitOptions.RemoveEmptyEntries).Length;

        }

        ///<summary>

        ///the method tostring is not execute

        ///</summary>

        public static string ToString(this string str) 

        {

            return "my ToString()";

        }

        

    }

}

 

 

8派生类向上转型时那么派生类中的新增数据成员和成员方法对基类的引用就不可见啦,

 

原文地址:https://www.cnblogs.com/moonfans/p/2789426.html