module 201 object oriented programming lecture 6...

20
Module 201 Object Oriented Programming Lecture 6 – Introduction to Classes

Upload: others

Post on 26-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

Module 201 Object Oriented Programming

Lecture 6 – Introduction to Classes

Page 2: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

Loops For loops For Each loop Break statement Goto Continue

Page 3: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

Introduction to classes

Page 4: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

Up until now we have used variables which contain a simple value

Number Date String (a bit different, but still a value)

What we need is some way to refer to OBJECTS Things that do something That represent a real thing

Like a customer A phone call A file or a directory

That means creating and consuming CLASSES

Page 5: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

Class acts as a template for an object Cookie cutter Blueprint

System.IO.FileInfo class provides code and logic to handle working with files

Must create an instance of the class to work with a file

Page 6: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

You can use one blueprint to make any number of houses, and you can use one class to make any number of objects

When you define a class, you define its methods, just like a blueprint defines the layout of the house

Page 7: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

A Class describes data It is like a blueprint of an object Defines abstract characteristics of a object

Things it can do – Methods Attributes - Properties and fields

Page 8: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

In order to create that instance you need to call the constructor for that class Instance of FileInfo class refers to a specific file

This instance called an object You need some way in which to communicate with that object

Retrieve information about… Manipulate properties of… Operate on…

A particular file

To do this you need to create an instance of a class and have a variable to refer to it

Page 9: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

When we created our new instance of a class we ran code like this: System.IO.FileInfo fi = new System.IO.FileInfo(“C:\boot.ini)

How do we read this? Read this code as:

Create a new instance of the FileInfo class (this is what the new keyword does) Referring to the C:\boot.ini file (this is the parameter we pass to the

constructor when we create the FileInfo class instance) Allow code to refer to the instance using a variable named fi

Page 10: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

The new keyword creates a new instance of the class you specify Calls code within the .NET Runtime that creates a new instance of the type and hands you back a reference

In C/C++ that reference is called a ‘pointer’

Code inside the class that runs as the class is created is called the constructor Class can (and often does) contain multiple constructors

Overloading makes this possible: All must have different sets of parameters So that at compile time the compiler can tell which one of those

procedures you need to call based on the parameters you supplied to the procedure

Page 11: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

Classes can expose members as either static or instance

Static members work with all instances of the class Instances work only with a specific instance

The designer of the class determines which members require a class instance (most do) and which don’t (static)

Page 12: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

You need to look carefully at class definitions to determine which members are instance members and which ones aren’t

FileInfo is an instance member: requires a specific file Directory.CreateDirectory does not require an instance

Just a service provided by the class

When you create your classes you choose which technique to use Why FileInfo, but not DirectoryInfo?

FileInfo and DirectoryInfo refer to a specific file/directory File and Directory work with all files or directories

Provide services for working with files

Page 13: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

To date you have used classes created by other people Just like the .NET Framework

Your application should probably consist of a number of classes Each of these provides a bit of your application’s functionality You might be tempted to write applications that consist of lots

of procedures called from Main Resist that urge

You want to design your application so that you are working with objects, methods and properties

Page 14: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

Objects generally represent 'things' or events that occur

Font TextBox Customer Order

"Things" generally have: Properties that describe the thing Methods: actions the thing can take Events: indicate when thing has changed its state

Page 15: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

Field Seen as a property of the class

Variable at the class level

Page 16: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

Where did these come from?? Every class in the .NET Runtime inherits from the base object class System.Object. System.Object supplies these four methods.

Lesson6.docx

Page 17: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

Class Name

Methods

Fields (or properties)

UML representation VS representation

Page 18: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

A class is shown as a rectangular box with three compartments Class diagrams are static – they display what interacts but not what happens when they do interact. Name is shown in bold and should be a noun Fields describe the class. They should have meaningful names and define the type Methods are actions used to manipulate the attributes or perform a specific task

Verbs in the specification

Page 19: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

Introduction to Classes

Page 20: Module 201 Object Oriented Programming Lecture 6 ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec06.pdf · Object Oriented Programming ... String (a bit different, but still a value)

Working with classes