在職教師教育網路應用系統程式 分享與實作研習 ---C# Tutorial(Code C) 行政網路組 傅志雄
Object Oriented Programming with C#
Classes and Objects 抽象化 (Abstraction) 關念 Defining Classes [attributes] [access-modifiers] class identifier [:base-class] {class-body} [assembly:AssemblyKeyFile(".\\keyFile.snk")] public class Tester { public static Main( ) { //... }
Classes and Objects-2 Simple Time class using System; public class Time { // public methods public void DisplayCurrentTime( ) { Console.WriteLine(“ 列印 ……"); } // private variables int Year; int Month; int Date; } public class Tester { static void Main( ) { Time t = new Time( ); t.DisplayCurrentTime( ); }
Classes and Objects Access Modifiers public private protected Internal( assembly ) Protected internal
public class Customer { public int Field1; internal int Field2; private int Field3; protected int Field4; protected internal int Field5; } Encapsulation and Member Accessibility Encapsulation-- 物件導向概念中,「封裝」可說是物件狀態的隱藏過程,或指程式實 作的隱藏 (implementation hiding) There are five levels of member accessibility –public: accessible to everyone –internal: accessible from within current assembly –private: accessible from this class only –protected: accessible from this class and from child classes –protected internal: union of protected and internal accessibility
Classes and Objects-5 Creating Objects Time t = new Time( );
Classes and Objects-4 Method Arguments void MyMethod (int firstParam, string secondParam) { //... }
Constructors Constructors are special methods designed to initialize fields –CLR executes constructor whenever New is called on a class –creatable classes must have at least one constructor –constructors defined using procedures with same name as class name –constructors can be parameterized and overloaded –parameters passed by client after class name public class Customer { private string m_Name; private string m_Phone; public Customer(string Name, string Phone) { m_Name = Name; m_Phone = Phone; } Customer c1; c1 = New Customer("Wendy", " "); Customer c2 = New Customer("Bob", " ");
Default constructor Non-parameterized constructor called "default constructor" –allows client to create object without passing parameters –C# compiler automatically adds a default constructor to classes that have no explicit constructor –default constructor (if desired) must be explicitly added to classes that contain other constructors public class Class3{ public Class3(string s){ //implementation } public Class3(){ //implementation } Class1 c1 = new Class1(); Class2 c2 = new Class2(); Class3 c3 = new Class3(); public class Class2{ public Class2(string s){ //implementation } public class Class1{ //no explicit constructor } Calls default constructor Illegal: no default constructor Calls default constructor
Classes and Objects [Constructors] public class Time { public void DisplayCurrentTime( ) { System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",Month, Date, Year, Hour, Minute, Second); } public Time(System.DateTime dt) { Year = dt.Year; Month = dt.Month; Date = dt.Day; Hour = dt.Hour; Minute = dt.Minute; Second = dt.Second; } int Year; int Month; int Date; int Hour; int Minute; int Second; } public class Tester { static void Main( ) { System.DateTime currentTime = System.DateTime.Now; Time t = new Time(currentTime); t.DisplayCurrentTime( ); } // 參考 demo01.txt
Overloading methods and properties Two or more class members can be given the same name –you can overload a set of methods or a set of properties Overloaded members must differ with their parameter lists –parameter lists must differ in size and/or in sequence of types –you cannot overload based on return type or parameter name public class CustomerManager { public string GetCustomerInfo(int ID) { //implementation } public string GetCustomerInfo(string Name) { //implementation } //client-side code string info1, info2; CustomerManager mgr = new CustomerManager(); //call GetCustomerInfo(Integer) info1 = mgr.GetCustomerInfo(23); //call GetCustomerInfo(String) info2 = mgr.GetCustomerInfo(“fu"); // 參考 demo02.txt
Encapsulation using Properties Properties require new syntax –each property must implement a Get block and/or a Set block public class Customer { //private field private string m_Name; //property controlled access to private field public string Name { //perform calculations if necessary get { return m_Name; } //perform validation here if necessary set {m_Name = value;} } //client-side code string s; Customer obj = new Customer(); obj.Name = “fu"; // triggers Set block s = obj.Name; // triggers Get block Set block Get block // 參考 demo03.txt
Implementation inheritance An OOP technique to reuse code across classes –derived class inherits implementation from base class –inheritance relationships create inheritance hierarchies Inheritance establishes "IS A" relationship between classes 「是一個」 VS. “Has A” Manager "IS A" Human Programmer "IS A" Human ManagerTraineeSeniorManager ProgrammerManager Human
Inheriting from a base class Class can explicitly inherit from a base class –class defines base class using Inherits keyword –class can only have one base class (no multiple inheritance) Class without explicit base class inherits from System.Object public class Human{ public string Name; public string Speak() { return "Hi I am a human named:" + Name; } public class Manager : Human { //Manager specific implementation here } public class Programmer : Human { //Programmer specific implementation here } // 參考 demo04.txt
Extending from a base class public class Human { public string Name; public string Speak() { return "Hi I am a human named:" + Name; } public class Manager : Human { //Manager specific implementation here public string Name2; public string Speak2() { return "My Dad is"+Name+"---Hi I am a human named:" + Name2; } // 參考 demo05.txt
Base classes and constructors Constructors and base types have "issues" –derived class contract doesn't include base class constructors –derived class must provide its own constructor(s) –derived class constructor must call a base class constructor –compiler can automatically generate call to accessible default constructor in base class constructor if one exists public class Human { protected string m_Name; public Human(string Name) { //implicit call to System.Object constructor m_Name = Name; } public class Programmer : Human { //doesn't compile //base class has no accessible default constructor! } 編譯出問題
Constructor Chaining public class a { public a() { System.Console.WriteLine(" 產生 a Class"); } public class b : a { public b() { System.Console.WriteLine(" 產生 b Class"); } public class c : b { public c() { System.Console.WriteLine(" 產生 c Class"); } public class d : c { public d() { System.Console.WriteLine(" 產生 d Class"); } // 參考 demo06.txt
Calling base class constructor 解決 1 –Base class constructor called via base keyword can only be called once public class Human { public Human() { } protected string m_Name; public Human(string Name) { //implicit call to System.Object constructor m_Name = Name; } public class Programmer : Human { } 增加 空 default Constructor // 參考 demo07.txt
Calling base class constructor 解決 2 –Base class constructor called via base keyword can only be called once public class Human { protected string m_Name; public Human(string Name) { //implicit call to System.Object constructor m_Name = Name; } public class Programmer : Human { public Programmer(string Name):base(Name) { //chaining a call to base class constructor } Special syntax for constructors // 參考 demo08.txt
待續 ……… 11/21 止