Bài tập Lập trình hướng đối tượng với C# (Thừa kế - Đa hình - Interface)
-
Saturday, June 22, 2013
No Comments
Class Diagram
Code:
namespace LopCa
{
public interface IBoi
{
void CachBoi();
}
public abstract class Ca:IBoi
{
//
private string TenCa;
private int CanNang;
private bool CoVay;
private string Mau;
//
public Ca(string TenCa,int CanNang,bool CoVay,string Mau)
{
this.TenCa = TenCa;
this.CanNang = CanNang;
this.CoVay = CoVay;
this.Mau = Mau;
}
//
public string TENCA
{
get { return TenCa; }
set { TenCa = value; }
}
public int CANNANG
{
get { return CanNang; }
set { CanNang = value; }
}
public bool COVAY
{
get { return CoVay; }
}
public string MAU
{
get { return Mau; }
set { Mau = value; }
}
//
public virtual void CachBoi()
{
Console.WriteLine("Ca nao chang biet boi !!!");
}
}
public class CaBayMau : Ca
{
public CaBayMau(string TenCa, int CanNang, bool CoVay, string Mau)
:base(TenCa, CanNang, CoVay, Mau)
{
Console.WriteLine("Ca Bay Mau");
Console.WriteLine("Ten : {0}",TenCa);
Console.WriteLine("Can nang : {0}",CanNang);
Console.WriteLine("La ca {0}",CoVay?"co vay":"khong co vay");
Console.WriteLine("Mau : {0}",Mau);
}
// IBoi
public override void CachBoi()
{
base.CachBoi();
Console.WriteLine("Boi loanh quanh tren mat nuoc !!!");
}
}
public interface IBo
{
void CachBo();
}
public class CaTre : Ca, IBo
{
//
public CaTre(string TenCa, int CanNang, bool CoVay, string Mau)
: base(TenCa, CanNang, CoVay, Mau)
{
Console.WriteLine("Ca Tre");
Console.WriteLine("Ten : {0}", TenCa);
Console.WriteLine("Can nang : {0}", CanNang);
Console.WriteLine("La ca {0}", CoVay ? "co vay" : "khong co vay");
Console.WriteLine("Mau : {0}", Mau);
}
// IBoi
public override void CachBoi()
{
base.CachBoi();
Console.WriteLine("Boi duoi day !!!");
}
// IBo
public void CachBo()
{
Console.WriteLine("Bo xa 2km!!!");
}
}
public interface IBay
{
void CachBay();
}
public class CaChuon : Ca, IBay
{
//
public CaChuon(string TenCa, int CanNang, bool CoVay, string Mau)
: base(TenCa, CanNang, CoVay, Mau)
{
Console.WriteLine("Ca Chuon");
Console.WriteLine("Ten : {0}", TenCa);
Console.WriteLine("Can nang : {0}", CanNang);
Console.WriteLine("La ca {0}", CoVay ? "co vay" : "khong co vay");
Console.WriteLine("Mau : {0}", Mau);
}
// IBoi
public override void CachBoi()
{
base.CachBoi();
Console.WriteLine("Boi nhanh duoi mat nuoc !!!");
}
// IBay
public void CachBay()
{
Console.WriteLine("Bay xa 100m !!!");
}
}
class Program
{
static void Main(string[] args)
{
//
CaBayMau cbm = new CaBayMau("Map", 10, true, "Colorful");
cbm.CachBoi();
//
CaTre ct = new CaTre("Om",2000,false,"black");
ct.CachBoi();
ct.CachBo();
//
CaChuon cc = new CaChuon("Vua", 500, true, "white");
cc.CachBoi();
cc.CachBay();
Console.ReadKey();
}
}
}
{
public interface IBoi
{
void CachBoi();
}
public abstract class Ca:IBoi
{
//
private string TenCa;
private int CanNang;
private bool CoVay;
private string Mau;
//
public Ca(string TenCa,int CanNang,bool CoVay,string Mau)
{
this.TenCa = TenCa;
this.CanNang = CanNang;
this.CoVay = CoVay;
this.Mau = Mau;
}
//
public string TENCA
{
get { return TenCa; }
set { TenCa = value; }
}
public int CANNANG
{
get { return CanNang; }
set { CanNang = value; }
}
public bool COVAY
{
get { return CoVay; }
}
public string MAU
{
get { return Mau; }
set { Mau = value; }
}
//
public virtual void CachBoi()
{
Console.WriteLine("Ca nao chang biet boi !!!");
}
}
public class CaBayMau : Ca
{
public CaBayMau(string TenCa, int CanNang, bool CoVay, string Mau)
:base(TenCa, CanNang, CoVay, Mau)
{
Console.WriteLine("Ca Bay Mau");
Console.WriteLine("Ten : {0}",TenCa);
Console.WriteLine("Can nang : {0}",CanNang);
Console.WriteLine("La ca {0}",CoVay?"co vay":"khong co vay");
Console.WriteLine("Mau : {0}",Mau);
}
// IBoi
public override void CachBoi()
{
base.CachBoi();
Console.WriteLine("Boi loanh quanh tren mat nuoc !!!");
}
}
public interface IBo
{
void CachBo();
}
public class CaTre : Ca, IBo
{
//
public CaTre(string TenCa, int CanNang, bool CoVay, string Mau)
: base(TenCa, CanNang, CoVay, Mau)
{
Console.WriteLine("Ca Tre");
Console.WriteLine("Ten : {0}", TenCa);
Console.WriteLine("Can nang : {0}", CanNang);
Console.WriteLine("La ca {0}", CoVay ? "co vay" : "khong co vay");
Console.WriteLine("Mau : {0}", Mau);
}
// IBoi
public override void CachBoi()
{
base.CachBoi();
Console.WriteLine("Boi duoi day !!!");
}
// IBo
public void CachBo()
{
Console.WriteLine("Bo xa 2km!!!");
}
}
public interface IBay
{
void CachBay();
}
public class CaChuon : Ca, IBay
{
//
public CaChuon(string TenCa, int CanNang, bool CoVay, string Mau)
: base(TenCa, CanNang, CoVay, Mau)
{
Console.WriteLine("Ca Chuon");
Console.WriteLine("Ten : {0}", TenCa);
Console.WriteLine("Can nang : {0}", CanNang);
Console.WriteLine("La ca {0}", CoVay ? "co vay" : "khong co vay");
Console.WriteLine("Mau : {0}", Mau);
}
// IBoi
public override void CachBoi()
{
base.CachBoi();
Console.WriteLine("Boi nhanh duoi mat nuoc !!!");
}
// IBay
public void CachBay()
{
Console.WriteLine("Bay xa 100m !!!");
}
}
class Program
{
static void Main(string[] args)
{
//
CaBayMau cbm = new CaBayMau("Map", 10, true, "Colorful");
cbm.CachBoi();
//
CaTre ct = new CaTre("Om",2000,false,"black");
ct.CachBoi();
ct.CachBo();
//
CaChuon cc = new CaChuon("Vua", 500, true, "white");
cc.CachBoi();
cc.CachBay();
Console.ReadKey();
}
}
}
st: Duy Phan