Lecture 18 Making C# Objects

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Java Classes Using Java Classes Introduction to UML.
4.1 Instance Variables, Constructors, and Methods.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
CSE 114 Computer Science I Objects Lake Superior, Michigan.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 4 Introduction to Classes, Objects, Methods and strings
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Chapter 3 Implementing Classes
Topic: Classes and Objects
Creating and Using Objects, Exceptions, Strings
Classes C++ representation of an object
Concepts of Object Oriented Programming
Building Java Programs
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Static data members Constructors and Destructors
Classes and OOP.
Andy Wang Object Oriented Programming in C++ COP 3330
Review: Two Programming Paradigms
University of Central Florida COP 3330 Object Oriented Programming
Classes.
CompSci 230 Software Construction
Class Variables We’ve seen how to add extra subroutines into our programs Can be accessed from within one another We can also add variables that are “global”
Lecture 4-7 Classes and Objects
Java LESSON 7 Objects, Part 1
CS1S467 GUI Programming LECTURE 15 Methods (2 of 2)
CS212: Object Oriented Analysis and Design
Lecture 11 C Parameters Richard Gesick.
CS1S467 GUI Programming Lecture 3 Variables 2.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 10 Thinking in Objects
Interface.
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
Teach A-Level Computer Science: Object-Oriented Programming in Python
Encapsulation and Constructors
Chapter 9 Objects and Classes
Object Oriented Programming
Dr. Bhargavi Dept of CS CHRIST
Defining Classes and Methods
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Tonga Institute of Higher Education
Object Oriented Programming in java
Java Programming Language
Introduction to Object-Oriented Programming
Review of Previous Lesson
Classes C++ representation of an object
Dr. R Z Khan Handout-3 Classes
Methods/Functions.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Previous Lecture: Today’s Lecture: Reading (JV):
Object-Oriented Programming and class Design
Presentation transcript:

Lecture 18 Making C# Objects CS1S467 GUI Programming Lecture 18 Making C# Objects

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

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

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

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.

Similarities Real Objects - Program Objects “Blueprint” or Plan = C# class Designed Once only Made / Built Repeatedly Manufacturing Line = C# object instances

Old type of Photocopy. And yes, they were really blue. Blueprint Old type of Photocopy. And yes, they were really blue.

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.

Enough Theory Lets do it !

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;

“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

“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.)

“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

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

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

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'

“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

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;

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

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(); }

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

 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

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;

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'

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( );

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;

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

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