Nội dung: - Truyền tham số - Đa năng hóa phương thức - Phương thức khởi tạo - Kế thừa. Các ví dụ. Bài 1 Truyền tham số
Nội dung: - Truyền tham số - Đa năng hóa phương thức - Phương thức khởi tạo - Kế thừa. Các ví dụ. Bài 1 Truyền tham số
Nội dung:
- Truyền tham số
- Đa năng hóa phương thức
- Phương thức khởi tạo
- Kế thừa.
Các ví dụ.
// Main method
int times=3;
nPrintln(“Welcome to Java!”,times);
while(n>0)
System.out.println(“n=”+n);
System.out.println(message);
n--;
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
if(num1>num2)
return num1;
else
return num2;
if(num1>num2)
return num1;
else
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
return num2;
import java.util.*;
public PhanSo() {
tu=0; mau=1;
tu=t; mau=m;
public PhanSo(int n) {
tu=n;mau=1;
int t=this.tu*x.mau+this.mau*x.tu;
int m=this.mau*x.mau;
try{
}catch(Exception e){
return null;
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
return (tu+"/"+mau);
return tu;
return tu*1.0/mau;
return mau;
PhanSo p=(PhanSo)o;
if(this.giaTri()<p.giaTri()) return 1;
else return 0;
try{
System.out.println(p.cong(q));
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
System.out.println("Xong.");
};
Bài 4 Kế thừa
class Point {
private double x, y;
this.x = x;
this.y = y;
double getX () {
return x;
double getY () {
return y;
}
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
class Square {
this.width = width;
double getWidth () {
return width;
// Truyền giá trị tham số width parameter vào lớp Square để khởi tạo đối tượng
super (width);
this.length = length;
double getLength () {
return length;
class Shapes {
Box () {
width = 0;
height = 0;
depth = 0;
this.width = width;
this.height = height;
this.depth = depth;
}//end of class
//SubBox khong can phai tao lai cac dac diem da co trong Box
//Tinh ke thua cho phep co the tao cac lop con rieng biet tu lop Box
int weight;
/* Cach 1
this.width = width;
this.height = height;
this.depth = depth;
this.weight = weight;
*/
/* Cach 2 */
this.weight = weight;
}//end of class
class JavaExample03 {
BÀI TẬP
2. Viết chương trình cộng, trừ 2 thời gian(giờ, phút, giây), sử dụng các phương thức khởi tạo.
- Họ tên
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
- Ngày sinh
- Lương
Luong=Lgcanban+(songaylam*100000)+trcap;
Luong=Lgcanban+(sosanpham*200000);