Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN 2013-01-271AK IT:

Similar presentations


Presentation on theme: "Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN 2013-01-271AK IT:"— Presentation transcript:

1 Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN 2013-01-271AK IT: Softwarekonstruktion

2 FEN 2013-01-27AK IT: Softwarekonstruktion2 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism In later sessions

3 FEN 2013-01-27AK IT: Softwarekonstruktion3 OO-Principles -encapsulation Seen from the outside an object is an entity offering a number of services (public methods and properties). The implementation and data representation are hidden or encapsulated. – This makes it possible to change implementation without affecting other parts of the program. – Also it becomes possible to think and talk about and use the object without knowing the implementation. – Hiding data behind methods and properties are called encapsulation or data abstraction. Encapsulation or data abstraction is one the fundamental principles of object-oriented programming.

4 FEN 2013-01-27AK IT: Softwarekonstruktion4 Definition of Object and Class Object – Represents a real-world concept, realised by data (attributtes) associated with the concept and a number of operations (methods)that may be used to access the attributes of the object. Class – A type that defines the attributes and methods of a set of objects that all represent instances of the same real-world concept. The class describes the structure of the concept, and the objects are the actual instances of the class. The class is static – exists only compile time. Objects are dynamic – exist runtime.

5 FEN 2013-01-27AK IT: Softwarekonstruktion5 Attributes (data) Attributes define the data to be stored (name and type). Attributes are defined in the class, and are assign values in the objects. E.g.: – BankAccount: accountNo, balance, maxLimit, interestRate etc. – Employee: name, departmentNo, salery, jobTitle etc. The state of an object is given by the value of its attributes at a given time.

6 FEN 2013-01-27AK IT: Softwarekonstruktion6 Methods (operations) The operations of an object are defined by the methods implemented in the class. Calls to methods either return information about the state of the object (accessors) or change the state of the object (mutators). BankAccount – WithDraw(), Deposite(), GetBalance() etc. Employee – GiveASaleryRaise (), SetTitle() etc.

7 FEN 2013-01-27AK IT: Softwarekonstruktion7 Properties (C# speciality) Are used for getting and setting attribute values. (Replace set- and get-methods in Java and C++). Provide a syntax similar to direct access of the attributes. (Anders Hejsberg footprint?)

8 FEN 2013-01-27AK IT: Softwarekonstruktion8 Constructor Method(s) with the same name as the class and no return type.The job of a constructor is to initialise the attributes of the object during object creation. E.g. Creating an object – BankAccount acc = new BankAccount(); BankAccount() is a call to the constructor. The new command – Allocates memory for the object. – Assigns the variable (the reference) acc to the allocated block of memory – new is actually a function that returns an address in the heap. Constructors may be overloaded

9 FEN 2013-01-27AK IT: Softwarekonstruktion9 The Anatomy of a Class Classes are usually written by this pattern: class ClassName { declaration of attributes constructors properties methods }

10 FEN 2013-01-27AK IT: Softwarekonstruktion10 Methods Metodenavn (parameter list) { statements } public int SumOf2Ints (int int1, int int2) { int sum; sum = int1 + int2; return sum; } Acessmodifier: public/protected /private Local variable return Parameters

11 FEN 2013-01-27AK IT: Softwarekonstruktion11 The Class BankAccount - attributes and constructor namespace Banking { public class BankAccount { private double balance; private int accNo; private int interestRate; public BankAccount(int no, int ir) { balance = 0; accNo = no; intrestRate = ir; }

12 FEN 2013-01-27AK IT: Softwarekonstruktion12 Methods public bool Withdraw(double amount) public void Deposite(double amout) public void GiveInterest()

13 FEN 2013-01-27AK IT: Softwarekonstruktion13 Properties public int InterestRate { get{return interestRate;} set{if( value>=0) interestRate = value;} } Lets do it in C# using Visual Studio. Source hereSource here.

14 UML class diagrams Can be used for different things: – Domain model (or conceptual model) showing objects in the users world ((problem) domain) and their relationships. – Design class diagram showing the structure of the code. For the moment our class diagrams will do both. FEN 2013-01-27AK IT: Softwarekonstruktion14

15 UML class diagram A class is described by: – Its name – Its attributes (field variables, data members) – Its methods (operations) FEN 2013-01-27AK IT: Softwarekonstruktion15 Class name Attributes Methods For simplicity attributes and/or methods may be omitted, so only structure is shown

16 UML class diagram The classes are connected – in UML: FEN 2013-01-27AK IT: Softwarekonstruktion16 The connection is called an association, and is shown as a line. The multiplicity or cardinality of the association is indicated by numbers. 1 means exactly one, so a bank account belongs to exactly one customer Multiplicity may have a minimum and a maximum: 0..1 means zero or one, so a customer may be associated with zero or one bank account.

17 Object Interaction The Banking example shows object interaction: – The classes are connected – in UML: FEN 2013-01-27AK IT: Softwarekonstruktion17

18 In the Code public class Customer{ //… private BankAccount account; //… public void CreateAccount(int no, double ir, double bal){ account= new BankAccount(no, ir, bal); } //… FEN 2013-01-27AK IT: Softwarekonstruktion18 Customer is responsible for creating BankAccount objects. The association is implemented by an object reference (attribute).

19 In the Code public class Program{ //… Customer c = new Customer(1, "Peter Thomsen"); //… Console.WriteLine("Customer: "+ c.Name +" has DKK “ + c.Account.Balance + " in the bank"); FEN 2013-01-27AK IT: Softwarekonstruktion19 Methods in the other class is called using the reference.

20 Cardinality or Multiplicity Tells how many objects an object may be associated with: – One customer may have one account, an account must belong to a customer. – One customer may have many accounts, an account must belong to one customer. – A customer may have zero or more accounts, an account may belong to one or more customers. FEN 2013-01-27AK IT: Softwarekonstruktion20

21 Exercise Do the first parts of the ForestExercise (classes Owner and Forest, no 1 – 13). The rest of the ForestExercise. FEN 2013-01-27AK IT: Softwarekonstruktion21


Download ppt "Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN 2013-01-271AK IT:"

Similar presentations


Ads by Google