ASP.NET MVC 从入门到放弃 (Day7) -C#物件导向介绍(封装 继承 多型

接着来讲讲常用的物件导向一些基本概念....

封装

可能你知道套件函式名称,但不知道里面是什麽就叫封装,简单来讲就是写一个funtion 包成dll 去给别人使用 包成dll的这个动做就叫封装..使用者不会知道dll是在内容什麽(看不到)。/images/emoticon/emoticon06.gif

ex:

Class1 aa = new Class1();
double Sum = aa.Total(20);

注解:呼叫Total这个函式但看不到函式内容,就叫封装

继承

继承让程序码可以重复使用,简单来讲你只要继承这个class 那麽该class的funtion就可以使用了,可以把常用的funtion写在一个class让其他class去继承它,就可以不用一直写重复的程序码。

ex:

public class HomeController : Controller
{
       public ActionResult Index()
        {
            return View();
        }
}

注解:这个是MVC 的Controller内容 : Controller代表它去继承 Controller功能 就可以使用ActionResult之类的功能...。
如果要继承该Class就在原本的Class後面加上冒号 :

多型

多型有分2种一种是方法多载一种是运算子多载

方法多载:简单来讲就是会有一个全部都是funtion名称的Class 搭配 另外一个Class都是函式内容

ex:
介面方法class

interface IInterface
{
    Result PutMember(string memberId, string memmberName, string connectionString);
    Result PostMember(string memberId, string memmberName, string connectionString);
}

注解:命名规则通常第一个字母会是英文字的I

介面函式class

public class MyInterface : IInterface
{
   public Result PutMember(string memberId, string memmberName,string connectionString)
   {
      //....内容省略
   }

   public Result PostMember(string memberId, string memmberName,string connectionString)
   {
      //....内容省略
   }
}

使用介面 Class用法

Models.Repositories.IInterface MyDB = new Models.Repositories.MyInterface();

[HttpPut("PutMember")]
public Result PutMember(string memberId, string memmberName)
{
   var Config = new Config();
   Config.connectionString = _config.GetValue<string>("connectionString");
   var result = MyDB.PutMember( memberId,  memmberName, Config.connectionString);
   return result;
}

注解:用途在於当函式一多就可以直接去介面方法class看函式名称即可,不用特别去看函式内容方便程序撰写。/images/emoticon/emoticon28.gif

运算子多载:指在一个类别(class)中,定义多个名称相同,但参数(Parameter)不同的方法(Method)。

ex:

public void Total (){
//....内容省略
}
public void Total (int a){
//....内容省略
}

public void Total (int a,int b){
//....内容省略
}

4.覆写(Override)是指子类别可以覆写父类别的方法内容,使该方法拥有不同於父类别的行为。

public virtual string aa (){
  return "aa";
}

public override string aa (){
  return "dd";
}

注解:简单来讲当函式有virtual 就可以使用override去改写它的函式内容。/images/emoticon/emoticon33.gif


<<:  【Day 12】Python os._exit()和 sys.exit()

>>:  Android学习笔记03

Day3 资料储存 - block storage优缺点及场景

优缺点 优点 Block storage最大的优点就是他使得计算与储存分离,我们能轻易地透过LUN ...

[重构倒数第22天] - 减少 watch,改用 computed

前言 该系列是为了让看过Vue官方文件或学过Vue但是却不知道怎麽下手去重构现在有的网站而去规画的系...

从零开始学游戏设计:游戏音效

这是 Roblox 从零开始系列,游戏环境章节的第五个单元,今天要来教你如何在游戏中的角色作出动作之...

马可夫模型

马可夫模型 (Markov Model) 会用来表达状态以及转移机率及它们的随机过程使用的模型,或许...

DAY17:清单元件之实作

今天要给大家看实例,接下来用图片介绍。 首先我们先在主画面建立三个清单元件 以及新增两个客制化的画面...