现在的位置: 首页 > 综合 > 正文

C#封装、多态、抽象、接口、匿名方法等学习

2018年05月20日 ⁄ 综合 ⁄ 共 2304字 ⁄ 字号 评论关闭

1:封装

   将对象进行封装,并不等于将整个对象完全包裹起来,而是根据实际需要,设置一定的访问权限,用户根据不同的权限调用对象提供的功能,在C#语言中,可以使用修饰符public、internal、protected、private分别修饰类的字段、属性和方法。

2:继承,主要是注意继承的格式

public class ParentClass  //父类
{
   public ParentClass();
}

public class ChildClass : ParentClass  :子类
{
   public ChildClass();
}

3:多态

    多态是面向对象编程思想的核心之一,多态的意思是:当从父类派生出了一些子类后,每个子类都由不同的代码实现,因此,同样的调用可以产生不同的效果,主要由子类来决定,在程序运行时,面向对象的语言会自动判断对象的派生类型,并调用相应的方法。

   在C#中多态分为两种类型,即编译时多态和运行时多态,编译时多态是通过重载来实现的,运行时多态是系统运行时根据实际情况来决定何种操作。

实例代码:

public class MyIntertest 
{
   public virtual void interest() 
   {
      Console.WriteLine("我的爱好");
      Console.ReadLine();
   }
}

public class Writer : MyIntertest
{
    public override void Interest() 
    {
       Console.WriterLine("写作");
    }
}

public class Program : MyInterest
{
    public override void interest() 
    {
        Console.WriteLine("编程");
    }
}

public class Sing : MyInterest 
{
    public override void interest() 
    {
        Console.WriteLine("唱歌");
    }
}


public class Test 
{
   public static int main(String[] args) 
   {
       MyInterest[] interests = new MyInterest[4];
       interests[0] = new Write();
       interests[1] = new Program();
       interests[2] = new Sing();
       interests[3] = new MyInterest()
       
       foreach ( MyInterest interest in interests)
       {
           interest.interest();
       }
       
       return 0;
   }
}

4:

接口:
   界面,是两个有界事物的交界处,通过界面可以与对方交互,交互的方式由约定产生,这种约定就是由具体交互动作的规则,只要遵循规则
即可与拥有规则的事物交互。
   在C#中,接口是声明类具有的方法和成员的、自身不能被实例化的、只能被继承的特殊的类,其只管描述事物的特性而实现特性的任务却由其他类负责。
   接口中不能包含字段
   接口定义了类要实现的一系列方法,但是它本身不能实现这些方法,只是用逻辑结构方式来描述。
   
   在接口的成员中,只能是方法、属性、事件或者索引器,它的成员不能包含常数、字段、运算符、实例构造函数、析构构造函数或类型,也不能包含任意种类的
   静态成员。
   

 interface MyInterface
   {
      string STR    //属性STR
      {
        get;       //属性可读
        set;       //属性可写
      }    
      void outMethod();   //成员方法outMethod
   }
   
   class InheritInterface : MyInterface
   {
      string str = "21天学习C#";    //定义并初始化字符串
      public string STR
      {
         get
         {
            return str;
         }
         set
         {
            str = value;
         }
      }  
      public void outMethod()
      {
         Console.WriteLine(this.STR);
      } 
   }

5: 域和属性
    域也叫成员变量,它可以用来保存类的各种信息,域可以分为静态域和实例域两种,其中,静态域属于类,实例域属于对象。
    属性是一种用来访问对象的特殊成员。
    
    域的声明:和普通变量相同,可以用new, public, protected, static和readonly等
    
   

 public class Date
    {
       private string name = "hoan";
       public string nameAttr 
       {
         get  //使用get访问器读值
         {
            return name;
         }
         set  //使用set访问器写值
         {
            
         }
       }
    }

6:匿名方法:
   匿名方法的作用是将代码块当作参数来使用,使代码对于委托的实例很直接,很便利,可以减少实例化委托对系统资源的开销。匿名方法还共享了
   本地语句包含的函数成员访问权限。
   
   匿名方法的使用:
    

 namespace codemo
     {
        delegate void Writer(string s);  //委托所调用的命名方法
        class Program 
        {
            static void NamedMethod(string k)   //委托所调用的命名方法
            {
               System.Console.WriteLine(k); 
            }
            static void Main(string[] args) 
            {
               Writer w = delegate(string j)  //委托类调用匿名方法
               {
                  System.Console.WriteLine(j);
               }
               w("调用了匿名方法");
               Console.WriteLine();
               w = new Writer(NamedMethod);  //创建对象
               w("调用了命名方法");          //调用命名方法
               Console.ReadLine();     
            } 
        } 
     }

【上篇】
【下篇】

抱歉!评论已关闭.