Hiện tại VS đang mò lại hàm kế thừa nhằm để hiểu rõ hơn về thuật toán này. Nói thì khó hình dung lắm nhưng VS sẽ minh hoạ bằng 1 bài ví dụ sau đây.
import java.lang.*;
import java.util.*;
class HinhVuong
{
private float canh;
public static float getFloat (String td)
{
Scanner scan = new Scanner(System.in);
System.out.println(td);
float f = scan.nextFloat();
while (f<=0)
{
System.out.println("Nhap sai yeu cau nhap lai: ");
f = scan.nextFloat();
}
return f;
}
public HinhVuong(){}
public HinhVuong (String td)
{
System.out.println(td);
canh= getFloat("Nhap canh: ");
}
public HinhVuong(float x)
{
canh=x;
}
public float getCanh()
{
return canh;
}
public void setCanh(float tam)
{
canh = tam;
}
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
-------------------------------------------------------------------
class HinhChuNhat extends HinhVuong
{
private float cd;
public HinhChuNhat()
{
super(HinhVuong.getFloat("Nhap chieu rong: "));
cd = HinhVuong.getFloat("Nhap chieu dai: ");
}
public float getChieuDai()
{
return cd;
}
public void setChieuDai(float t)
{
cd = t;
}
public HinhChuNhat (float crr, float cdd)
{
super(crr);
cd=cdd;
}
public float tinhDT()
{
return cd*getCanh();
}
public float tinhCV()
{
return (cd+getCanh())*2;
}
public String toString()
{
return "Chu vi: = " + tinhCV() + "\nDien tich =: " + tinhDT();
}
public static void main(String[] args)
{
HinhChuNhat a = new HinhChuNhat();
System.out.println(a.cd+"\t"+a.getCanh());
a.tinhDT();
a.tinhCV();
System.out.println(a);
}
}
--------------------------------------------
class HinhTamGiac extends HinhChuNhat
{
private float c;
public HinhTamGiac()
{
super(HinhVuong.getCanh("Nhap canh a: "), HinhChuNhat.cd("Nhap canh b: "));
c=HinhVuong.getFloat("Nhap canh c: ");
}
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
VS muốn lấy Class HinhTamGiac kế thừa từ HinhChuNhat (mà HinhChuNhat kế thừa HinhVuong)... đang gặp rắc rối chổ này