Professional Documents
Culture Documents
Oop Javam05
Oop Javam05
Ni dung
Vn s dng li S dng li bng k tha K tha trong Java
nh ngha lp k tha thm phng thc, thuc tnh kim sot truy cp constructor
Lp Object
Nguyn Vit H K tha 2
Nguyn Vit H
K tha
S dng li
Tn ti nhiu loi i tng c cc thuc tnh v hnh vi tng t hoc lin quan n nhau
Person, Student, Manager,
S dng li
Copy m ngun
Tn cng, d nhm Kh sa li do tn ti nhiu phin bn
Quan h has_a
S dng lp c nh l thnh phn ca lp mi S dng li ci t vi giao din mi
Phi vit li giao din Cha mm do
Nguyn Vit H K tha 5
V d: has_a
class Person { private String name; private Date bithday; public String getName() { return name; } ... } class Employee { private Person me; private double salary; public String getName() { return me.getName(); } ... }
Nguyn Vit H K tha 6
class Manager { private Employee me; private Employee assistant; public setAssistant(Employee e) {...} ... } ... Manager junior = new Manager(); Manager senior = new Manager(); senior.setAssistant(junior); // error
Nguyn Vit H
K tha
K tha
Da trn quan h is_a Tha hng li cc thuc tnh v phng thc c Chi tit ha cho ph hp vi mc ch s dng mi
Thm cc thuc tnh mi Thm hoc hiu chnh cc phng thc
Nguyn Vit H
K tha
Thut ng
K tha Lp c s, lp cha Lp dn xut, lp con
Nguyn Vit H
K tha
Nguyn Vit H
K tha
10
Nguyn Vit H
K tha
11
private members
class Employee extends Person { ... public String getDetail() { String s; // s = name + "," + birthday; s = getName() + "," + getBirthday(); s += "," + salary; return s; } }
Nguyn Vit H
K tha
12
Mc truy cp protected
m bo che du thng tin, thng thng cc thuc tnh c khai bo l private
i tng thuc lp dn xut phi truy cp ti chng thng qua cc phng thc get v set.
public class Person { protected Date birthday; protected String name; ... } public class Employee extends Person { ... public String getDetail() { String s; s = name + "," + birthday; s += "," + salary; return s; } }
Nguyn Vit H
K tha
14
Nguyn Vit H
K tha
15
Trong cng gi
public class Employee extends Person { ... public String getDetail() { String s; s = name + "," + birthday; s += "," + salary; return s; } }
Nguyn Vit H K tha 16
Khc gi
package abc; public class Person { protected Date birthday; protected String name; ... }
import abc.Person; public class Employee extends Person { ... public String getDetail() { String s; s = name + "," + birthday; s += "," + salary; return s; } }
Nguyn Vit H K tha 17
Nguyn Vit H
K tha
18
V d
package abc; public class Person { protected Date birthday; protected String name; public String getDetail() {...} ... }
import abc; public class Employee extends Person { ... public String getDetail() { String s; s = super.getDetail() + "," + salary; return s; } }
19
Nguyn Vit H
K tha
Nguyn Vit H
K tha
20
class Parent { public void doSomething() {} public int doSomething2() { return 0; } } class Child extends Parent { protected void doSomething() {} public void doSomething2() {} }
Nguyn Vit H
K tha
21
Student
Nguyn Vit H
K tha
22
Constructor
Lp dn xut k tha mi thuc tnh v phng thc ca lp c s Khng k tha phng thc khi to C hai gii php gi constructor ca lp c s
s dng constructor mc nh gi constructor ca lp c s mt cch tng minh
Nguyn Vit H K tha 23
class Point { protected int x, y; public Point() {} public Point(int xx, int yy) { x = xx; y = yy; } } class Circle extends Point { protected int radius; public Circle() {} } Point p = new Point(10, 10); Circle c1 = new Circle(); Circle c2 = new Circle(10, 10); // erorr
Nguyn Vit H K tha 24
Gi constructor ca lp c s
Vic khi to thuc tnh ca lp c s nn giao ph cho constructor ca lp c s S dng t kha super gi constructor ca lp c s
Constructor ca lp c s bt buc phi c thc hin u tin Nu lp c s khng c constructor mc nh th bt buc phi gi constructor tng minh
Nguyn Vit H K tha 25
class Point { protected int x, y; public Point() {} public Point(int xx, int yy) { x = xx; y = yy; } } class Circle extends Point { protected int radius; public Circle() {} public Circle(int xx, int yy, int r) { super(xx, yy); radius = r; } }
Nguyn Vit H K tha 26
class Point { protected int x, y; public Point(int xx, int yy) { x = xx; y = yy; } } class Circle extends Point { protected int radius; public Circle() { super(0, 0); } public Circle(int xx, int yy, int r) { super(xx, yy); radius = r; } }
Nguyn Vit H
K tha
27
class Point { protected int x, y; public Point() {} public Point(int xx, int yy) { x = xx; y = yy; } } class Circle extends Point { protected int radius; public Circle() { } public Circle(int xx, int yy, int r) { // super(xx, yy); radius = r; } }
Nguyn Vit H K tha 28
Th t khi to
class Point { protected int x, y; public Point() { System.out.println("Point constructor"); } } class Circle extends Point { protected int radius; public Circle() { System.out.println("Circle constructor"); } } ... Circle c = new Circle();
Nguyn Vit H
K tha
29
T kha final
Thuc tnh final
hng s, ch c gn gi tr khi to mt ln, khng thay i c gi tr
Tham s final
khng thay i c gi tr ca tham chiu
Lp final
khng nh ngha c lp dn xut
30
Nguyn Vit H
K tha