C tutorial code c
This presentation is the property of its rightful owner.
Sponsored Links
1 / 21

在職教師教育網路應用系統程式分享與實作研習 ---C# Tutorial(Code C) PowerPoint PPT Presentation


  • 46 Views
  • Uploaded on
  • Presentation posted in: General

在職教師教育網路應用系統程式分享與實作研習 ---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")]

Download Presentation

在職教師教育網路應用系統程式分享與實作研習 ---C# Tutorial(Code C)

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


C tutorial code c

在職教師教育網路應用系統程式分享與實作研習---C# Tutorial(Code C)

行政網路組 傅志雄


Object oriented programming with c

Object Oriented Programming with C#


Classes and objects

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

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 objects1

Classes and Objects

  • Access Modifiers

    public

    private

    protected

    Internal(assembly)

    Protected internal


Encapsulation and member accessibility

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

publicclass Customer

{

publicint Field1;

internalint Field2;

privateint Field3;

protectedint Field4;

protectedinternalint Field5;

}


Classes and objects 5

Classes and Objects-5

  • Creating Objects

    Time t = new Time( );


Classes and objects 4

Classes and Objects-4

  • Method Arguments

    void MyMethod (int firstParam, string secondParam)

    {

    // ...

    }


Constructors

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

publicclass Customer

{

privatestring m_Name;

privatestring m_Phone;

public Customer(string Name, string Phone)

{

m_Name = Name;

m_Phone = Phone;

}

}

Customer c1;

c1 = New Customer("Wendy", "432-4636");

Customer c2 =New Customer("Bob", "555-1212");


Default constructor

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

publicclass Class1{

//no explicit constructor

}

publicclass Class3{

publicClass3(string s){

//implementation

}

publicClass3(){

//implementation

}

}

publicclass Class2{

publicClass2(string s){

//implementation

}

}

Class1 c1 = new Class1();

Class2 c2 = new Class2();

Class3 c3 = new Class3();

Calls default constructor

Illegal: no default constructor

Calls default constructor


Classes and objects constructors

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

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

publicclass CustomerManager

{

publicstring 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

Encapsulation using Properties

  • Properties require new syntax

    • each property must implement a Get block and/or a Set block

publicclass Customer

{

//private field

privatestring m_Name;

//property controlled access to private field

publicstring Name

{

//perform calculations if necessary

get

{ return m_Name; }

//perform validation here if necessary

set

{m_Name = value;}

}

}

Get block

Set block

//client-side code

string s;

Customer obj = new Customer();

obj.Name = “fu"; // triggers Set block

s = obj.Name; // triggers Get block

//-------參考demo03.txt


Implementation inheritance

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”

Human

Manager "IS A" Human

Programmer "IS A" Human

Manager

Programmer

SeniorManager

ManagerTrainee


Inheriting from a base class

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

publicclass Human{

publicstring Name;

publicstring Speak() {

return "Hi I am a human named:" + Name;

}

}

publicclass Manager : Human

{

//Manager specific implementation here

}

publicclass Programmer : Human

{

//Programmer specific implementation here

}

//-------參考demo04.txt


Extending from a base class

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

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

publicclass Human

{

protectedstring m_Name;

public Human(string Name)

{

//implicit call to System.Object constructor

m_Name = Name;

}

}

publicclass Programmer : Human

{

//doesn't compile

//base class has no accessible default constructor!

}

編譯出問題


Constructor chaining

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

Calling base class constructor解決1

  • Base class constructor called via base keyword

    • can only be called once

publicclass Human

{public Human()

{

}

protectedstring m_Name;

public Human(string Name)

{

//implicit call to System.Object constructor

m_Name = Name;

}

}

publicclass Programmer : Human

{

}

增加 空default Constructor

//-------參考demo07.txt


Calling base class constructor 2

Calling base class constructor解決2

  • Base class constructor called via base keyword

    • can only be called once

publicclass Human

{

protectedstring m_Name;

public Human(string Name)

{

//implicit call to System.Object constructor

m_Name = Name;

}

}

publicclass Programmer : Human

{

public Programmer(string Name):base(Name)

{

//chaining a call to base class constructor

}

}

Special syntax for constructors

//-------參考demo08.txt


11 21

11/21 止

  • 待續………


  • Login