zoukankan      html  css  js  c++  java
  • 面向对象编程的特性

    一、接口

    接口是把公共的方法与属性组合起来,以封装特定功能的集合。

    接口不能单独存在,不能像实例化一个类一样实例化接口,且接口不能包含实现成员的任何代码,只能定义成员本身,

    实现过程只能在实现接口的类中实现。

    C#定义接口关键字为interface,

    例如一个人事管理系统中员工接口类

        public interface IEmployees
        {
            string Name{ get; set;}
            decimal GetPay();
        }
    

    注意:接口中的成员不能有修饰符,因为默认都是公有的,同时不能声明虚拟与静态的,接口一般都以I开头

    class Program
        {
            static void Main(string[] args)
            {
                IEmployees tempEmployees = new TempEmployees();
                tempEmployees.Name = "临时员工";
                IEmployees commonEmployees = new CommonEmployees();
                commonEmployees.Name = "普通员工";
                Console.Write("我是{0},工资为:{1}\n我是{2},工资为:{3}",tempEmployees.Name,tempEmployees.GetPay(),commonEmployees.Name,commonEmployees.GetPay());
                Console.ReadKey();
            }
        }
    
        public interface IEmployees
        {
            string Name { get; set; }
            decimal GetPay();
        }
    
        public class TempEmployees : IEmployees
        {
            private string _name;
            public string Name 
            {
                get { return _name; }
                set { _name = value; }
            }
            public decimal GetPay()
            {
                return 30 * 7 + 10;
            }
        }
    
        public class CommonEmployees : IEmployees
        {
            private string _name;
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
            public decimal GetPay()
            {
                return 30 * 30 + 100;
     
            }
        }
    
  • 相关阅读:
    洛谷 P1233 木棍加工
    洛谷 P3378 【模板】堆(小根堆)
    leetcode难度及面试频率
    设计模式大全
    多线程经典面试题
    查找子字符串----KMP算法深入剖析
    线程与进程的区别
    海量数据面试题----分而治之/hash映射 + hash统计 + 堆/快速/归并排序
    解析STL中典型的内存分配
    C++ 常量类型 const 详解
  • 原文地址:https://www.cnblogs.com/ljx2012/p/2692376.html
Copyright © 2011-2022 走看看