Download presentation
Presentation is loading. Please wait.
Published byJasmin Whitehead Modified over 8 years ago
1
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science
2
FEN 2012UCN Technology: Computer Science2 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle
3
FEN 2012UCN Technology: Computer Science3 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, talk 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 2012UCN Technology: Computer Science4 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 2012UCN Technology: Computer Science5 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.: –Account: 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 2012UCN Technology: Computer Science6 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 2012UCN Technology: Computer Science7 Properties (C# speciality) Are used for getting and setting attribute values. (Replace set- and get-methods in Java). Provide a syntax similiar to direct access of the attributes. (Anders Hejsberg footprint?)
8
FEN 2012UCN Technology: Computer Science8 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 –Account acc = new Account(); Account() 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 2012UCN Technology: Computer Science9 The Anatomy of a Class Classes are usually written by this pattern: class ClassName { declaration of attributes constructors properties methods }
10
FEN 2012UCN Technology: Computer Science10 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 2012UCN Technology: Computer Science11 The Class Account - attributes and constructor namespace Banking { public class Account { private double balance; private int accNo; private int interestRate; public Account(int no, int ir) { balance = 0; accNo = no; intrestRate = ir; }
12
FEN 2012UCN Technology: Computer Science12 Methods public bool Withdraw(double amount) public void Deposite(double amout) public void GiveInterest()
13
FEN 2012UCN Technology: Computer Science13 Properties public int InterestRate { get{return interestRate;} set{if( value>=0) interestRate = value;} } Lets do in C# using Visual Studio. Source here.here
14
FEN 2012UCN Technology: Computer Science14 Exercise Create a VS project using this code: EmpProjectV1.rar EmpProjectV1.rar Test it – understand it. Banking exercise.Banking exercise
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.