Java Class và Object
Java Class và Object
Lớp (Class)
Cú pháp:
class TenLop {
// các thành phần của lớp
}Ví dụ:
public class Car {
// các thuộc tính của ô tô
String color;
int maxSpeed;
// phương thức khởi tạo
public Car(String color, int maxSpeed) {
this.color = color;
this.maxSpeed = maxSpeed;
}
// phương thức để hiển thị thông tin của ô tô
public void displayInfo() {
System.out.println("Color: " + color);
System.out.println("Max Speed: " + maxSpeed + " km/h");
}
}Đối tượng (Object)
Cú pháp:
Ví dụ:
Multiple Class và Multiple Object
Ví dụ:
Ví dụ với Multiple Object:
Bài tập thực hành
Last updated