25
2014-2015

FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

2014-2015

Page 2: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

Lecture 4

C# Language Fundamentals

OOP Concepts

Phil Smith

Page 3: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

Learning Outcomes LO1 LO1 Understand the principles of object oriented programming

1.1 discuss the principles, characteristics and features of

objected oriented programming.

Page 4: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

Identifying objects In a particular situation, often known as a problem

domain, a common starting point for identifying objects is to start looking at the nouns.

A noun is a word that names a person, thing, action, quality or state.

This immediately implies that not all nouns are going to be objects.

Page 5: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

Identifying objects Criteria for Identifying Nouns

An object must have some attributes that together describe the object state and these need to be stored so the system model can function.

Page 6: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

Identifying objects Criteria for Identifying Nouns – cont

An object must have a set of identifiable operations, methods or requests that can change these state attributes in some way –

Most objects have more than one attribute. Objects with only one attribute are often so simple that they are best modelled as an attribute of another object.

Page 7: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

Identifying objects Criteria for Identifying Nouns - cont

All of the attributes and operations for a particular object must apply to all occurrences of that object.

Page 8: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

Identifying objects What are the nouns in this requirement.

A local squash club needs a computer system to allocate the use of its courts and keep track of members.

The system will:

Display all courts and whether they are operational.

Display courts that are available.

Display who is currently playing.

Enter data for players waiting for a court.

Enter pre booking data.

Inform operator when time is up for games on a court in use.

Page 9: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

Identifying objects Having identified nouns we next need to look at the

verbs (actions).

Using the same process for nouns read the requirement to identify the verbs as these will be candidates for methods of the object.

Page 10: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

Identifying objects What are the verbs in this requirement.

A local squash club needs a computer system to allocate the use of its courts and keep track of members.

The system will:

Display all courts and whether they are operational.

Display courts that are available.

Display who is currently playing.

Enter data for players waiting for a court.

Enter pre booking data.

Inform operator when time is up for games on a court in use.

Page 11: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

Identifying objects/classes Task 1

Do lab04a.

Page 12: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

More on classes Humans are good at grouping objects together

in various ways.

Groups of objects that are similar make up a class in object-oriented systems.

Within a large object (complex) many possible subdivisions could be made to create a class hierarchy.

By using a process of subdividing we can limit the complexity of the system.

The process of abstraction allows us to focus on the essential characteristics of each class relative to the system.

Page 13: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

More on classes At each level of the hierarchy, the class may be used to

create an object of that class.

The class hierarchy is a tree structure similar to a family tree with the root at the top known as the superclass.

Each class below is a subclass of the superclass and inherits the attributes of its ancestors.

Page 14: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

More on classes Name the super classes and sub-classes.

Page 15: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

UML representation of a Class Lets take a simple switch…

It is an object.

It has different states.

It has methods of doing things .

This is a design

Page 16: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

OOP Principles We now know that all objects have characteristics.

When designing classes it is important to know what the characteristics are and how you could use them.

We have previously created a class but we are going to create a new class as a separate file in visual studio.

We also need to learn about principles and features of classes using c#.

Page 17: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

OOP Principles Constructors and destructors. Overloading and signatures. Accessors and mutators. Loose and tight coupling. Inheritance (and abstract classes). Encapsulation (already done). Aggregation. Composite classes. Association. Public and private classes. Public and private methods. Message passing. Polymorphism.

Page 18: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

OOP Principles by example Constructors and destructors. Overloading and

signatures.

Every class has to have at least one constructor.

You always get one for free if you do not declare any.

Task

Follow my lead on constructors/destructors.

Start by creating a new C# form project in visual studio.

We shall create classes for the Library books.

Page 19: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

OOP Principles by example What about destructors.

Usually you do not code these as .net manages memory for us.

But if you have a real reason for doing so then you need to implement an interface named “Idisposable”

Review this article https://msdn.microsoft.com/en-us/library/system.idisposable.aspx

We shall look at interfaces later.

Page 20: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

OOP Principles by example Now Accessors , mutators and coupling.

We already know that fields can be hidden in our classes, this is part of encapsulation.

We also know that classes can have fields and properties.

Properties look like fields outside the class but they act like methods inside the class.

Task

Follow my lead on Accessors /mutators and loose/tight coupling.

Page 21: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

OOP Principles by example Now Inheritance (and abstract classes).

Again we know that one advantage of OOP is code reuse.

There are different ways to interpret code reuse.

Task

Follow my lead on inheritance and abstract classes.

We shall add 2 new classes

Page 22: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

OOP Principles by example Now Aggregation, Composite classes and Association. We have already seen weak and strong association with

properties and get/set etc

Now we need to consider object ownership and relationships Composition is an Association Aggregation is an Association

Composition is a strong Association (If the life of contained

object totally depends on the container object, it is called strong association)

Aggregation is a weak Association (If the life of contained object doesn't depends on the container object, it is called weak association)

Page 23: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

OOP Principles by example

Task

Follow my lead on Aggregation, Composite classes and Association.

Page 24: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

Summary We have learnt about –

Designing objects with classes.

We have learnt how to implement some OOP principles and features using C#.

Page 25: FDSc - Herefordshire and Ludlow Collegewiki.hct.ac.uk/_media/computing/hnd/hndu19_lecture_04.pdf · OOP Principles Constructors and destructors. Overloading and signatures. Accessors

Next time More on classes.

Design for OOP.

Assignment 1.