2014-03-12 21 views
2

我有一個基類,它有一些抽象方法,並有21個類從這個基類繼承。現在對於那些抽象方法之一,我希望用21個類中的6個的通用實現來實現它,所以我想創建另一個可以實現這一點的基類。您可以用另一個基類重寫基類中的某個抽象方法嗎?

我願意接受建議,但我創建當前基類和21類之間的另一個基類的主要目的是從重複相同的代碼在21班的6,如果我沒有繼續。

這裏是一個代碼示例來說明情況:

public abstract class FooBase 
{ 
    public abstract string Bar(); 
    public abstract string SomeMethod(); 
    public virtual string OtherMethod() 
    { 
     return this.SomeMethod(); 
    } 
} 

public abstract class AnotherBase : FooBase 
{ 
    public abstract string Bar(); 
    public abstract string SomeMethod(); 
    public override OtherMethod() 
    { 
     //this is the common method used by 6 of the classes 
     return "special string for the 6 classes"; 
    } 
} 

public class Foo1 : FooBase 
{ 
    public override string Bar() 
    { 
     //do something specific for the Foo1 class here 
     return "Foo1 special string"; 
    } 
    public override string SomeMethod() 
    { 
     //do something specific for the Foo1 class here 
     return "Foo1 special string"; 
    } 
} 

public class Another2 : AnotherBase 
{ 
    public override string Bar() 
    { 
     //do something specific for the Another2 class here 
     return "Another special string"; 
    } 
    public override string SomeMethod() 
    { 
     //do something specific for the Another2 class here 
     return "Another2 special string"; 
    } 
} 
+2

你的想法似乎是正確的。你試過了嗎?你遇到過任何問題嗎? – Benesh

回答

1

是的,你可以從另一個抽象類

public abstract class FooBase 
{ 
    //Base class content 
} 

public abstract class AnotherBase : FooBase 
{ 
    //it is "optional" to make the definition of the abstract methods of the Parent class in here 
} 

派生一個抽象類,當我們說這是可選定義父類的抽象方法的子類裏面,它是強制性的,子類應該是抽象的

public abstract class FooBase 
{ 
    public abstract string Bar(); 
    public abstract string SomeMethod(); 
    public abstract string OtherMethod(); 
} 

public abstract class AnotherBase : FooBase 
{ 
    public override string OtherMethod() 
    { 
     //common method that you wanted to use for 6 of your classes 
     return "special string for the 6 classes"; 
    } 
} 

//child class that inherits FooBase where none of the method is defined 
public class Foo1 : FooBase 
{ 
    public override string Bar() 
    { 
     //definition 
    } 
    public override string SomeMethod() 
    { 
     //definition 
    } 
    public override string OtherMethod() 
    { 
     //definition 
    } 
} 

//child class that inherits AnotheBase that defines OtherMethod 
public class Another2 : AnotherBase 
{ 
    public override string Bar() 
    { 
     //definition 
    } 
    public override string SomeMethod() 
    { 
     //definition 
    } 
} 

所以我猜會有5個類,如Another2AnotherBase繼承,將有一個共同的定義OtherMethod

+1

謝謝,[jerriclynsjohn](http://stackoverflow.com/users/3407838/jerriclynsjohn),這工作! – Paul

0

是的,這是完全可能的,並經常做。沒有規則說你可以在你的類層次結構的最底層只有一個基類;該類的子類可以一樣好是抽象的,並由此成爲(有點更專門的)基類爲一組的間接從您的一般基類派生的類。

0

應指定究竟這些類的事情,但..給你提供的信息:

這是確切的問題,即Strategy pattern旨在解決,如在Head First設計模式給出的例子書。

您有一個摘要Duck類,其他鴨子(例如,RedheadDuckMallardDuck)從中導出。 Duck類有一個Quack方法,它只是在屏幕上顯示字符串「嘎嘎」。

現在告訴您要添加一個RubberDuck。這傢伙不嘎嘎!所以你會怎麼做?讓Quack抽象並讓子類決定如何實現這個?不,這會導致重複的代碼。

相反,你定義一個IQuackBehaviour接口與Quack方法。從那裏,你得到兩個類,QuackBehaviourSqueakBehaviour

public class SqueakBehaviour: IQuackBehaviour 
{ 
    public void Quack(){ 
     Console.WriteLine("squeak"); 
    } 
} 

public class QuackBehaviour: IQuackBehaviour 
{ 
    public void Quack(){ 
     Console.WriteLine("quack"); 
    } 
} 

現在,你撰寫的鴨子這種行爲酌情:

public class MallardDuck : Duck 
{ 
    private IQuackBehaviour quackBehaviour = new QuackBehaviour(); 

    public override void Quack() 
    { 
     quackBehaviour.Quack(); 
    } 
} 

public class RubberDuck : Duck 
{ 
    private IQuackBehaviour quackBehaviour = new SqueakBehaviour(); 

    public override void Quack() 
    { 
     quackBehaviour.Quack(); 
    } 
} 

您可以通過屬性,如果你想鴨子在運行時改變自己的行爲,即使注入的IQuackBehaviour實例。

相關問題