0

## C# 入門筆記02

• 封裝
• 繼承
• 多型

`void作為方法的傳回類型或區域函數，來指定方法不會傳回值。`
`double作為使用計算浮點數值型別類型`

``````    class Rectangle
{
// member variables
double length;
double width;
public void Acceptdetails()
{
length = 10.0;
width = 20.1;
}

public double GetArea()
{
return length * width;
}

public void Display()
{
Console.WriteLine("Length: {0}, Width:{0}, Area:{0}", length, width, GetArea());
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
}
}
``````

``````Length: 10,
Width: 20.1,
Area: 201
``````

``````using System;
namespace RectangleApplication
{
class Rectangle
{
//member variables
internal double length;
internal double width;

double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}//end class Rectangle

class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.length = 12.5;
r.width = 8.25;
r.Display();
}
}
}
``````

``````Length: 12.5
Width: 8.25
Area: 103.125
``````

`建立物件的方式為：new Class名稱()`

1. 創立一個空白專案，並將class命名為Student
2. 宣告變數
3. 建立Method
``````public class Student
{
//宣告變數
public int StudentID;
public string Name;

//Method
public string Say()
{
}
public string Talk(int sgrade, string sname)
{
}
}
``````
1. 創立一個form表單
2. 引入Method的專案
※(假設Method為`namespace Demo`，那表單引入方法即為:`using Demo;`)
3. 呼叫函式
`````` private void button1_Click(object sender, EventArgs e)
{
Student s = new Student();
s.Name = "老王";

MessageBox.Show(s.Talk(2, "小明"));
//3年級的老王對2年級的小明說Hello
MessageBox.Show(s.Say());
//我叫老王，我是３年級的學生
}
``````

1. 製作一個max的function，分別帶入２個參數
``````//method
public int max(int a, int b)
{
int result;

if (a < b)
{
result = b;
} else
{
result = a;
}
return result;
}
``````
1. 宣告int a, b, c三個變數
2. 分別賦予變數值
``````private void button1_Click(object sender, EventArgs e)
{
//建立一個新物件m
Person m = new Person();
int a = 10;
int b = 20;
int c = m.max(a, b);

//Number前面加""，是強制將數字轉為字串
MessageBox.Show("" + c);　//20
}
``````

1. 創建Name、Money的變數
2. 創建一個borrow和repay的function做邏輯判斷，判斷教學寫在程式碼內
``````    class Person
{
//創建變數
public String Name;
public int Money;

public Person(String name, int money)
{
this.Name = name;
this.Money = money;
}

public void borrow(Person lender, int money)
{
//如果朋友的錢 >= 我借的數量
if (lender.Money >= money)
{
lender.Money -= money;　//跟朋友借錢，則朋友的戶頭會減少
this.Money += money;　//我的戶頭增加
}
}
public void repay(Person lender, int money)
{
//如果我的錢 >= 我借的錢
if (this.Money >= money)
{
this.Money -= money; //還給朋友錢時，自己的戶頭會減少
lender.Money += money;　//朋友的戶頭錢變多
}
}
}
``````
1. 創建2個物件，分別為my、friend
2. 設定Enabled按下提交後，是否能夠做修改
``````    public partial class Form1 : Form
{
Person my, friend;
public Form1()
{
InitializeComponent();
}
private void submit_Click(object sender, EventArgs e)
{
my = new Person(myInput.Text, 0);
friend = new Person(friendInput.Text, 20000);

myInput.Enabled = false;
friendInput.Enabled = false;
submit.Enabled = false;
borrowButton.Enabled = true;
repayButton.Enabled = true;

borrowButton.Text = \$"跟{friend.Name}借了\$1000";
repayButton.Text = \$"償還{friend.Name}\$1000";

myNameLabel.Text = \$"{my.Name}";
friendNameLabel.Text = \$"{friend.Name}";
}
private void borrowButton_Click(object sender, EventArgs e)
{
my.borrow(friend, 1000);
updateMoney();
}
private void repayButton_Click(object sender, EventArgs e)
{
my.repay(friend, 1000);
updateMoney();
}
private void updateMoney()
{
myMoneyLabel.Text = "" + my.Money;
friendMoneyLabel.Text = "" + friend.Money;
}
}
``````