zoukankan      html  css  js  c++  java
  • Abstract Class 和 Interface用法

    abstract 修饰符可以和类、方法、属性、索引器及事件一起使用。在类声明中使用 abstract 修饰符以指示某个类只能是其他类的基类。标记为抽象或包含在抽象类中的成员必须通过从抽象类派生的类来实现。


    在此例中,类 Square 必须提供 Area 的实现,因为它派生自 ShapesClass:


    abstract class ShapesClass
    {
        abstract public int Area();
    }
    class Square : ShapesClass
    {
        int x, y;
        // 如果基类没有提供一个Area() 方法 就编译出错
        public override int Area()
        {
            return x * y;
        }
    }


    接口:只包含方法、委托或事件的签名。方法的实现是在实现接口的类中完成的,如下面的示例所示:

    interface ISampleInterface
    {
        void SampleMethod();
    }

    class ImplementationClass : ISampleInterface
    {
        // 执行中接口中方法 :
        void ISampleInterface.SampleMethod()
        {
            // 实施方法;
        }

        static void Main()
        {
            //声明接口的实例
            ISampleInterface obj = new ImplementationClass();

            // 访问方法
            obj.SampleMethod();
        }
    }

  • 相关阅读:
    设置 menu 菜单 监听
    在 flash.net.FileReference 上找不到属性 save,且没有默认值。
    swf获取当前页面的路径
    控制舞台缩放比例
    相对于自身的坐标位移
    tweenmax
    exe文件反编译为源文件
    spring 整合 spring mvc
    springmvc学习第四天
    springmvc学习第三天
  • 原文地址:https://www.cnblogs.com/xiaobaigang/p/830513.html
Copyright © 2011-2022 走看看