public abstract class Disk
{
protected double total, free;
public float Total //總容量
{
get { return total; }
set { total = value; }
}
public float Free //剩余容量
{
get { return free; }
}
public virtual void Write(float size)
{
if (free >= size)
free -= size;
else
Console.WriteLine("剩余空間不足!");
}
public virtual void Delete(float size)
{
if (free + size <= total)
free += size;
}
}
public class HardDisk
{
public HardDisk(float size)
{
this.total = this.free = size;
}
}
public class FlashDisk
{
public FlashDisk()
{
this.total = this.free = 64000;
}
}
public class CDROM
{
public override void Delete(float size)
{
Console.WriteLine("光碟不可洗掉!");
}
}
uj5u.com熱心網友回復:
public class HardDisk : Disk{
public HardDisk(float size)
{
this.total = this.free = size;
}
}
public class FlashDisk : Disk
{
public FlashDisk()
{
this.total = this.free = 64000;
}
}
public class CDROM : Disk
{
public override void Delete(float size)
{
Console.WriteLine("光碟不可洗掉!");
}
}
uj5u.com熱心網友回復:
你都說是派生類 HardDisk。那么為何沒有繼承關系?
uj5u.com熱心網友回復:
我沒把后面改對的復制過來,只有原先的,改對了后運行,報錯沒有主方法uj5u.com熱心網友回復:
主方法不知道怎么寫,我老想把這些寫進main里,還停留在c語言的結構上uj5u.com熱心網友回復:
你找到Program.cs的 Main函式。在里面呼叫就可以了
uj5u.com熱心網友回復:
public class Disk
{
private UInt64 m_TotalSize;
private UInt64 m_UsedSize;
private bool m_DeleteAble;
public UInt64 TotalSize
{
get{ return m_TotalSize; }
}
public UInt64 FreeSize
{
get{ return m_TotalSize - m_UsedSize; }
}
public UInt64 UsedSize
{
get{ return m_UsedSize; }
set
{
if(value > TotalSize)
{
Console.WriteLine("剩余空間不足!");
return;
}
m_UsedSize = value;
}
}
public bool DeleteAble
{
get{ return m_DeleteAble; }
set{ m_DeleteAble = value; }
}
public virtual bool Write(UInt64 size)
{
UInt64 curUsedSize = UsedSize;
UsedSize += size;
return curUsedSize != UsedSize;
}
public virtual bool Free(UInt64 size)
{
if( !DeleteAble )
{
Console.WriteLine("該磁盤不允許洗掉檔案!");
return false;
}
if(size > UsedSize)
{
Console.WriteLine("釋放空間不應該大于磁盤使用空間!");
return false;
}
UsedSize -= size;
return true;
}
public Disk(UInt64 size)
{
m_TotalSize = size;
m_UsedSize = 0;
m_DeleteAble = true;
}
}
public class HardDisk:Disk
{
public HardDisk(UInt64 size = 1024000):base(size)
{
}
}
public class FlashDisk:Disk
{
public FlashDisk(UInt64 size = 64000):base(size)
{
}
}
public class CDROM:Disk
{
public CDROM(UInt64 size = 640000):base(size)
{
DeleteAble = false;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/90651.html
標籤:C#
上一篇:小程式跳轉訪問Web頁面問題
