zoukankan      html  css  js  c++  java
  • C# 类与接口的几个问题的总结(待续)

    1. 有关类的多态性

    C#中,类的多态性是通过在子类中重载基类的虚方法(virtual标识)或函数成员来实现。

    在C#中,继承、虚方法和重写方法组合在一起才能实现多态性。

    2. 显式接口成员的使用

    显式接口成员属于接口的成员,而不是类的成员,因此,不能使用类对象直接访问,而只能通过接口对象来访问。

    如:

    interface IwgInterface1
    {
      int Add();
    }
    
    interface IwgInterface2
    {
      int Add();
    }
    
    class MyClass: IwgInterface1,IwgInterface2
    {
      int IwgInterface1.Add() // 显式接口的实现d
      {
        int x=3;
        int y=5;
        return x+y;
      }
    
      int IwgInterface2.Add()
      {
        int x=3;
        int y=5;
        int z=7;
        return x+y+z;
      }
    }
    
    class Program
    {
      static void Main(string[] args)
      {
        MyClass myClass = new MyClass();
        IwgInterface1 my_itf1 = myClass;  // 使用接口继承类的对象实例化接口
        IwgInterface2 my_itf2 = myClass;
    
        Console.WriteLine(my_itf1.Add()); // 8
        Console.WriteLine(my_itf2.Add()); // 15
      }
    }

    3. 抽象方法与抽象类

    当从抽象类派生一个非抽象类时,需要再非抽象类中重写抽象方法,以提供具体的实现。重写抽象方法时,使用override关键字。

    抽象类的示例如下:

    public abstract class wgClass

    {

       public abstract void method1(); //抽象方法

    }

    public class Child: wgClass

    {

      public override void method1()

      {

        Console.WriteLine("0...0");

      }

    }

    4. 抽象类与接口

    抽象类和接口都包含可以由派生类继承的成员,他们都不能直接实例化,但可以声明它们的变量。如果这样做,就可以使用多态性把继承这两种类型的对象指定给它们的变量。接着,通过这些变量来使用这些类型的成员,但不能直接访问派生类的其它成员。

    两者的区别:

    抽象类的派生类只能继承一个基类, 但可以继承任意多个接口。

    抽象类中可以定义成员的实现,但接口中不可以。

    抽象类中可以包含字段、构造函数、析构函数、静态成员或常量等,接口不可以。

    抽象类中的成员可以是私有的(只要它们不是抽象的),受保护的,内部的或者受保护的内部的成员(受保护的内部的成员只能在应用程序类本身或派生类中访问),但接口中的成员必须是公开的。

    抽象类主要用于对昂系列的基类,共享某些主要特性。如共同的目的和结构。接口则主要用于类,这些类在基础水平上有所不同,但仍可以完成某些相同的任务。

    5. 密封类与密封方法

    密封类可以用来限制扩展性,如果弥生了某个类,则其它类不能从该类继承;如果是成员,则派生类不能重写该成员的实现。密封可以防止对库的类型和成员进行自定义。

    满足以下条件,即可密封一个类:

    类是静态类

    类包含带有安全敏感的继承的受保护的成员。

    类继承多个虚成员,并且密封每个成员的开发和测试开销明显大于密封整个类。

    类是一个要求使用反射进行快速搜索的属性。密封属性可以提高反射在检索属性时的性能。

    密封方法,例

    public class Class1

    {

      public virtual void Method()

      {

        Console.WriteLine("0...0"); //基类中的虚方法

      }

    }

    public sealed class MyClass: Class1

    {

      public sealed override void Method()

      {

        base.Method();

        Console.WriteLine("密封类中重写后的方法");

      }

    }

  • 相关阅读:
    批量新增百万条数据 十百万条数据
    sqlserver 组内排序
    EF ++属性会更新实体
    Entity Framework Core: A second operation started on this context before a previous operation completed
    abp Cannot access a disposed object. A common cause of this error is disposing
    abp xunit Can not register IHostingEnvironment. It should be a non-abstract class. If not, it should be registered before.”
    hangfire enqueued but not processing(hangfire 定时任务入队列但不执行)
    EF 更新实体 The instance of entity type 'BabyEvent' cannot be tracked because another instance
    datatable to entiy list 不支持可空类型和枚举类型
    webapi 设置不显示接口到swaggerUI
  • 原文地址:https://www.cnblogs.com/arxive/p/5972082.html
Copyright © 2011-2022 走看看