C#的Class的基礎應用_建構子_多載






物件導向程式設計(OOP):即是為了方便開發者去建立物件透過物件互動來完成任務或工作的設計。

要建立物件前須先知道class

Class主要分成兩部分

第一部分 : 屬性(property)

class具有的性質(大多用變數來記錄)


第二部分 : 方法(method)  

這個class具有的能力、行為(大多用function)




class Student
{
   //學生這個物件的屬性
   int  Student ID;
   String  Name;
   int Grade;
   //學生物件的方法
   Public Void Walk(); 
   Public String Say();
}

class就是一個物件的設計藍圖

我們可以用這個設計圖去產生出多個物件(object)

這裡我們新增一個  Student 的 Class



注意!!

只宣告Student的變數並不會產生物件,後面要記得new 一個Class名稱()

示意圖:

寫法1. 兩行拆解_先宣告再建立

Student s ; //先宣告一個物件

s = new Student(); //再建立物件實體
==========================================================

寫法2.  一行搞定

Student s = new Student();



再來一個不接收任何輸入值得例子

我們在類別部分添加一個method

做  integer++





有接收輸入值method寫法





若每次都要跟 2年級的阿陳說些話  就要打很多次
年級    名字

而且還有可能打錯阿陳的名字

這時  物件導向的概念就來了  我們就再多新增一個 阿陳的object

指派一次 就會被 永遠記起來 再也不用重複打好多 2年級的’2’
又或者是 阿陳這個 名字字串







主程式code  Programe.cs

程式碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace class_exercise
{
    class Program
    {
        static void Main(string[] args)
        {
            //我們會需要一個變數來存取物件!!!
            Student kunyu = new Student();//用Student類別產生一個物件名稱為kunyu
            kunyu.name = "冠羽";
            kunyu.Grade = 4;

            //使用物件中的method
            Console.WriteLine(kunyu.Say());
            //較不好的寫法
            //Console.WriteLine(kunyu.Talk(2,"阿陳"));
            //物件導向概念的寫法
            Student ochen = new Student();
            ochen.name = "阿陳";
            ochen.Grade = 2;
            Console.WriteLine(kunyu.Talk(ochen));

            /*
            Console.WriteLine("\n");
            kunyu.Update();
            Console.WriteLine(kunyu.Say());

            Console.WriteLine("\n");
            kunyu.Update();
            Console.WriteLine(kunyu.Say());

            Console.WriteLine("\n");
            kunyu.Update();
            Console.WriteLine(kunyu.Say());
            */
            Console.ReadKey();
        }
    }
}


類別的code Student.cs

程式碼


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace class_exercise
{
    class Student
    {
        //property
        public int studentID;
        public string name;
        public int Grade;

        //method

        // public output型別 method名稱(input型別名稱與名稱)

        ////不接受任何input型別與名稱 會 回傳字串
        public string Say()
        {
            ////回傳後方字串
            return "我叫" + name + "我是" + Grade + "年級的學生";                
        }
        ////不接受任何input型別與名稱 也不 回傳
        public void Update()//學生會升年級
        {
            Grade++;
        }
        //有接受輸入值
        public string Talk(Student g)//輸(傳)入對方學生的資料
        {//Talk接收了兩個輸入值一個sgrade、一個sname
            return Grade + "年級的" + name 
                + "對" + g.Grade + "年級的" + g.name + "說你好";
        }
    }
}






建構子(Constructor)


1.是一種Method

2.會在物件被建立的時候執行!!!!!!



建構子的特徵

1.沒有 output型別

2.名稱 與class名稱相同


一般 Class中的 method 是有輸出型別的喔



我們的  class 區塊多添加一個 建構子



主程式區塊

建立了物件之後( new Student() 之後 )

建構子中的程式碼也馬上被執行


那學  建構子 到底要幹麻?????


假設一個情境  你的老師   想要教你幫忙他做一個學生資料統整表


一個校園學生系統  好了   有一堆入學學生的年級和學號資訊


這時你會怎麼做???


這時  建構子 就發揮他的功能了

建構子會在物件被建立的時候執行!!!!!!


今天一整批上一屆學生從大一升到大二

假設  100  個  學生

我們要一口氣去改   400行


我們就到  建構子區塊

如果知道 某個屬性的初始值為多少
就可以寫在建構子內,讓物件預先設定好


可是你說  那我的程式碼還是要重複寫爆多行阿

