C#從零開始_自學C#、Visual Studio實境秀 41/ Getting Started with C# Lesson 11~ of 2...



{ Lesson 11 of 20 Defining and Calling Methods http://bit.ly/2eJRaaM

{

9:00 Since the initial int parameter of the static method is preceded by the this modifier, you know this method is an extension method. Since the parameter is an int, it means the code may be used as a method on any int variable (or literal) you have.

13:00 引用擴充方法必須在同一個 namespace ,否則就要 using :Before using an extension method, your code will require a using statement for the namespace of your static class containing the extension method

19:00 練習就先略過了 17:00

}

20:00 Lesson 12 of 20 Understanding Classes and Objects  http://bit.ly/2tWa7ZB

{

21:10 強型別(強式類型) a strongly-typed language

23:10 type class 的區別就很明顯了 :The types you work with, both the built-in ones and your own custom types that you'll create, are defined (usually) by classes.

24:20 class 就如藍圖:You can think of a class as a definition or design of an object.

In C#, an instance of a class is called an object.

28:00 file that ends in ".cs" will be a class definition.

29:00 可見 class struct interface enum 都是同一層級的東西:You may also have some interfaces, structs, enums, or other elements, but classes usually make up the vast majority of C# applications.

34:00 靜態無權取用非靜態的 Within a console application's static main method, you don't have access the Program's non-static members (if any).

However, you can create an instance of Program from within the Main method, and then work with that instance and its members.

38:30 難怪感覺預設的方法就是 ToString() WriteLine will automatically call ToString for you internally

41:30 屬性 欄位 Properties and Fields

Fields are types that are attached to a class.

They track the state of the class, and separate instances of the same class will each track the data of their fields independently.

44:50 override Tostring()

49:00 屬性和欄位還是有分的 Properties provide a way for other objects to access state from an object in a controlled manner. Unlike fields, which are essentially just variables, properties are methods and can add additional behavior around manipulating the state of an object.

Unlike fields, which are essentially just variables, properties are methods and can add additional behavior around manipulating the state of an object.

屬性竟也是方法!欄位則基本上只是個變數或變量。

1:07:00 屬性 Properties are a C# feature that allows you to define special get and set methods for any state you want your objects to expose to other objects (or even for their own use).

get set 也都是 method

屬性有點像把數個方法(methods)合併成 get set 二個方法。

value is a keyword used in setters representing the new value

所以屬性裡的 value 也是個關鍵字

資訊安全 You should rarely expose fields from your classes, and instead use properties to control external access to your object's state.

Even then, you should limit such access, so that your object's collaborators do not become too tightly coupled to your object's internal implementation.

欄位盡量不要公開,給外部用。

1:14:00 自動實作屬性 :In cases where you don't need to perform any additional behavior, your property definitions can be very simple:

1:16:00 Constructors and Property Initializers

1:17:30 未初始化的 DateTime: will initially be DateTime.MinValue

1:19:00 constructor

建構子也是方法,也當如方法一樣處理的 business 單純、單一,不要太複雜:  1:22:00 business logic

1:20:00 You should avoid putting complex logic into constructors, but they're a good place to ensure fields and properties are initialized.

1:24:10 這裡的 logic 就好像我們一般口語上的內容;只不過因為是程式在用的,必有其 logic 所以用 logic

1:45:00 總之一個大原則,程式一定要注意可讀性、人性化!簡要、簡潔,跟寫文章一樣!都是在寫文章嘛,只是用不同的語言在寫而已。用英文寫要不要簡潔?要啊,那用程式語言寫就不用了嗎?

2:00:00 Putting property initialization code into constructors can result in a lot of extra code,

Putting property initialization code into constructors can result in a lot of extra code, and for larger classes there can be a lot of separation between where the property is declared and where its value is initialized. That's why C# recently added support for property initializers.

2:02:00 property initializers 的來源

Property initializers can be used to set the default value of properties at the same time they're declared.

在宣告時就同時設定了預設值

The syntax is straightforward: after the automatic property declaration, add an = initialValue; where initialValue is what you want the property to be.

the automatic property declaration 應就是 auto-implemented properly {get;set;}

這裡漏掉了建構子最重要的兩個特徵,因為它也是方法,但它不能有傳回值 type 且其 identifier(名稱) 也要和 class 一樣



1:48:00 因為 string 是 reference type 所以在未初始化時是 null,null 就是 null reference.

1:54:00 預計的建構子都是沒有參數的 By default, all classes have a default constructor that takes no parameters.

1:58:00 只要自訂了一個建構子,則預設的就沒有作用了 If you add a constructor, the default constructor will no longer be created,

public class Person

{

public Person(DateTime dateOfBirth)

{

DateOfBirth = dateOfBirth;

}//注意屬性宣告和設定並不在建構子內!因為最多只是把初始化放在建構子裡。而若將初始化的程式碼寫在建構子裡,則會造成建構子的內容太冗雜。所以就把初始化值的指令也與宣告一併,而屬性宣告並不放在建構子中,因為屬性是 class 的成員 member,並不是建構子的。

public string FirstName { get; set; } = string.Empty;

public string LastName { get; set; } = string.Empty;

public DateTime DateOfBirth { get; set; }//注意,屬性宣告卻在建構子後

public string TaxPayerId { get; set; } = string.Empty;

}

2:21:00 Composition 就是將分散而可合併的 member 組合、組建(如作文、寫成一篇文章)成一個新的 class 即物以類聚

Fields and properties provide a way for you to compose your objects from other objects.This gives you a powerful way to add and share properties and behavior between objects.

2:30:00 所以抽離出來創建一個新的類別(或物件類別)是有必要的

However, a better approach would be to pull out the properties that logically can be thought of as an address, and compose the other classes using this new type:

2:30:00 2:33:00 繼承的妙用 Another approach to share this state between two different classes would be to use inheritance,

2:35:00 雖然有組建、繼承二法可用,但仍建議是用組建式才好 :However, it's generally better to favor composition over inheritance in your C# applications, as it results in a more flexible design.

2:49:40 This is another reason to favor composition over inheritance, since there's no limit to how many other classes you can reference in your classes via fields or properties.

組建一個新的 class 就不受單一繼承的限制,可以參照無限的資源。

2:41:00 C# 的繼承關係就很像人類親族的關係 Just as you inherit from your grandparents,

2:43:00 Inheritance provides a form of reuse,  重複利用程式碼 避免資源浪費,提昇效能。 DRY! http://bit.ly/2eMnQQR http://bit.ly/2eJRaaM

2:45:00 我上網分享自掃資源,不也是 DRY 嗎?

2:59:00 You can actually add : System.Object to any class that doesn't have another base class without changing its behavior - this is implied within .NET. Classes that inherit from other classes expose the base class's methods to their collaborators.

3:01:00 virtual 是不能省略的。預設是父類別的不給子類別覆寫 By declaring that method as virtual, child classes can modify the behavior of the method; by default methods cannot be changed by child class implementations.

覆寫時 名稱、傳回型別、和方法簽章要與被覆寫的相同;When changing base class behavior, the child class defines the method with the same return type, name, and signature, as well as the override keyword.

3:06:00 覆寫 ToString()

3:07:00 抽象類別 Abstract types abstract classes 即只能被繼承而不能被實例化 instantiated

define a class that should never actually be instantiated as an object - it should only be used as a base class for others.

3:09:00 當實例化 instanti  一個 class 並沒有實質上的意義的時候,就可以考慮將其設定為抽象類別:it doesn't really make a lot of sense to create an instance of

3:10:30 抽象方法 當在基底類別不需要被實作時,就可考慮使用:You can also define methods as abstract, so that their implementation is required in child classes, but unnecessary in the base class.

抽象的方法是一定要被覆寫、實作的,否則不會被編譯:Note that abstract methods do not require the virtual keyword, since they must be overridden in child class implementations (the code will not compile otherwise).

3:14:10 Polymorphism 多型(Polymorphism)

Polymorphism means, "occurring in several different forms".

In programming, polymorphism refers to the ability to have code that can work with objects of different forms as if they were the same.

多型果然與繼承息息相關 Polymorphism and inheritance are related,

you can write methods that accept a base type as a parameter 把基底類別當作方法的參數來傳遞,而此參數,卻可用其衍生類別的實例(執行個體)來傳遞,就是多型的展現。所以多型就像擴充方法,主要是在方法上的呈現。用方法來呈現出來它的功能及特色。

4:53:10 4:40:00 多型為何更易於維護呢 想與 composition 類似 便於統合於其基底類別

This allows the calling code and the specific implementation of Shape to evolve independently from one another, resulting in a more maintainable program.

4:56:00 collaborators 在這裡應就指如 Rectangle Triangle 這兩種的 instances

public class Program

{

public static void Main()

{

var rectangle = new Rectangle();

rectangle.Height = 5;

rectangle.Width = 6;

DisplayShape(rectangle);

//rectangle.Perimeter();//假如不用多型的方法,就只能這樣一個一個 call 了,這樣的話 the calling code 不就更複雜,更繁多,less maintainable 了!



var triangle = new Triangle();

triangle.Side1 = 3;

triangle.Side2 = 4;

triangle.Side3 = 5;

DisplayShape(triangle);

//triangle.Perimeter();

}

public static void DisplayShape(Shape shape)

{

Console.WriteLine($"Shape Perimeter: {shape.Perimeter()}");//做多型方法,就如同 Composition,可以統合管理,易於維護了

}



}

public abstract class Shape

{

public abstract int Perimeter();

}



//public class Shape

//{

//    public virtual int Perimeter()

//    {

//        return 0;

//    }

//}

public class Rectangle : Shape

{

public int Height { get; set; }

public int Width { get; set; }



public override int Perimeter()

{

return (Height + Width) * 2;

}

}

public class Triangle : Shape

{

public int Side1 { get; set; }

public int Side2 { get; set; }

public int Side3 { get; set; }



public override int Perimeter()

{

return Side1 + Side2 + Side3;

}

}

5:00:00 所以多型仍與方法有關,而其表現主要即以可以寫一個特殊的方法,其 parameers 參數型別是基底類別,而其引數 arguments則可以是衍生類別的執行個體實例。

5:05:00 封裝的意思:encapsulate the logic for displaying the address information, so that your program minimizes repetition and moves as much of the business logic from the Main method to other classes.

都是為了讓程式更簡潔易讀、好維護!

6:38:30 Consider ways that you can encapsulate the logic for displaying the address information, so that your program minimizes repetition and moves as much of the business logic from the Main method to other classes. 考慮如何封裝顯示地址信息的處理程序,以便讓您的程式做到最少的重複,並將 Main 方法裡的處理程序盡可能地移動到其他類別裡。--移動到其他的類別,不就是抽離(組建)composition!

5:06:50 注意這裡提到的 logic  和 logic block 及 encapsulate 都是關鍵字!

如:any necessary business logic 、You should avoid putting complex logic into constructors、a better approach would be to pull out the properties that logically can be thought of as an address, and compose the other classes using this new type

pull out 就是抽離出來。

5:25:00 Composition 再溫習,更理解、體會了: 5:35:00

如下試作實例,可見建構了一個新的 class Address ,再利用此 class 在別的 class 宣告一個成員,(此例為屬性)就可以用宣告它的 class 來實例化一個物件,便可以用在新建構的 class 的成員了。很類似繼承和多型。只是多型( polymorphism)與繼承( inheritance)是有關聯的,而組建 composition 則否,故更彈性!

using System;

using static System.Console;

public class Program

{

public static void Main()

{

Person p = new Person("Oscar","Sun","DeXing","Taipei","Taiwan","11147","ROC");//若ShippingAddress未實例化,則會出現 NullReference的 exception,即使實例外了,但因 Address 本身並未實例化,故仍是 其下屬性仍是 null

WriteLine(p.ShippingAddress.StreetAddress);//是不是好像 p 這個 Person 類別的全都「繼承」了 Adress裡才有的屬性( Properties)呢

WriteLine(p.ShippingAddress.City);

WriteLine(p.ShippingAddress.State);

WriteLine(p.ShippingAddress.PostalCode);

WriteLine(p.ShippingAddress.Country);

}

}

public class Address

{

public string StreetAddress { get; set; }

public string City { get; set; }

public string State { get; set; }

public string PostalCode { get; set; }

public string Country { get; set; }

}

public class Person

{

public string FirstName { get; set; }

public string LastName { get; set; }//雖其亦為 reference type 但是 compiler 卻會以 System.Empty 初始化 string

public Address ShippingAddress { get; set; }

internal Person(string fn,string ln,String str,string city, string stat ,string pcode,string contry)

{

FirstName = fn;

LastName = ln;

ShippingAddress = new Address();//物件類別則須初始化(實例化),才不會是 null

ShippingAddress.City = city;

ShippingAddress.Country = contry;

ShippingAddress.PostalCode = pcode;

ShippingAddress.State = stat;

ShippingAddress.StreetAddress = str;

}

}

public class Company

{

public string Name { get; set; }

public Address ShippingAddress { get; set; }

}



5:11:00 Using methods to make updates to the state of an object is often preferred, since it allows you to add any necessary business logic surrounding what kinds of updates are allowed. Properties are a C# feature that allows you to define special get and set methods for any state you want your objects to expose to other objects (or even for their own use).

5:13:30 可見這裡 logic 翻成「內容」或「(處理 business)程序」會更達意。

5:14:50 九陽神功、十全大補丸、七星大法 裡頭的 上下文、前後文的重要

5:16:10 「只是」怎麼翻?「只」什麼意思?「是」又什麼義?

5:18:00 甘茂 孔融;傷仲永

6:14:00 溫故知新 多型 Polymorphism 也可參考此篇: http://bit.ly/2uDvRvt 總之,多型,即一個指令,得到不同結果。而能夠如此的原因,只因他們有共同的繼承。所以與單一繼承是有關係的。

6:28:00 複習一下 collections 。 arrays 有 ToList 方法可以將其轉成 List 用:One last very common extension method you can apply to arrays is ToList. This method does just what you would expect - it creates a new List type and initializes it with the current contents of the array.

7:07:00 {練習 Next Steps 成功: 8:09:30

using System;

using static System.Console;

using System.Collections.Generic;



public class Program

{

public static void Main()

{

addList();

printoutAddress.printout(pList,comList);

}

static List<Person> pList=new List<Person> ();

static List<Company> comList=new List<Company> ();

static void addList()

{

for (int i=0;i<3;i++)

{

Person p = new Person();

Company c = new Company();

WriteLine("person =p or com= c?");

if (ReadLine() == "p")

{

WriteLine("FirstName?");

p.FirstName = ReadLine();

WriteLine("LastName?");

p.LastName = ReadLine();

WriteLine("StreetAddress?");

p.ShippingAddress.StreetAddress = ReadLine();

WriteLine("City");

p.ShippingAddress.City = ReadLine();

WriteLine("State");

p.ShippingAddress.State = ReadLine();

WriteLine("PostalCode");

p.ShippingAddress.PostalCode = ReadLine();

WriteLine("Country");

p.ShippingAddress.Country = ReadLine();

pList.Add(p);

}

else

{

WriteLine("Name?");

c.Name = ReadLine();

WriteLine("StreetAddress");

c.ShippingAddress.StreetAddress = ReadLine();

WriteLine("City");

c.ShippingAddress.City = ReadLine();

WriteLine("State");

c.ShippingAddress.State = ReadLine();

WriteLine("PostalCode");

c.ShippingAddress.PostalCode = ReadLine();

WriteLine("Country");

c.ShippingAddress.Country = ReadLine();

comList.Add(c);

}

WriteLine();

}

}

}

class printoutAddress:Address

{

internal static void printout(List<Person> pList, List<Company> comList)

{

WriteLine(); WriteLine("-----------"); WriteLine();

ForegroundColor = ConsoleColor.Red;

foreach (Person p in pList)

{

WriteLine($"{p.FirstName} {p.LastName}\n{p.ShippingAddress.StreetAddress}\n" +

$"{p.ShippingAddress.City}, {p.ShippingAddress.State}, {p.ShippingAddress.PostalCode}\n" +

$"{p.ShippingAddress.Country}");

WriteLine();

}

foreach (Company c in comList )

{

WriteLine($"{c.Name}\n{c.ShippingAddress.StreetAddress}\n" +

$"{c.ShippingAddress.City}, {c.ShippingAddress.State}, {c.ShippingAddress.PostalCode}\n" +

$"{c.ShippingAddress.Country}");

WriteLine();

}

ForegroundColor = ConsoleColor.Gray;

}

}



public class Address

{

public string StreetAddress { get; set; }

public string City { get; set; }

public string State { get; set; }

public string PostalCode { get; set; }

public string Country { get; set; }

}

public class Person

{

public string FirstName { get; set; }

public string LastName { get; set; }

public Address ShippingAddress { get; set; }

internal Person()

{

ShippingAddress = new Address();

}

internal Person(string fn,string ln,String str,string city, string stat ,string pcode,string contry)

{

FirstName = fn;

LastName = ln;

ShippingAddress = new Address();

ShippingAddress.City = city;

ShippingAddress.Country = contry;

ShippingAddress.PostalCode = pcode;

ShippingAddress.State = stat;

ShippingAddress.StreetAddress = str;

}

}

public class Company

{

public string Name { get; set; }

public Address ShippingAddress { get; set; }

internal Company()

{

ShippingAddress = new Address();

}

}

}



}

8:10:40 Lesson 13 of 20 Working with Scope and Accessibility Modifiers http://bit.ly/2u0McIx

{

16:45:50 timeframe 有效範圍

16:47:00 notepad++ 快速鍵 Ctrl+K 將該行作為 c# 註解; visual studio 則是連按二次 Ctrl+k 也可以新增或刪除游標所在行書籤

16:49:30  three different levels of scoping in C#: method-level, block-level, and class-level.

16:51:50 Block Scoping :In C#, when you declare a variable inside of a block, it exists from the line where it is declared until the end of the block.

16:52:50 short-lived variables

block-level scoping

}

3:36:00 模式比對 http://bit.ly/2uAZoG0

3:55:00 3:40:00 下載 C# 6.0 draft specification-6language-specification

8:14:00 優化 歷代詞作韻律資料庫.mdb





}


留言

熱門文章