Professional Documents
Culture Documents
Bai 04
Bai 04
3 4
5 6
1
8/24/2011
1.1. Chồng phương thức (3) Một số chú ý với chồng phương thức
Ví dụ 2:
class MyDate {
int year, month, day;
public boolean setMonth(int m) { …}
public boolean setMonth(String s) { …}
}
public class Test{
public static void main(String args[]){
MyDate d = new MyDate();
d.setMonth(9);
d.setMonth(”September”);
}
} 7 8
2
8/24/2011
Ví dụ //(cont.)
private double degreeToRadian(double degrees) {
public class Ship { return(degrees * Math.PI / 180.0);
private double x=0.0, y=0.0
}
private double speed=1.0, direction=0.0;
public void move() {
public String name;
move(1);
public Ship(String name) { }
this.name = name; public void move(int steps) {
} double angle = degreesToRadians(direction);
public Ship(String name, double x, double y) { x = x + (double)steps*speed*Math.cos(angle);
this(name); this.x = x; this.y = y;
y = y + (double)steps*speed*Math.sin(angle);
}
public Ship(String name, double x, double y, }
double speed, double direction) { public void printLocation() {
this(name, x, y); System.out.println(name + " is at ("
this.speed = speed; + x + "," + y + ").");
this.direction = direction; }
}
13 } //end of Ship class 14
//continue…
15 16
Phương thức:
17 18
3
8/24/2011
19 20
Ví dụ 1 Ví dụ 2
class TestStatic{ public class Demo {
public static int iStatic;
public int iNonStatic; int i = 0;
} void tang(){ i++; }
public class TestS {
public static void main(String[] args) { public static void main(String[] args) {
TestStatic obj1 = new TestStatic(); tang();
obj1.iStatic = 10; obj1.iNonStatic = 11;
System.out.println(obj1.iStatic+”,”+obj1.iNonStatic); System.out.println("Gia tri cua i la" + i);
TestStatic obj2 = new TestStatic();
}
System.out.println(obj2.iStatic+”,”+obj2.iNonStatic);
obj2.iStatic = 12; }
System.out.println(obj1.iStatic+”,”+obj1.iNonStatic);
}
}
21 22
23 24
4
8/24/2011
25 26
27 28
i 201
j 201
29 30
5
8/24/2011
31 32
3.4. So sánh đối tượng (2) 3.4. So sánh đối tượng (3)
Đối với các đối tượng, toán tử == có ý nghĩa Bất kỳ đối tượng nào cũng có phương thức
khác equals
Ví dụ:
35 36
6
8/24/2011
37 38
4. Truyền tham số cho phương thức (2) 4.1. Với kiểu dữ liệu tham trị
Java: pass-by-value Các giá trị nguyên thủy không thể thay đổi
khi truyền như một tham số
39 40
41 42
7
8/24/2011
43 44
45 46
47 48
8
8/24/2011
a. Tham chiếu giữa các lớp a. Tham chiếu giữa các lớp (2)
Ví dụ: Lệnh import:
public class HelloNameDialog{ Sử dụng lệnh import để khai báo các package hoặc các
public static void main(String[] args){ lớp để khi sử dụng không cần nêu tên đầy đủ.
String result;
result = javax.swing.JOptionPane .showInputDialog
(“Hay nhap ten ban:”);
javax.swing.JOptionPane.showMessageDialog(null,
”Xin chao “+ result + “!”);
}
}
49 50
b. Các package trong Java (3) 5.2. Các lớp bao (Wrapper class)
Các package cơ bản trong Java Mỗi kiểu dữ liệu nguyên thủy có một lớp
java.math
tương ứng gọi là lớp bao:
java.sql
javax.swing
53 54
9
8/24/2011
55 56
Boolean Float
Boolean FALSE float MAX_VALUE
Boolean TRUE float MIN_VALUE
Byte float NaN
byte MIN_VALUE float NEGATIVE_INFINITY
byte MAX_VALUE float POSITIVE_INFINITY
Character Integer
int MAX_RADIX int MIN_VALUE
char MAX_VALUE int MAX_VALUE
int MIN_RADIX Long
char MIN_VALUE long MIN_VALUE
Unicode classification constants long MAX_VALUE
Double Short
double MAX_VALUE short MIN_VALUE
double MIN_VALUE short MAX_VALUE
double NaN
double NEGATIVE_INFINITY
double POSITIVE_INFINITY
57 58
59 60
10
8/24/2011
61 62
s2
oneString.equalsIgnoreCase(anotherString)
String s1 = new String(“Hello”);
String s2 = new String(“Hello”);
s2 Hello
63 64
d. Điểm đặc biệt của String String Literal vs. String Object
Khởi tạo String theo 2 cách: String s1 = "Hello";
dùng)
String s5 = new String("Hello");
Ví dụ:
String str1 = "Java is Hot";
65 66
11
8/24/2011
67 68
69 70
71 72
12
8/24/2011
73
13