我還有 name 跟 學生ID耶!?!?!

而且每個學生都不同的name 和 ID

這又要如何解決呢???


一樣   建構子  可協助我們來縮短程式碼

我們在  建構子 區塊

對它 進行指定 輸入型別 及 輸入的名稱



this  關鍵字

會跟電腦去說  這個是成員資料(Data member)/變數又稱欄位

而非方法的參數!!!

this.  --->  念作  這個物件的

this.name  --->  學生這個物件的name

this  這個變數會跟隨物件建立的當下而產生!!!



當你回主程式時就會發現  ㄟ!!!!
多出了輸入參數了
原來 建構子   後面這個   new Student( )  有小括弧的東西

其實就是一個有輸入值得函數



建構子的input 需在建立物件時給予~~~



主程式code Programe.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace class_exercise
{
    class Program
    {
        static void Main(string[] args)
        {
            Student s1 = new Student(10601 , "冠羽");
            Console.WriteLine(s1.Say());

            Student s2 = new Student(10602  , "威志");
            Console.WriteLine(s2.Say());

            Console.ReadKey();
        }
    }
}



類別的code Student.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace class_exercise
{
    class Student
    {
        //property
        public int studentID;
        public string name;
        public int Grade;

        //method
        public Student( int  studentID , string name)
        {
            //this  關鍵字 指明的是物件本身的欄位(Property)而非參數
            this.studentID = studentID;
            this.name = name;
            Grade = 2;
        }

        // public output型別 method名稱(input型別名稱與名稱)

        ////不接受任何input型別與名稱 會 回傳字串
        public string Say()
        {
            ////回傳後方字串
            return "我叫" + name + "我是" + Grade + "年級的學生";                
        }
        ////不接受任何input型別與名稱 也不 回傳
        public void Update()//學生會升年級
        {
            Grade++;
        }
        //有接受輸入值
        public string Talk(Student g)//輸(傳)入對方學生的資料
        {//Talk接收了兩個輸入值一個sgrade、一個sname
            return Grade + "年級的" + name 
                + "對" + g.Grade + "年級的" + g.name + "說你好";
        }



    }
}



你又會問  建構子的輸入參數難道都會固定為兩個輸入嗎???

我們可以到 class區塊去多添一個方法一樣叫做Student
的建構子

只是多添一個參數是年級

可以讓我們自由調整   不同年級   改為一個有彈性的寫法
























這就是建構子的輸入參數彈性(不固定輸入數量)寫法!!!!

這種

method 名稱一樣

但是 輸入值的數量不同或型別不同的寫法

我們就稱之為  overloaded



主程式code Programe.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace class_exercise
{
    class Program
    {
        static void Main(string[] args)
        {
            Student s1 = new Student(10601 , "冠羽");
            Console.WriteLine(s1.Say());

            Student s2 = new Student(10602  , "威志");
            Console.WriteLine(s2.Say());

            Student s3 = new Student(10602, "小新" , 3);
            Console.WriteLine(s3.Say());

            Student s4 = new Student(10602, "振偉", 4);
            Console.WriteLine(s4.Say());

            Console.ReadKey();
        }
    }
}


類別的code Student.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace class_exercise
{
    class Student
    {
        //property
        public int studentID;
        public string name;
        public int Grade;

        //method
        public Student( int  studentID , string name)
        {
            //this  關鍵字 指明的是物件本身的欄位(Property)而非參數
            this.studentID = studentID;
            this.name = name;
            Grade = 2;
        }
        public Student(int studentID, string name,int grade)
        {
            //this  關鍵字 指明的是物件本身的欄位(Property)而非參數
            this.studentID = studentID;
            this.name = name;
            Grade = grade;
        }




        // public output型別 method名稱(input型別名稱與名稱)

        ////不接受任何input型別與名稱 會 回傳字串
        public string Say()
        {
            ////回傳後方字串
            return "我叫" + name + "我是" + Grade + "年級的學生";                
        }
        ////不接受任何input型別與名稱 也不 回傳
        public void Update()//學生會升年級
        {
            Grade++;
        }
        //有接受輸入值
        public string Talk(Student g)//輸(傳)入對方學生的資料
        {//Talk接收了兩個輸入值一個sgrade、一個sname
            return Grade + "年級的" + name 
                + "對" + g.Grade + "年級的" + g.name + "說你好";
        }



    }
}




留言

張貼留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題