1 oop in c#: object interaction. inheritance and polymorphism oop in c# - del 1

17
1 OOP in C#: Object Interaction. Inheritance and Polymorphism OOP in C# - Del 1

Upload: aron-greene

Post on 29-Dec-2015

248 views

Category:

Documents


2 download

TRANSCRIPT

1

OOP in C#:

Object Interaction. Inheritance and Polymorphism

OOP in C# - Del 1

Object-Oriented Programming

“ The Three Pillars of OOP”:EncapsulationInheritancePolymorphism

The Substitution Principle

UCN Technology: Computer Science 2

3

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

Definition of Object and Class

ObjectRepresents 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.

ClassA 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

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

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

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?)

Java kursus KMD 8

Properties (Auto property)

private string name;public string Name{ get { return this.name; } set { this.name = value; }}

9

Auto-Implemented Properties

Java kursus KMD

// This class is mutable. Its data can be modified from // outside the class. class Customer{ // Auto-Impl Properties for trivial get and set public double TotalPurchases { get; set; } public string Name { get; set; } public int CustomerID { get; set; }

// Constructor public Customer(double purchases, string name, int ID) { TotalPurchases = purchases; Name = name; CustomerID = ID; }

Java kursus KMD 10

Auto-Implemented Properties

// This class is immutable. After an object is created, // it cannot be modified from outside the class. It uses a // constructor to initialize its properties. class Contact { // Read-only properties. public string Name { get; private set; } public string Address { get; private set; }

// Public constructor. public Contact(string contactName, string contactAddress) { Name = contactName; Address = contactAddress; } }

11

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

12

The Anatomy of a Class

Classes are usually written by this pattern:

class ClassName {

declaration of attributesconstructorspropertiesmethods

}

13

Methods

<access modifier> <return type> Metodenavn (parameter list) { statements}

public int SumOf2Ints (int int1, int int2) {

int sum;sum = int1 + int2;return sum;

}

Acessmodifier: public/protecte

d/private

• Local variable• return• Parameters

14

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;

}

FEN 2012 15

Methods

public bool Withdraw(double amount)

public void Deposite(double amout)

public void GiveInterest()

16

Properties

public int InterestRate{

get{return interestRate;}set{if( value>=0) interestRate = value;}

}

Lets do in C# using Visual Studio.Source here.

17

Opgaver

Lav et MS VS projekt brug denne kode:EmpProjectV1.rar

Test kode – understand it.