Tải bản đầy đủ (.docx) (8 trang)

Lý thuyết Lập trình Hướng đối tượng - Võ Tấn Dũng (votandung) Class DongVat

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (27.35 KB, 8 trang )

namespace DongVat
{
abstract class DongVat
{
protected String ten;
protected float trongluong;
protected int sochan;
protected String mausac;
abstract public float DiChuyen();
abstract public float AnGi();
abstract public String Ten
{
get;
set;
}
public DongVat()
{
ten = "";
trongluong = 0;
sochan = 0;
mausac = "";
}
public DongVat(String name, float weight, int numberOfFoot, String
bodyColor)
{
ten = name;
trongluong = weight;
sochan = numberOfFoot;
mausac = bodyColor;
}
public override string ToString()


{
return ten + ";" + trongluong + "kg;" + sochan + "chan;mau than:" +
mausac;
}
}
abstract class Thu : DongVat
{
public Thu() : base() { }


public Thu(String name, float weight, int numberOfFoot, String
bodyColor) : base(name, weight, numberOfFoot, bodyColor) { }
abstract public String LoaiBanChan();
abstract public void NoiSong();
}
class ThuAnThit : Thu
{ public String cachSanMoi;
public ThuAnThit() : base() {}
public ThuAnThit(String name, float weight, int numberOfFoot, String
bodyColor)
: base(name, weight, numberOfFoot, bodyColor) { }
public override float DiChuyen()
{
Console.WriteLine("Chay nhanh");
float tocdochay = 50; //km/h
return tocdochay;
}
public override float AnGi()
{
Console.WriteLine("An thit");

float khoiluongthucan = 5; //kg/ngay
return khoiluongthucan;
}
public override string Ten
{
get
{
return ten + "(thu an thit)";
}
set
{
if (value != "") ten = value;
}
}
public override string LoaiBanChan()
{
return "Co mong vuot";
}
public override void NoiSong()
{
Console.WriteLine("Trong rung, noi hoang da");


}
public void SanMoi()
{
Console.Write("Cho biet cach san moi cua con {0} nay:", this.Ten);
this.cachSanMoi = Console.ReadLine();
}
public override string ToString()

{
return "[" + base.ToString() + ";Loai chan:" + LoaiBanChan() + "]";
}
}
class ThuAnCo : Thu
{
private bool coSungHayKhong;
public ThuAnCo() : base() { }
public ThuAnCo(String name, float weight, int numberOfFoot, String
bodyColor)
: base(name, weight, numberOfFoot, bodyColor) { }
public override string LoaiBanChan()
{
return "Co mong guoc";
}
public override void NoiSong()
{
Console.WriteLine("Trong rung thua, cao nguyen");
}
public override float DiChuyen()
{
Console.WriteLine("Chay rat nhanh");
float tocdochay = 70; //km/h
return tocdochay;
}
public override float AnGi()
{
Console.WriteLine("An co va la cay");
float khoiluongthucan = 10; //kg/ngay



return khoiluongthucan;
}
public override string Ten
{
get
{
return ten + "(thu an co)";
}
set
{
if (value != "") ten = value;
}
}
public override string ToString()
{
return "[" + base.ToString() + ";Loai chan:" + LoaiBanChan() + "]";
}
public bool CoSungHayKhong()
{
Console.Write("Cho biet con {0} nay co sung hay khong?\n(Nhap 1YES SoKhac-NO):", this.Ten);
int yesNO = int.Parse(Console.ReadLine());
if (yesNO == 1)
{
Console.WriteLine("Con {0} nay Co Sung.", this.Ten);
coSungHayKhong = true;
}
else
{
Console.WriteLine("Con {0} nay Khong Co Sung.", this.Ten);

coSungHayKhong = false;
}
return false;
}
}
class Chim : DongVat
{
private bool coBietHotKhong;
public Chim() : base() { }


public Chim(String name, float weight, int numberOfFoot, String
bodyColor)
: base(name, weight, numberOfFoot, bodyColor) { }
public override float DiChuyen()
{
Console.WriteLine("Bay, chay hoac leo treo");
float tocdodichuyen = 65; //km/h
return tocdodichuyen;
}
public override float AnGi()
{
Console.WriteLine("An hat, qua, sau bo");
float khoiluongthucan = 0.2f; //kg/ngay
return khoiluongthucan;
}
public override string Ten
{
get
{

return ten + "(chim)";
}
set
{
if (value != "") ten = value;
}
}
public override string ToString()
{
return "[" + base.ToString() + "]";
}
public bool BietHotKhong()
{
Console.Write("Cho biet con {0} nay co biet hot hay khong?\n(Nhap 1YES SoKhac-NO):", this.Ten);
int yesNO = int.Parse(Console.ReadLine());
if (yesNO == 1)
{
Console.WriteLine("Con {0} nay biet hot.", this.Ten);
coBietHotKhong = true;
}
else
{
Console.WriteLine("Con {0} nay khong biet hot.", this.Ten);
coBietHotKhong = false;


}
return false;
}
}

class BoSat : DongVat
{
private bool coChanHayKhong;
private bool thanCoVayHayKhong;
public BoSat() : base() { }
public BoSat(String name, float weight, int numberOfFoot, String
bodyColor)
: base(name, weight, numberOfFoot, bodyColor) { }
public override float DiChuyen()
{
Console.WriteLine("Truon, bo, leo treo");
float tocdodichuyen = 10; //km/h
return tocdodichuyen;
}
public override float AnGi()
{
Console.WriteLine("An sau bo, thu nho hon");
float khoiluongthucan = 0.3f; //kg/ngay
return khoiluongthucan;
}
public override string Ten
{
get
{
return ten + "(bo sat)";
}
set
{
if (value != "") ten = value;
}

}
public override string ToString()
{
return "[" + base.ToString() + "]";
}
public bool ThanCoVayHayKhong()
{
Console.Write("Cho biet than cua con {0} nay co vay hay
khong?\n(Nhap 1-YES SoKhac-NO):", this.Ten);
int yesNO = int.Parse(Console.ReadLine());


if (yesNO == 1)
{
Console.WriteLine("Con {0} nay than co vay.", this.Ten);
thanCoVayHayKhong = true;
}
else
{
Console.WriteLine("Con {0} nay than khong co vay.", this.Ten);
thanCoVayHayKhong = false;
}
return false;
}
public bool CoChanHayKhong()
{
Console.Write("Cho biet con {0} nay co chan hay khong?\n(Nhap 1YES SoKhac-NO):", this.Ten);
int yesNO = int.Parse(Console.ReadLine());
if (yesNO == 1)
{

Console.WriteLine("Con {0} nay co chan.", this.Ten);
coChanHayKhong = true;
}
else
{
Console.WriteLine("Con {0} nay khong co chan.", this.Ten);
coChanHayKhong = false;
}
return false;
}
}
class Program
{
static void Main(string[] args)
{
DongVat dv;
//dv = new DongVat(); //bao loi tai day vi DongVat la abstract class
//Thu d = new Thu(); //bao loi tai day vi Thu la lop abstract class
ThuAnThit dv1 = new ThuAnThit("Cop", 100, 4, "Vang Den");
Console.WriteLine(dv1);
float khoiluongthucan = dv1.AnGi();
Console.WriteLine("Mot ngay con {0} nay an het {1} kg thuc an.",
dv1.Ten, khoiluongthucan);


float tocdodichuyen = dv1.DiChuyen();
Console.WriteLine("Toc do di chuyen cua con {0} nay la {1} km/h",
dv1.Ten, tocdodichuyen);
dv1.SanMoi();
Console.WriteLine("--------------------------------");

ThuAnCo dv2 = new ThuAnCo("Nai", 120, 4, "Vang Sao");
Console.WriteLine(dv2);
khoiluongthucan = dv2.AnGi();
Console.WriteLine("Mot ngay con {0} nay an het {1} kg thuc an.",
dv2.Ten, khoiluongthucan);
tocdodichuyen = dv2.DiChuyen();
Console.WriteLine("Toc do di chuyen cua con {0} nay la {1} km/h",
dv2.Ten, tocdodichuyen);
dv2.CoSungHayKhong();
Console.WriteLine("--------------------------------");
Chim chim1 = new Chim("Ket", 0.5f, 2, "Xanh La");
Console.WriteLine(chim1);
khoiluongthucan = chim1.AnGi();
Console.WriteLine("Mot ngay con {0} nay an het {1} kg thuc an.",
chim1.Ten, khoiluongthucan);
tocdodichuyen = chim1.DiChuyen();
Console.WriteLine("Toc do di chuyen cua con {0} nay la {1} km/h",
chim1.Ten, tocdodichuyen);
chim1.BietHotKhong();
Console.WriteLine("--------------------------------");
BoSat bosat1 = new BoSat("Ran", 1.0f, 0, "Den");
Console.WriteLine(bosat1);
khoiluongthucan = bosat1.AnGi();
Console.WriteLine("Mot ngay con {0} nay an het {1} kg thuc an.",
bosat1.Ten, khoiluongthucan);
tocdodichuyen = bosat1.DiChuyen();
Console.WriteLine("Toc do di chuyen cua con {0} nay la {1} km/h",
bosat1.Ten, tocdodichuyen);
bosat1.CoChanHayKhong();
bosat1.ThanCoVayHayKhong();

Console.WriteLine("--------------------------------");
}
}
}



×