object-oriented programming

18
Object-Oriented Programming IUP02 At Exceep camp

Upload: dexter-orr

Post on 30-Dec-2015

28 views

Category:

Documents


0 download

DESCRIPTION

Object-Oriented Programming. IUP02 At Exceep camp. class Car { private string carColor; private string carType; private string carStatus = “Stop”; public Car(string color,string type){ carColor = color; cartype = type; } public void drive (){ - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Object-Oriented Programming

Object-Oriented Programming

IUP02

At Exceep camp

Page 2: Object-Oriented Programming

The vocabulary of OOP

class Car { private string carColor; private string carType; private string carStatus = “Stop”; public Car(string color,string type){ carColor = color; cartype = type; } public void drive (){ carStatus = “Drive”; } public void stop(){ carStatus = “Stop”; }}

การประกาศ class

Page 3: Object-Oriented Programming

The vocabulary of OOP

class Car { private string carColor; private string carType; private string carStatus = “Stop”; public Car(string color,string type){ carColor = color; cartype = type; } public void drive (){ carStatus = “Drive”; } public void stop(){ carStatus = “Stop”; }}

Fields

Page 4: Object-Oriented Programming

The vocabulary of OOP

class Car { private string carColor; private string carType; private string carStatus = “Stop”; public Car(string color,string type){ carColor = color; cartype = type; } public void drive (){ carStatus = “Drive”; } public void stop(){ carStatus = “Stop”; }}

Methods

Page 5: Object-Oriented Programming

The vocabulary of OOP

class Car { private string carColor; private string carType; private string carStatus = “Stop”; public Car(string color,string type){ carColor = color; cartype = type; } public void drive (){ carStatus = “Drive”; } public void stop(){ carStatus = “Stop”; }}

Constructor

Page 6: Object-Oriented Programming

The vocabulary of OOP

class carTesting{

static void Main(string[] args){

Car camry = new Car(“Blue”,”Car”);

Car vigo = new Car(“Green”,”Truck”);

}

camry.drive();

Console.WriteLine(“Camry status=”+ camry.carStatus);

Console.WriteLine(“Vigo status=”+ vigo.carStatus);

}

Object

camry ก�บ vigo คื�อ object

Out put: Camry status=drive

Vigo status=stop

Page 7: Object-Oriented Programming

ภาษาไหนเป�น OOP ดู�อย่�างไร

OOP must provide support for 3 key language features

1.encapsulation

2.inheritance

3.dynamic binding

Page 8: Object-Oriented Programming

Class

Class เหมื�อน พิ�มืพิ�เขี�ย่ว ขีอง Object ที่�!จะเก�ดูขี#$น

Car

- carColor:String

-carType:String

-carStatus:String

+drive()

+stop()

Example:

Class Name: Car

Field: carColor, cartype, carStatus

Method: drive, stop

An UML class diagram

Page 9: Object-Oriented Programming

Object

An object is an actual instance of the class. Object’s characteristics are behavior (defined by the class),

state (the value of its attributes) and identity (a name).

Car

- carColor:String-carType:String-carStatus:String

+drive()+stop()

Example1: Car camry = new Car(“Blue”,”Car”); Car vigo = new Car(“Green”,”Truck”);/*Camry Fields carColor = Blue and carType = Car *//*Vigo Fields carColor = Green and carType = Truck*/Example2: Random ran = new Random():

Page 10: Object-Oriented Programming

Object

camry

carColor = “Blue”

carType = “Car”

carStatus = “Drive”

vigo

carColor = “Green”

carType = “Truck”

carStatus = “Stop”

Page 11: Object-Oriented Programming

Class versus Objects

Defines the properties

and behavior for all instances

(objects) of this class.

Specific realization of the class

James Brucker’s slide 03 object

Page 12: Object-Oriented Programming

Inheritance

“Inheritance is a powerful concept that greatly enhances the potential reuse of existing software, thereby providing the possibility of significant increases in software development productivity.”

(concepts of Programming Languages seventh edition Robert W. Sebesta)

Archer(Parent) Camel

Archer

HorseArcher

[Camel Archer & Horse ArcherExtends Archer]

Page 13: Object-Oriented Programming

Inheritance

Archer

-HP-AP

+Archery+walk

Camel Archer

+Camel riding

Horse Archer

+Hose riding

Camel Archer ก�บ Horse Archer จะมื�คืวามืสามืารถ เที่�ย่บเที่�าก�บArcher หร�อ มื�คืวามืสามืารถมืากกว�า

Page 14: Object-Oriented Programming

Inheritance

EX1

public class CamelArcher : Archer

EX2

public class Form1 : Form

Ex1 CamelArcher จะเป�นตั�วลู�กขีอง Archer แลู*วจะมื�คืวามืสามืารถเที่�าก�น หร�อมืากกว�า

Ex2 จากเมื�!อวาน เราจะเห+นว�ามื�การใช้*งานขีอง inheritance เช้�นFrom1 ขีองน*องๆ จะถ�าย่ที่อดูคืวามืสามืารถตั�างจากclass From

Page 15: Object-Oriented Programming

Vending machine

เคืร�!องขีาย่ขีอง ขีองเราจะประกอบดู*วย่

Class ส�นคื*าตั�างๆเช้�น Coke and Fanta

Class เคืร�!องขีาย่ขีอง Class GUI

Page 16: Object-Oriented Programming

Vending Machine

ป/0มืส�!งส�นคื*า ป/0มืน�$เมื�!อกดู Class เคืร�!องขีาย่ขีองจะที่1าการดู#ง Method หร�อ Field ขีองคืลูาส ส�นคื*า เพิ�!อที่1าการคื1านวณ เง�นที่�!ตั*องจ�าย่

ป/0มืcheck stock กดูเพิ�!อให* Class เคืร�!องขีาย่ขีองที่1าการเช้+คืส�นคื*าก�บคืลูาสส�นคื*าตั�างๆ

Page 17: Object-Oriented Programming

Class ส�นคื*าprivate int price = 15;

private int quantity = 100;

public int getPrice(){

return price;

}

public int getQuatity(){

return quantity;

}

public void setQuatity(int number){

quantity = number

}

Setter ก�บ Getter ในการเขี*าถ#งขี*อมื�ลูที่�!ถ�กprivate

Getter ใช้*ในการแสดูงคื*าตั�วแบบที่�!อย่��ภาย่ในClass

Setter ใช้*ในการsetคื�าตั�วแปรในClass

Page 18: Object-Oriented Programming

Web ที่�!แนะน1า

http://en.wikipedia.org/wiki/Object-oriented_programming

http://java.sun.com/docs/books/tutorial/java/concepts/index.html

http://bdn.borland.com/article/0,1410,31863,00.html

http://www.cs.indiana.edu/classes/c304/oop.html