170 likes | 315 Vues
CTEC2902 Advanced Programming in Visual Studio. CTEC2902 Advanced Programming. The story so far… You have Had the introductory session and lab Started revising / remembering ASP + VB fundamentals. Let’s carry on. Introducing Objects What is an “object”?. Oxford English Dictionary says
E N D
CTEC2902Advanced Programming in Visual Studio Introducing Objects
CTEC2902Advanced Programming The story so far… You have Had the introductory session and lab Started revising / remembering ASP + VB fundamentals Let’s carry on Introducing Objects
Introducing Objects What is an “object”? Oxford English Dictionary says Any thing you can see or touch Examples? Any problem with this definition? Introducing Objects
Introducing Objects What is an “object”? UML says Any thing you can define and interact with in your program Examples? How to recognize objects? Introducing Objects
Introducing Objects How to Recognize Objects • Analyse problem specification/definition • Pick out the nouns (they are potential objects) • Not all nouns in spec become objects; some may become properties of other objects • You may need objects not mentioned in spec How to define objects? Introducing Objects
Introducing Objects Defining Objects Step 1 – Identify an object An object must have a unique name Step 2 – How would you describe your object? An object may have zero or more defining characteristics (or properties) Step 3 – What can your object do? An object may have zero or more abilities or capabilities (or methods) Introducing Objects
Introducing Commonality Commonality of Characteristics & Operations My Car Mum’s Car Neighbour's Car • Properties • Year • Engine Size • Colour • etc. • Methods • Move (Direction, Speed) • Turn (Direction, Degree) • Blow horn • etc. Introducing Objects
Introducing Commonality Commonality of Characteristics & Operations My Sister My Neighbour My Uncle • Properties • DoB • Hair Colour • Height • etc. • Methods • Smile • Say (Something) • Wave (Hand) • etc. Introducing Objects
Introducing Commonality Commonality of Characteristics & Operations Similar objects share common characteristics and perform common operations => class Introducing Objects
Introducing Classes What is a class? • Generic definition of similar objects, specifying their • Shared characteristics (or properties) • Common operations (or methods) A template, from which any number of individual (unique) objects may be created Introducing Objects
Introducing Classes Class-Object Relationship A class is where you define … the properties and capabilities that objects can have An object is what you create and use in your solution, as a specific instance of a class e.g. Class – Car Object – My Mum’s car Object – Neighbour’s car • e.g. Class - Computer • Object - My computer at home • Object – The computer in the lab • Object – My laptop Introducing Objects
Introducing Classes E.g., a class & its instance, to represent this university • Class – University • Instance – DMU • Instance – Leicester University E.g., a VB control (class) & two typical instances of it • Class – List box • Object – lstSurnames • Object - lstBooks How do we specify/design classes? Introducing Objects
Introducing Classes E.g., Specifying a class Unique name, indicating purpose & functionality clsCar e.g. Step 1 Registration Make Model Colour Properties (like variables) Data types & validation Read-Write or Read-Only e.g. Step 2 Start Engine Stop Engine Blow Horn Turn Right (Degree) Put In Gear (Number) Methods As Subs or Functions (with/without parameters) e.g. Step 3 Introducing Objects
Each instance has • same properties but with (some or all) different property values • same methods Creating Objects Creating a Car Object - An Instance of Class “Car” Object created with a unique name Specific values assigned to properties A class may have many instances; MumsCar, DadsCar, PoliceCar, etc. MyCar Registration – FN 02 XTC Make - Volkswagen Model - Polo Colour - Silver • Each instance must be • individually created • uniquely named Start Engine Stop Engine Blow Horn Turn Right (12) Put In Gear (3) Introducing Objects
Introducing Classes • Exercise: • Define a class to represent people (or persons) • Class name is “Person” Class Name Person • Properties Name • Surname • Date of Birth • Height • Weight • Hair Colour • Can Swim • etc. • Methods Walk (Speed) • Run (Speed) • Sit • Stand • Say (What) • etc. Introducing Objects
Introducing Classes • Exercise: • Define a class to represent rooms • Class name is “Room” Class Name Room • Properties ID • Seating Capacity • Occupancy • OHP • Internet access • Layout • Air conditioner • etc. Methods SetAirCon(State) Enter(Who) Leave(Who) Other methods? Introducing Objects
Introducing Classes Keep up the good work Introducing Objects