有两批不同的文件要处理,所以要基于上面的情况,做两个类。
想定义一个只有 get 方法的 property,好让两个类的代码看起来"像一些",可是如果是 interface 就必须是
public,如果是基类就必须有个函数体。
还有一个方法,也是只要 private 就好了,可是就是不能放 interface 里头。其实是想搞一个单例,可是 interface
不支持 static,不支持就算了。
据说可以用 attribute 来做么
怎么办呢
update:
理解错了的事情: CurrentFile 和 LatestFile 对于 listfile 来讲是 singleton,但是对于 advfile 就不是,每个 advfile 有自己的最新的文件和存档。
为了使用一致的接口,还是应用 MyFile 基类处理 CurrentFile (存档),用 IArchivable 接口区分 LatestFile 的方法。不在基类里使用任何 static,唯一使用 static 的地方是 listfile 的 LatestListFile (隐藏掉构造函数)。
在 vim 中编译的命令是 :!mcs %
abstract class MyFile: System.IDisposable { public MyFile CurrentFile{ get{ if(m_current == null) { LoadCurrentFile(); } return m_current; } } public virtual void Dispose() { // do nothing } protected abstract void LoadCurrentFile(); // TODO: override native compare functions public bool EqualTo(MyFile file) { return TestEqual(file); } protected abstract bool TestEqual(MyFile file); protected MyFile m_current; }; interface IArchivable { void RotateArchive(); } class ListFile: MyFile, IArchivable { public static ListFile LatestListFile { get{ if(m_latest == null) { m_latest = new ListFile(); } return m_latest; } } static ListFile m_latest; ListFile(){} protected override void LoadCurrentFile() { m_current = this; } protected override bool TestEqual(MyFile file) { using(file as ListFile) { // do nothing return false; } } public void RotateArchive() { // do nothing } }; class ADVFile: MyFile, IArchivable { protected override void LoadCurrentFile() { m_current = this; } protected override bool TestEqual(MyFile file) { // TODO: move this to base and use reflection to get this.Type using(file as ListFile) { // do nothing return false; } } public void RotateArchive() { // do nothing } }; class EntryClass { public static void Main() { ListFile f1 = ListFile.LatestListFile; MyFile f2 = f1.CurrentFile; if(!f2.EqualTo((MyFile)f1)) { f1.RotateArchive(); } ADVFile f3 = new ADVFile(); MyFile f4 = f3.CurrentFile; if(!f4.EqualTo((MyFile)f3)) { f3.RotateArchive(); } } }