Date post: | 17-Feb-2017 |
Category: |
Education |
Upload: | hum-chivorn |
View: | 126 times |
Download: | 4 times |
Java Presentation
មជ្ឈមណ្ឌ លកូរ៉េ � សហ្វវែ� រ៉េ�ច � ឌីKorea Software HRD Center
www.kshrd.com.kh
ក្រកមុទី៤៖ រ៉េ�ក ជឹម មិនា រ៉េ�ក ជាគីមហា� រ៉េ�ក ហំុ៊ ជី�ន័ រ៉េ�ក ស៊៊ាង គង្គា* រ៉េ�ក គីម បុ៊នហុង
ថ្នា- ក់ ភ-ំរ៉េ0ញ ក្រកមុទី ៤
ក្របធានបទ៖ មូលដ្ឋា4 នក្រគឹះបស់ភាស៊ា Java
សមាជិកលោ�ក ជាគីមហាវលោ�ក ហំុ៊ ជីវន័លោ�ក ស៊៊ាង គង្គា�លោ�ក គីម បុ៊នហុង
3
មាតិកា
1 .លោ�ីអ្វីជា OOP?
2. លោ�ីអ្វីជា Class និង Object ?
3. លោ�ីអ្វីជា Encapsulation ?
4. លោ�ីអ្វីជា Inheritance?
5. លោ�ីអ្វីជា Abstraction (Abstract class Interface)
6. លោ�ីអ្វីជា Polymorphism ?
4
OOP
ពាក្យថា OOP មានន័យថា Object Oriented Programming លោ ីម្បីជួយលោ#ះស្រាស៊ាយលោ&បញ្ហា( ទាងំឡាយ។
មូល#. នស្រាគឹះរបស់ OOP គឺជា class លោ ីម្បីស៊ា� ល់ថា Program មួយមានលក្ខណៈជា OOP លុះស្រា7តែ� Program លោ9ះបំលោ:ញនូវលក្ខខណ្ឌ ៣យ៉ា@ ងគឺ៖
លោ�ីអ្វីលោBជា OOP ?
1. Encapsulation2. Inheritance 3. Polymorphism
5
OOP
Class គឺជា:ុម្ពគំរសូស្រាមាប់បលោងDី� Object ឬ កំណ�់ទស្រាមង់របស់ Object ។ Java លោស្រាបីលក្ខណៈ:ិលោសសរបស់ Class លោ ីម្បីបលោងDី� Object ។
លោ�ីអ្វីលោBជា Class ?
Instance Variable ( Data Member ) និង Method តែ ល Instance Variable និង Method គឺជា Member របស់ Class។
6
OOP
class class-name{type instance-variable1;type instance-variable2;//….type instance-variableN;type methodname 1(parameter-list){//body of method
}type methodname 2(parameter-list){//body of method
}type methodname N(parameter-list){//body of method
}
}
ទំរង់ទូលោBរបស់ Class
7
OOP
Class Vehicle{int passengers; //number of passengersint fuelcap; //fuel capacity in litersint kpl; //fuel consumption in kilometers per liter
}
ការបលោងDី� Class
o Class អាចបលោងDី�នូវស្រាបលោKទទិន្នន័យ (Data type) ថ្មីមួយ ។ ក្នងុករណីលោនះ ស្រាបលោKទទិន្នន័យថ្មីលោ9ះលោO្ម ះថា Vehicle លោហីយលោយីងនឹងលោស្រាបីលោO្ម ះលោនះលោ ីម្ីបស្រាបកាស Objects ។
Vehicle minivan = new Vehicle();
OOP
Object គឺជា Instance របស់ Class។ Object ជាអ្វីៗតែ លលោ&ជំុវញិខ្លួនលោយីង ូចជា កង់ ម@ូ�ូ ឡាន លោសSវលោT បិ៊ច កំុ:្យូទ័រ…។
លោ�ីអ្វីលោBជា Object ?
Object មាន State និង Behavior។ State ជាលក្ខណៈ ឬ ធា�ុផ្ំសសស្រាមាប់បលោងDី� Object ។ Ex1: មនុស្សមាន សម្បរុ កំ:ស់ អាយុ លោKទ ។ Ex2:ម@ូ�ូមាន កង់ តែកប ស្រាចវ៉ាZក់ លោK្លីងសីុញ@ូ … ។ Behavior ជាអ្វីតែ ល Object អាចលោ[្វីបាន។ Ex1: មនុស្សមាន លោ ីរ លោ ក អង�ុយ ឈរ យំ លោសីច….។ Ex2:ទូរ ATM មាន កស្រាបាក់ #ក់ស្រាបាក់ …. ។
9
OOP
រលោបSបបលោងDី� Objects
Vehicle minivan = new Vehicle();• លោយីងស្រាបកាសអញ្ញ�ិ្តមួយមានលោO្ម ះថា minivan តែ លមាន Vehicle ជាស្រាបលោKទទិន្នន័យ
(Reference of Object)។ • new operator បលោងDី�ទី7ងំក្នងុ Memory របស់ Object មួយលោហីយផ្តល់
Reference លោBឲ្យ Object លោ9ះ ។• Reference ជាអាស័យ#. ននៃន Object លោ&ក្នងុ Memory តែ លបលោងDី�ទី7ងំលោ#យ
new ។
10
OOP
រលោបSបបលោងDី� Methods
ret-type methName(param-list){
//body of method}
ret-type បញ្ហាc ក់:ីស្រាបលោKទទិន្នន័យតែ លស្រា�វូបានផ្តល់ឲ្យ Method ។ វ៉ាអាចជាស្រាបលោKទទិន្នន័យ[ម្ម7 ឬ អាចស្រាបលោKទជា Class តែ លលោយីងបានបលោងDី�លោeងី ។ Method មិនស្រា�វូការផ្តល់�នៃម្លលោទ លោ9ះស្រាបលោKទទិន្នន័យរបស់វ៉ាស្រា�វូតែ�ជា void ។
methName ជាលោO្ម ះរបស់ Method ។ param-list ជាស្រាបលោKទទិន្នន័យ និង លោO្ម ះអញ្ញ�្តិលោស្រាបីបន្តគ្នា្ន តែ លនឹងតែញកផ្តា្ត ច់:ីគ្នា្ន លោ#យ សញ្ហា្ញ លោក្បhស (,)។
11
Encapsulation
Encapsulation វ៉ាជាតែផ្នកមួយនៃន OOP concept។ លោ[្វីលោអាយ Field លោ&ក្នងុ class មួយមិនអនុញ្ហា្ញ �លោអាយមានការ access ចូល:ី class នៃទ។លោពាលគឺ variable លោ&ក្នងុ class មួយស្រា�វូបាន hide :ី class នៃទ លោហីយវ៉ាអាច access បាន7មរយៈ method getter() និង setter()។
លោ�ីអ្វីលោBជា Encapsulation ?
12
Encapsulation
Field របស់ class អាចកំណ�់ជា Read-only ឬ Write-only. Class អាចស្រាគប់ស្រាគងទាងំស្រាសងុលោBលោលី data លោ#យលោស្រាបីស្រាបាស់ code។
គុណសម្ប�្តិរបស់ Encapsulation ?
អ្នកលោស្រាបីស្រាបាស់ class មិន ឹង:ីរលោបSបតែ ល class ផ្ទុក(store) ទិន្ទន័យ។ Class អាចប្តូរនូវ data type នៃន field មួយ។ អ្នកលោស្រាបីស្រាបាស់់ class មិនស្រា�វូការប្តូរ code របស់លោគ។
13
Encapsulation
public class TestJava {
private int id;private String name;private int age;
public void setId(int id){this.id=id;}public int getID(){return id;}public void setName(String name)
{this.name=name;}public String getName(){return name;}public void setAge(int age){
if(age<0){this.age=1;
}else{this.age=age;}
}public int getAge(){return age;}
}
Public class DemoEncap{public static void main(String args[]){TestJava obj= new TestJava();
obj.setId(100);obj.setName("Hong");obj.setAge(-1);
System.out.println("ID:"+ obj.getID() + "\nName:"+ obj.getName()+ "\nAge:"+ obj.getAge());
}}
14
Inheritance
លោ�ីអ្វីលោBជា Inheritance?
Inheritance គឺជាលោផ្ទរមរ�ក:ី Class មួយលោB Class មួយលោផ្សងលោទS� ។ Class តែ លលោផ្ទរ�នៃម្លលោអាយលោគលោkថា Parent Class លោហីយ Class តែ លទទួល�នៃម្លលោkថា Child Class។ Inheritance អនុញ្ហា្ញ �ិឲ្យ Class មួយលោស្រាបីស្រាបាស់លោ& properties និង methods នៃន class លោផ្សងលោទS�បាន ។ Inheritance គឺជា compile-time mechanism ។ super-class អាចមាន subclasses នៃនមួយចំនួន។ ប@ុតែន្ត subclass មានស្រា�ឹមតែ� super-class មួយប@ុលោlm ះ ។
15
Inheritance
លោ�ី Inheritance លោ[្វីការយ៉ា@ ង ូចលោម្តច?
Extends: លោ&ក្នងុJava លោ ីម្បីលោ[្វីការ Inheritance :ី Parent Class លោBChild Class លោយីងស្រា�វូលោស្រាបីស្រាបាស់ Keyword Extends។ Super: លោគលោស្រាបីវ៉ាសស្រាមាប់កំណ�់លក្ខណៈMemberរបស់ Classលោម ករណីតែ ល Method និង Variable របស់ Classលោមនិង Classកូន ូចគ្នា្ន ។ Implement: ជាKeyword មួយតែ លអនុញ្ហា្ញ �លោអាយ Class លោ[្វីការទទួលមរ�ក:ី Interface។
16
Inheritance
class ClassA{ int a=; public void add(int x, int y){ a = x+y; System.out.println("The sum of the given numbers:"+a); }}public class ClassB extends ClassA{ public void multiplication(int x, int y){ a= x*y; System.out.println("The product of the given numbers:"+a); } public static void main(String args[]){ int a = 20, b = 10; ClassB sub = new ClassB (); sub.add(a, b); sub.multiplication(a, b); } }
ឧទាហរណ៏ៈ លោយីងមាន Class:ីរ គឺ ClassA និងClassB តែ លClassBលោ[្វីការ Inheritannce�នៃម្ល:ី ClassA។
17
Inheritance
ឧទាហរណ៏ៈpublic class Sub_class extends Super_class { int num = 10; //display method of sub class public void display(){ System.out.println("This is the display method of subclass");} public void my_method(){ Sub_class sub = new Sub_class();//Instantiating subclass sub.display();//Invoking the display() method of sub class super.display();//Invoking the display() method of superclass System.out.println("value of the variable named num in sub class:"+ sub.num); //printing the value of variable num of superclass System.out.println("value of the variable named num in super class:"+ super.num); }public static void main(String args[]){ Sub_class obj = new Sub_class(); obj.my_method(); }
class Super_class{
int num = 20; //display method of superclass
public void display(){ System.out.println("This is the display method of superclass");}}
18
Inheritance
ឧទាហរណ៏ៈ Public Interface Motor{void speedUp(int increment);
}class MyMotor implement Motor{ int speed = 0; void speedUp(int increment) { speed = speed + increment; } void printStates() { System.out.println("My motor speed" +speed); } }
19
Inheritance
ប្លោKទនៃន Inheritance Single inheritance in java multilevel inheritance multiple inheritance hybrid inheritance hierarchical inheritance
20
Abstraction
រាល់ programming languages តែ�ងតែ�ផ្តល់ abstractions ។
វ៉ាជួយផ្តល់លោ& ំលោlះស្រាស៊ាយតែ លមានភា:ស្មគុស៊ា្ម ញនៃនបញ្ហា( (លោ#ះស្រាស៊ាយ ល់ផ្តា្ទ ល់លោB7មស្រាបលោKទនិង គុណសម្ប�្តិនៃន abstraction ។
លោ�ីអ្វីលោBជា Abstraction?
Abstraction វ៉ាជាធា�ុសំខាន់មួយនៃន object-oriented programming Abstraction ជា ំលោណីរការនៃនការ�ក់បាងំលោសចក្តីលំអិ�នៃនការអនុវ�្តន៍( implementation)លោហីយបង្គា( ញតែ� �ួ9ទីតែ�មួយគ�់លោB ល់អ្នកលោស្រាបីស្រាបាស់។
21
Abstraction
Abstract class ជា class មួយតែ លស្រាបកាសជា abstract ។
Abstract method គឺជា methodមួយតែ លស្រាបកាសជា abstract ។
លោ�ីអ្វីលោBជា Abstract class?
Abstract Class ក៏រក្សាទុកលោ& abstract method មួយឬលោស្រាចីន។
Syntax: abstract class (class name){ //abstract methods }
22
Abstraction
ចំl៖ំ
o ប្សិនជាមាន abstract method លោស្រាចីនលោ&ក្នងុ class តែ�មួយ លោ9ះ class ស្រា�វូតែ�ជា abstract ។o ប្សិនជាអ្នកកំ:ុងតែ� extend abstract class លោស្រាចីនតែ លមាន abstract method
អ្នកមិនស្រា�វូផ្តល់ការ implementation លោBលោអាយ method ឬ លោ[្វីលោអាយ class លោ9ះ កា្ល យជា abstract class ។
23
Abstraction
លោ�ីអ្វីលោBជា Interface ?
Interface លោមីលលោB ូចជា class ប@ុតែន្តវ៉ាមិនតែមនជា class លោទ។
Interface គឺលោស្រាបីលោ& default abstract (មានស្រា�ឹមតែ� method signature គ្នា្ម ន body លោទ)។
Interface លោ ីរ�ួ9ទីជា blueprint (ប្លង់)សំរាប់ class ។ Interface មិនអាចបលោងDី� object បានលោទ។
24
Abstraction
លោហ�ុអ្វីលោយីងស្រា�វូលោស្រាបី Interface ?
លោ[្វីឲ្យ abstract មួយ មានលក្ខណៈលោ:ញលោលញ ។ 7មរយៈ interface លោយីងអាចស្រាប�ិប�្តិការមុខង្គាររបស់ multi-inheritance។ វ៉ាអាចស្រា�វូលោស្រាបីស្រាបាស់លោ ីម្បីបំលោ:ញ loose coupling ។
25
Abstraction
1. Abstract class VS Interface:
ភា0ដូចគ្នា- វាង Abstract class & Interface: អាចឲ្យ abstraction និងអាចស្រាបកាស abstract method ។
Abstract class & Interface មិនអាចជា instantiated។
26
Abstraction
Abstract class Interface
1) Abstract class អាចមានឬគ្នា្ម ន(0%-100% ) abstract method។
Interface ស្រា�វូតែ�មាន (100%) abstract method។
2) Abstract class មិន support multiple inheritance។
Interface supports multiple inheritance.
3) Abstract class អាចមាន final, non-final, static and non-static variables។
Interface អាចមាន static and final variables ប@ុលោlm ះ។
4) Abstract class អាចមាន static methods, main method and constructor។
Interface មិនមាន static methods, main method and Constructor។
5) Abstract class អាចផ្តល់ implementation របស់interface។
Interface មិនអាចផ្តល់ implementation របស់ abstract class បានលោទ។
6) The abstract keyword គឺលោស្រាបីលោ ីម្ីបស្រាបកាស់ abstract class។
The Interface keyword គឺលោស្រាបីលោ ីម្ីបស្រាបកាស់ Interface។
7) Example:public abstract class Shape{public abstract void draw();}
Example:public interface Drawable{void draw();}
ភា0ខុសគ្នា- វាង Abstract class & Interface:
27
Polymorphism
Polymorphism មក:ីភាស៊ាលោស្រាកក :ីរពាក្យគឺ poly និង morphs ។ Poly មានន័យថា លោស្រាចីន និង morphs មានន័យថា ទំរង់ ។ polymorphism មាន:ីរស្រាបលោKទលោ&ក្នងុ java គឺ
1) compile time polymorphism(static polymorphism)
2) runtime polymorphism (Dynamic polymorphism )
លោ�ីអ្វីលោBជា Polymorphism?
លោយីងអាចអនុវ�្ត polymorphism លោ&ក្នងុ java លោ#យ method overloading និង method overriding ។
28
Polymorphism
លោ�ីអ្វីលោBជា Runtime Polymorphism ? Method overriding គឺជាឧទាហរណ៍ ៏ល្អចំលោពាះ runtime polymorphism ។
Y obj = new Y(); //Parent class reference can be assigned to child object X obj = new Y();
លោ7ះលោយីងលោBលោមីលឧទាហរណ៍លោ ីម្បីយល់កាន់ច្បាស់ public class X { public void methodA() //Base class method { System.out.println ("hello, I'm methodA of class X"); } } public class Y extends X { public void methodA() //Derived Class method { System.out.println ("hello, I'm methodA of class Y"); } } public class Z { public static void main (String args []) { X obj1 = new X(); // Reference and object X X obj2 = new Y(); // X reference but Y object obj1.methodA(); obj2.methodA(); } }
Output:hello, I'm methodA of class X hello, I'm methodA of class Y
29
Polymorphism
លោ�ីអ្វីលោBជា Compile time Polymorphism ? Method overloading គឺជាឧទាហរណ៍ ៏ល្អចំលោពាះ compile time polymorphism ។
Y obj = new Y(); //Parent class reference can be assigned to child object X obj = new Y();
លោ7ះលោយីងលោBលោមីលឧទាហរណ៍លោ ីម្បីយល់កាន់ច្បាស់ class X { void methodA(int num) { System.out.println ("methodA:" + num); } void methodA(int num1, int num2) { System.out.println ("methodA:" + num1 + "," + num2); } double methodA(double num) { System.out.println("methodA:" + num); return num; } } class Y { public static void main (String args []) { X Obj = new X(); double result; Obj.methodA(20); Obj.methodA(20, 30); result = Obj.methodA(5.5); System.out.println("Answer is:" + result); } }
Output:methodA:20 methodA:20,30 methodA:5.5 Answer is:5.5
30
Polymorphism
អនុញ្ហា្ញ �ិឲ្យ interface មួយអាចស្រា�វូបានលោស្រាបីស្រាបាស់ជាទូលោBលោ&ក្នងុ class ។ ស្រាប�ិប�្តិការមួយអាចមាន behavior ខុសគ្នា្ន លោ&ក្នងុករណីលោផ្សងគ្នា្ន ។
លោ�ី Polymorphism លោ[្វីការយ៉ា@ ង ូចលោម្តច?
Behavior :ឹងតែផ្អកលោBលោលីស្រាបលោKទនៃន data តែ លបានលោស្រាបីលោ&ក្នងុលោ:លអនុវ�្ត។ វ៉ាលោ ីរ�ួ9ទីយ៉ា@ ងសំខាន់លោ&ក្នងុការអនុញ្ហា្ញ �ិឲ្យ object តែ លមាន internal structures លោ ីម្បីតែចករតំែលក external interface ។
Polymorphism ស្រា�វូបានលោគលោស្រាបីយ៉ា@ ងទូលំទូ�យក្នងុការ implement inheritance ។
31
Polymorphism
លោ�ីអ្វីជា Method Overloading ?
Method Overloading មានលោO្ម ះ ូចគ្នា្ន តែ�ខុសគ្នា្ន លោ&ចំនួន និង data type នៃន parameter ។ class Overload { void demo (int a) { System.out.println ("a: " + a); }
void demo (int a,int b){
System.out.println ("a and b: " + a + "," + b);
} double demo(double a) { System.out.println("double a: " + a); return a*a; } }
class MethodOverloading { public static void main (String args []) { Overload Obj = new Overload(); double result; Obj .demo(10); Obj .demo(10, 20); result = Obj .demo(5.5); System.out.println("O/P : " + result); } }
Output:a: 10 a and b: 10,20 double a: 5.5 O/P : 30.25
32
Polymorphism
លោ�ី Method Overloading លោ[្វីការយ៉ា@ ង ូចលោម្តច ?1. Method Overloading អាចលោ[្វីការលោ&ក្នងុ class ូចគ្នា្ន ឬ លោ&ក្នងុ sub-class របស់វ៉ា ។2. Constructor លោ&ក្នងុ Java អាចជា Overloaded3. Overloaded methods គួរតែ�មាន argument list ខុសគ្នា្ន4. Parameter អាចខុសគ្នា្ន លោBលោលី type ឬ number ឬក៏ ទាងំ:ីរតែ�ម្តង ។5 .:ួកវ៉ាអាចមាន return types ខុសគ្នា្ន ឬ ូចគ្នា្ន ។6 .វ៉ាក៏ស្រា�វូបានលោគលោkថាជា compile time polymorphism ។
33
Polymorphism
លោ�ីអ្វីជា Method Overriding ?
Method Overriding មានលោO្ម ះ ូចគ្នា្ន ចំនួន និង data type នៃន parameter ក៏ ូចគ្នា្ន ។ public class BaseClass { public void methodToOverride() //Base class method { System.out.println ("I'm the method of BaseClass"); } } public class DerivedClass extends BaseClass { public void methodToOverride() //Derived Class method { System.out.println ("I'm the method of DerivedClass"); } } public class TestMethod { public static void main (String args []) { // BaseClass reference and object BaseClass obj1 = new BaseClass(); // BaseClass reference but DerivedClass object BaseClass obj2 = new DerivedClass(); // Calls the method from BaseClass class obj1.methodToOverride(); //Calls the method from DerivedClass class obj2.methodToOverride(); } }
Output:I'm the method of BaseClass I'm the method of DerivedClass
34
Polymorphism
លោ�ី Method Overriding លោ[្វីការយ៉ា@ ង ូចលោម្តច ?1 .អនុញ្ហា្ញ �ិតែ�ចំលោពាះ inherited methods2. Object type (មិនតែមនជា reference variable type)
លោ#យកំណ�់ overridden method និងស្រា�វូបានលោស្រាបីលោ&លោ:ល runtime
3. Overriding method អាចមាន return type ខុសគ្នា្ន4. Overriding method មិនស្រា�វូមាន restrictive access
modifier ។5. Abstract methods ស្រា�វូបានអាច overridden 6. Static និង final method មិនអាច overridden7 .វ៉ាស្រា�វូបានលោគស៊ា� ល់ថាជា Runtime polymorphism ។
35
10. ក្របភ0ឯកស៊ា
36
សូម�គុណ!!!