Object-Oriented Programming
IUP02
At Exceep camp
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
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
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
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
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
ภาษาไหนเป�น OOP ดู�อย่�างไร
OOP must provide support for 3 key language features
1.encapsulation
2.inheritance
3.dynamic binding
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
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():
Object
camry
carColor = “Blue”
carType = “Car”
carStatus = “Drive”
vigo
carColor = “Green”
carType = “Truck”
carStatus = “Stop”
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
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]
Inheritance
Archer
-HP-AP
+Archery+walk
Camel Archer
+Camel riding
Horse Archer
+Hose riding
Camel Archer ก�บ Horse Archer จะมื�คืวามืสามืารถ เที่�ย่บเที่�าก�บArcher หร�อ มื�คืวามืสามืารถมืากกว�า
Inheritance
EX1
public class CamelArcher : Archer
EX2
public class Form1 : Form
Ex1 CamelArcher จะเป�นตั�วลู�กขีอง Archer แลู*วจะมื�คืวามืสามืารถเที่�าก�น หร�อมืากกว�า
Ex2 จากเมื�!อวาน เราจะเห+นว�ามื�การใช้*งานขีอง inheritance เช้�นFrom1 ขีองน*องๆ จะถ�าย่ที่อดูคืวามืสามืารถตั�างจากclass From
Vending machine
เคืร�!องขีาย่ขีอง ขีองเราจะประกอบดู*วย่
Class ส�นคื*าตั�างๆเช้�น Coke and Fanta
Class เคืร�!องขีาย่ขีอง Class GUI
Vending Machine
ป/0มืส�!งส�นคื*า ป/0มืน�$เมื�!อกดู Class เคืร�!องขีาย่ขีองจะที่1าการดู#ง Method หร�อ Field ขีองคืลูาส ส�นคื*า เพิ�!อที่1าการคื1านวณ เง�นที่�!ตั*องจ�าย่
ป/0มืcheck stock กดูเพิ�!อให* Class เคืร�!องขีาย่ขีองที่1าการเช้+คืส�นคื*าก�บคืลูาสส�นคื*าตั�างๆ
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
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
Top Related