zoukankan      html  css  js  c++  java
  • 接口与抽象类

    前几天又有人不知道接口与抽象类的区别,我回头就做了这个例子。今天发布在我的空间中。

    首先我们来解释下抽象类与接口:接口只能有方法签名,属性签名而在抽象里面你可以把设计一个抽象方法同时这个抽象类也支持完整有业务逻辑块的方法,抽象类不能实例化,所以大家注意下如果有方法语句块需要把方法声明成静态的。

    最后我来帮大家解释下它们两者之间的什么时候需要使用接口什么时候选择使用抽象类,什么时候选择使用接口。

    条件如下:

    当一个方法要使用多次的时候就选择使用接口。

    当一个方法只需要使用一次的时候选择使用抽象类

    接下来我们来看看它们之间的代码。

    三个CS文件,一个winform窗体

     

    接口

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace WindowsFormsApplication2
     6 {
     7     interface ITeacher//跨程序集使用必须使用修饰符public
     8     {
     9         string Name();
    10         int Age();
    11     }
    12 }
    13 

    抽象类

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace WindowsFormsApplication2
     6 {
     7     abstract public class Student//一样必须使用修饰符
     8     {
     9         public static string Name()
    10         {
    11             return "XiaoLongZhang";
    12         }
    13         public static int Age()
    14         {
    15             return 90;
    16         }
    17         abstract public string Job();
    18         abstract public string Sex();
    19     }
    20 }

    类继承接口与抽象类。

     

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace WindowsFormsApplication2
     6 {
     7     public class Person : Student, ITeacher
     8     {
     9 
    10         public override string Job()
    11         {
    12             return "教师";
    13         }
    14 
    15         public override string Sex()
    16         {
    17             return "";
    18         }
    19 
    20         #region ITeacher 成员
    21 
    22         string ITeacher.Name()//如果是跨程序集使用,必须在接口那修改访问级别(注:在接口类的方法前不要加修饰符)
    23         {
    24             return "张晓龙";
    25         }
    26 
    27         int ITeacher.Age()
    28         {
    29             return 23;
    30         }
    31 
    32         #endregion
    33     }
    34 }
    35 

    WinForm窗体

     

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 namespace WindowsFormsApplication2
    11 {
    12     public partial class Test : Form
    13     {
    14         
    15         public Test()
    16         {
    17             InitializeComponent();
    18         }
    19 
    20         private void btnAbstract_Click(object sender, EventArgs e)
    21         {
    22             Person _person = new Person();
    23             this.txtAbstractName.Text = Student.Name();
    24             this.txtAbstractAge.Text = Convert.ToString(Student.Age());
    25             this.txtAbstractJob.Text = _person.Job();
    26             this.txtAbstractSex.Text = _person.Sex();
    27         }
    28 
    29         private void btnInterface_Click(object sender, EventArgs e)
    30         {
    31             ITeacher _IMyTeacher = new Person();
    32             this.txtInterfaceName.Text = _IMyTeacher.Name();
    33             this.txtInterfaceAge.Text = Convert.ToString(_IMyTeacher.Age());
    34         }
    35     }
    36 }
    37 
  • 相关阅读:
    Python NLP入门教程
    一个月入门Python爬虫,轻松爬取大规模数据
    Python爬虫实战案例:爬取爱奇艺VIP视频
    探索Python F-strings是如何工作
    Ruby 和 Python 分析器是如何工作的?
    超级干货,python常用函数大总结
    Python 开发者的 6 个必备库,你都了解吗?
    神经网络中 BP 算法的原理与 Python 实现源码解析
    新手程序员必学的代码编程技巧
    零基础小白怎么用Python做表格?
  • 原文地址:https://www.cnblogs.com/XiaoLongZhang/p/1640460.html
Copyright © 2011-2022 走看看