Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class & Object 蔡柏灃.

Similar presentations


Presentation on theme: "Class & Object 蔡柏灃."— Presentation transcript:

1 Class & Object 蔡柏灃

2 Object Oriented Programming
Basic concepts Everything is an object Blueprint and its realization Ex1: A building Ex2: A dog Encapsulation Inheritance Polymorphism

3 Encapsulation Information of objects should be encapsulated
Data and corresponding accessing methods Check and constrain accessing (access-modifiers) Safety and reusability

4 Class & Object Class Object 1 class  lots of objects !
An user-defined type Data (Properties) & Behaviors (Methods) The Main() Object An entity of specific characteristics/functions existing in the memory – Normally, you are using objects not classes ! The new keyword The dot ( . ) operator 1 class  lots of objects !

5 Access modifiers It is strongly recommended to put the modifiers EXPLICITLY !!

6 Examples Example 4_1 Example 4_1_modified (access modifiers)
We build a time class and provide a method to display the time message Example 4_1_modified (access modifiers) Not a good example (public on variables) Getter and setter Property (introduce later)

7 Property Property is used to access the private variables
Getter: only provide get You have to return Setter: only provide set Keyword value Example 4_1_modified_2

8 using System; namespace example4_1 { public class Time // private variables private int Year = 2008; private int Month = 1; private int Date = 12; private int Hour = 8; private int Minute = 0; private int Second = 0; // public methods public void DisplayCurrentTime( ) Console.WriteLine("Time --> {0}/{1}/{2} AM {3}:{4}:{5}", Year, Month, Date, Hour, Minute, Second); } // properties public int YEAR get { return Year; } set { Year = value; } public int MONTH get { return Month; } set { Month = value; } public class Tester static void Main( ) Time t = new Time( ); t.YEAR = 2007; t.MONTH = 11; t.DisplayCurrentTime( );

9 Exercise 10 A simple personal telephone book
Please build a class that records your friends’ names and mobile phone numbers Provide a display function that displays these messages

10 Constructors It is used to initialize an object when it is firstly constructed by CLR Every class has at least a constructor If you don’t provide one, an empty constructor is provided automatically and implicitly If you provide one, this default one will no longer exist Should always has the same name as the class Public No return (not even void) Example 10_modified Keyword this

11 Exercise 11 A simple calculator
Please try to write a simple calculator that provides some basic arithmetic operations for two integer inputs Max Min Mean Static methods

12 A system time displayer (example 4_3)
System.DatTime class Provided by C# Used to store time System.DateTime.Now is a property to get the current time Don’t you don’t have to new anything? Keyword static You can access the methods or properties by ClassName.xxx static to static

13 static members Once you put the static keyword on you class member, it will be kept in the memory You don’t have to new any object in order to use it There is only one static member for all of you objects No copies

14 Telephone book counter
How do you keep a counter that records the number of your friends recorded in the phone book ? Example 10_modified_2

15 Exercise 12 Cats and dogs adoption system
Please write a system that keeps the numbers of cats and dogs and provides functions that modifies these numbers when any cat or dog is adopted or newly coming catAdopted(int number) catNew(int number) Property CatNumber …


Download ppt "Class & Object 蔡柏灃."

Similar presentations


Ads by Google