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
Nguyn Vit H
K tha
S dng li
Student, Manager,
K tha
S dng li
Copy m ngun
Tn
cng, d nhm
Kh sa li do tn ti nhiu phin bn
Quan h has_a
S
Nguyn Vit H
K tha
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
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
Person
-name
-birthday
+setName()
+setBirthday()
Employee
-salary
+setSalary()
+getDetail()
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
K tha
13
Nguyn Vit H
K tha
14
Same
class
Same
package
Subclass
private
Yes
package
(default)
Yes
Yes
protected
Yes
Yes
Yes
public
Yes
Yes
Yes
Nguyn Vit H
K tha
Universe
Yes
15
Trong cng gi
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;
}
}
Nguyn Vit H
K tha
19
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
Mi i tng u
tha k t lp gc Object
Student
Employee
-salary
+setSalary
+getDetail
Manager
-rank
...
Nguyn Vit H
K tha
-id
...
Programmer
-project
...
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
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
Tham s final
khng
Lp final
khng
Nguyn Vit H
nh ngha c lp dn xut
K tha
30