Download presentation
Presentation is loading. Please wait.
1
Lecture 18 Making C# Objects
CS1S467 GUI Programming Lecture 18 Making C# Objects
2
Today Object Fundamentals Making & using a Car Object Demo
Why Objects? Making & using a Car Object The ‘Car’ class The ‘Driver’ class Demo Making Space Invader Objects
3
Why Objects ? History of Programming (abridged): In the beginning…….
BASIC: the "Goto crisis" PROCEDURES (’Methods’ in C#) A little later…... Pascal, C, et al.: the "Software crisis“ OBJECTS
4
An Example: Re-Usability Consider a car:-
Single car manufacturer may use a component across a range of models e.g. locks and ventilation controls. Different manufacturers may use the same component supplied by a single manufacturer e.g. tyres and wiper-blades. Re-Usability
5
Similarities Real Objects - Program Objects
However, the components must still be : Designed Once only Made / Built Repeatedly So too must software components and the programs which manipulate them.
6
Similarities Real Objects - Program Objects
“Blueprint” or Plan = C# class Designed Once only Made / Built Repeatedly Manufacturing Line = C# object instances
7
Old type of Photocopy. And yes, they were really blue.
Blueprint Old type of Photocopy. And yes, they were really blue.
8
Object Orientated Basics
The state of an object is represented by private attributes (i.e. content of variables) of the object. The state of an object (i.e. the content of variables) can be changed or interrogated only via public methods.
9
Enough Theory Lets do it !
10
Object Orientated Example: “Car” Fundamentals (1 of 3)
Consider Cars: We start with a single underlying “blueprint“ for the Cars we want to use Lots of Cars will then be instances (i.e. copies) of the same blueprint;
11
“Car” Fundamentals (2 of 3)
Each Car instance (copy) can access methods such as : Setting the attribute changeSpeedBy(…) getSpeed( ) Interrogating the attribute In this example Car attributes (e.g. int speed, int direction ) store the state of the cars. private attributes public methods
12
“Car” Fundamentals (3 of 3)
To summarise: We start with a single “blueprint” for Cars From that we can later build multiple instances (i.e. copies) of Cars Each Car instance has methods (i.e. can do things) The methods act on the attributes of each Car (i.e. speed, direction, etc.)
13
“Car” Object Implementation
Design once Car “Blueprint” - methods - attributes The ‘Car’ class Car.cs Multiple builds Car 3 - methods - attributes Car 2 - methods - attributes Car 1 - methods - attributes The ‘Driver’ class Driver.cs Car instances
14
General Structure of “Blueprints”
Attributes (variables, constants) Class attributes Attributes which are identical for all objects created from the blueprint. e.g. "FORWARDS" for Cars - same for all objects. ‘static’ keyword (or ‘const’ which is also static) class NameOfClass { // Class attribute(s) go here // Object attribute(s) go here // Constructor(s) go here // Other methods go here // Optional ‘Main’ method here } // end of the ‘blueprint’ class Objects attributes Variables which are copied into individual objects created from the blueprint. e.g. “state” (speed), - different for each object. NO 'static' keyword
15
General Structure of “Blueprints”
Methods (manipulate attributes) Constructor(s) Special method that creates a new object from the blueprint. Has same name as blueprint. class NameOfClass { // Class attribute(s) go here // Object attribute(s) go here // Constructor(s) go here // Other methods go here // Optional ‘Main’ method here } // end of the ‘blueprint’ class Other methods Usually so called ‘get’ and ‘set’ methods that manipulate the attributes. Possibly other ‘helper’ methods ’Main’ method Not required, but may be used for testing and demonstration of usage
16
Car.cs “Blueprint” Class attributes Objects attributes Constructor(s)
class Car { // Class attribute(s) go here // Object attribute(s) go here // Constructor(s) // 'Set' and 'Get' methods Car.cs “Blueprint” public const int FORWARDS=1; public const int BACKWARDS=0; private int speed=0; private int direction=FORWARDS; Class attributes Attributes which are identical for all objects created from the blueprint. e.g. "FORWARDS" for Cars - same for all objects. usually public ‘static’ keyword (or ‘const’ which is also static) public Car () { speed=0; direction=FORWARDS; } Objects attributes Variables which are copied into individual objects created from the blueprint. e.g. “state” (speed), - different for each ‘private’ keyword no 'static' Constructor(s) Special method that creates a new object from the blueprint. Has same name as the blueprint class (i.e. 'Car'). ‘public’ keyword no 'static' Other methods Usually so called ‘get’ and ‘set’ methods that manipulate the attributes. Other ‘helper’ methods ‘public’ keyword no 'static' public void changeSpeedBy(int amount) { speed = speed + amount; if ( speed > 70 ) speed = 70; } if ( speed < 0 ) speed = 0; } // end of method 'changeSpeedBy' ’Main’ method Not required, but may be used for testing and demonstration of usage public int getSpeed() { return speed; } // end of method 'getSpeed'
17
“Driver” Implementation
Design once Car “Blueprint” - methods - attributes The ‘Car’ class Car.cs Multiple builds Car 3 - methods - attributes Car 2 - methods - attributes Car 1 - methods - attributes The ‘Driver’ class Driver.cs
18
Building Cars (Constructing) (Driver.cs)
class Driver { static void Main(string[] args) Car beatle = new Car(); Car volvo = new Car(); Console.WriteLine("Initial Speed:"); Console.WriteLine("Beatle: " +beatle.getSpeed() ); Console.WriteLine("Volvo : " +volvo.getSpeed() ); Console.WriteLine("Flooring pedal of the Beatle:"); beatle.changeSpeedBy(500); Console.WriteLine("Beatle: " + beatle.getSpeed()); Console.WriteLine("Volvo : " + volvo.getSpeed()); Console.ReadLine(); } Call the constructor Class of object to construct Keyword "new" Name of the object to construct public Car () { speed=0; direction=FORWARDS; } Compare 'constructing' to int x = 5;
19
Building Cars (Constructing) (Driver.cs)
public int getSpeed() { return speed; } // end of method 'getSpeed' class Driver { static void Main(string[] args) Car beatle = new Car(); Car volvo = new Car(); Console.WriteLine("Initial Speed:"); Console.WriteLine("Beatle: " +beatle.getSpeed() ); Console.WriteLine("Volvo : " +volvo.getSpeed() ); Console.WriteLine("Flooring pedal of the Beatle:"); beatle.changeSpeedBy(500); Console.WriteLine("Beatle: " + beatle.getSpeed()); Console.WriteLine("Volvo : " + volvo.getSpeed()); Console.ReadLine(); } Output: Beatle : 0 Volvo : 0 Name of object dot Name of method
20
Building Cars (Constructing) (Driver.cs)
public void changeSpeedBy(int amnt) { speed = speed + amnt; if ( speed > 70 ) speed = 70; } if ( speed < 0 ) speed = 0; } // end of method 'changeSpeedBy' class Driver { static void Main(string[] args) Car beatle = new Car(); Car volvo = new Car(); Console.WriteLine("Initial Speed:"); Console.WriteLine("Beatle: " +beatle.getSpeed() ); Console.WriteLine("Volvo : " +volvo.getSpeed() ); Console.WriteLine("Flooring pedal of the Beatle:"); beatle.changeSpeedBy(500); Console.WriteLine("Beatle: " + beatle.getSpeed()); Console.WriteLine("Volvo : " + volvo.getSpeed()); Console.ReadLine(); }
21
Building Cars (Constructing) (Driver.cs)
public int getSpeed() { return speed; } // end of method 'getSpeed' class Driver { static void Main(string[] args) Car beatle = new Car(); Car volvo = new Car(); Console.WriteLine("Initial Speed:"); Console.WriteLine("Beatle: " +beatle.getSpeed() ); Console.WriteLine("Volvo : " +volvo.getSpeed() ); Console.WriteLine("Flooring pedal of the Beatle:"); beatle.changeSpeedBy(500); Console.WriteLine("Beatle: " + beatle.getSpeed()); Console.WriteLine("Volvo : " + volvo.getSpeed()); Console.ReadLine(); } Output: Beatle : 70 Volvo : 0
22
Done ! Design once Car “Blueprint” - methods The ‘Car’ class
- attributes The ‘Car’ class Car.cs Multiple builds Car 3 - methods - attributes Car 2 - methods - attributes Car 1 - methods - attributes The ‘Driver’ class Driver.cs
23
Review: The keyword 'static'
'static' methods and variables (usually constants, which are always static in C#) exist only once for all copies made from the 'blueprint'. These methods or variables are then called 'class' methods or 'class' variables public const int FORWARDS=1; public const int BACKWARDS=0; If methods and class variables are declared WITHOUT 'static' or ‘const’ a separate copy is made for each instance produced from the 'blueprint' These methods or variables are then called 'object' methods or 'object' variables private int speed=0; private int direction=FORWARDS;
24
Review: 'private' Object Variables
Object variables (i.e. no 'static'), declared 'private' can NOT be accessed directly from other classes (e.g. UseCar.java) The reason for this 'encapsulation' is 'safety‘. Access to these variables is ONLY indirectly via ‘public‘ methods. private int speed=0; private int direction=FORWARDS; public void changeSpeedBy(int amnt) { speed = speed + amnt; if ( speed > 70 ) { speed = 70; } if ( speed < 0 ) speed = 0; } // end of method 'changeSpeedBy'
25
Review: Constructors Can be thought of as factories turning blueprints (the 'Car' class) into 'real' (well…) objects. Once "constructed" an object 'exists'. (i.e. has its own space inside the memory) It has: a name (car1, car2, myCar, gaiusWreck) attributes (speed, direction) a state (values stored in 'speed', 'direction') methods (changeSpeedBy(…), getSpeed() ) Car car1 = new Car( );
26
Review: Everything Together
Two classes required: creates object ‘blueprint’ creates object ‘instances’ Object Definition Class Usage Class Class attributes (same for all objects) public static (or: public const) Object attributes (different content for each object) private Constructor public (same name as class) Set and Get Methods public methods acting on private attributes Main method optional, just for ‘how-to’ and testing Main method to run the program Call of constructor e.g. Car myCar = new Car(); Indirect access to private object attributes via public methods e.g. myCar.setSpeed (500); Direct access to class attributes e.g. x = Car.FORWARDS;
27
Today Object Fundamentals Making & using a Car Object Demo
Why Objects? Making & using a Car Object The ‘Car’ class The ‘Driver’ class Demo Making Space Invader Objects
28
And in the Next 2 Weeks We put it into Practive: Demos
Creating an Asteroid Class Making & using Asteroid Objects An then the same again with SpaceInvaders
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.