2011-06-30 8 views
7

私はコースのカテゴリにコース管理システムを作成する必要があります。複数の抽象クラスを派生しましたか?

Cooking, sewing and writing courses 

cookingwritingそれぞれが2コース(イタリア語、シーフード、創作の書き込みやビジネスの書き込みを)持っています。

abstract course -> abstract cooking -> concrete seafood 

抽象的な料理と執筆には共通のフィールドといくつかの一般的なメソッドがありますが、基本クラスの抽象メソッドもあります。

これはC#で行うことができますか?抽象クラスの抽象メソッドを抽象化すると、Visual Studioでは基本クラス抽象を隠してから、具体クラスメソッドには基本クラスが抽象クラスでなければならないというエラーがあります。私は答えを探しました。私は単一の継承がC#で使用されていることを知っていますが、継承はチェーンを運びます。最高の答えは何ですか?ここで

は、コードスニペットである - 私はそれが問題を明確に願っています:

public abstract class Course 
{ 
    public abstract void AddStudent(StudentName sn, int f); 
    public abstract decimal CalculateIncome(); 
} 

public abstract class WritingCourse : Course 
{ 
    public void AddStudent(StudentName sn, int f) 
    { 
    //Add student 
    } 
    public abstract decimal CalculateIncome(); // can only be claculated in concrete 
} 

public class BusinessWritCourse : WritingCourse 
{ 
    public void AddStudent(StudentName sn, int f): 
    base(sn, false){} 

    public decimal CalculateIncome() 
    { 
     return //do stuff 
    } 
} 

public class SewingCourse : Course 
{ 
    public override void AddStudent(StudentName sn, int f) 
    { 
     //do stuff 
    } 

    public override decimal CalculateIncome() 
    { 
     return //do stuff 
    } 
} 
+1

は、あなたが直面している問題にサンプルコードを投稿することができますか? –

答えて

0

私が正しく理解していれば、私はあなたがオーバーライドしたい抽象メソッドに「仮想」キーワードを使用したいと思いますか?

あなたが基本法上の仮想、「隠れが意図されていた場合は、新しいキーワードを追加し、いくつかの方法で継承されたメンバーを隠し」と継承方法に上書きのようなものが言うエラーについて話している場合に行います。

私は派生抽象クラスを作る場合
public abstract class BaseClass 
{ 
    public virtual void SomeMethod() 
    { 
    } 
} 


public abstract class InheritingClass : BaseClass 
{ 
    public override void SomeMethod() 
    { 
    } 
} 
+0

ここにコードスニペットがあります - それは明らかになります – Julesyw

7

は抽象Visual Studioは を言う 方法は、彼らはその後、具体的なクラスメソッドは、基本クラスが 抽象的でなければならないと言って エラーが持っている基本抽象クラスと を隠す(それがあるではない必要がありますレジスタ)

抽象メソッド 'b()'を持つ基本クラス 'A'をお持ちの場合、B:Aでは抽象として 'b()'を再度宣言する必要はありません。それを無効にして、それを使わないでください。あなたは、クラスBの「B()」メソッドをオーバーライドしている場合でも、あなたは再びクラス「C()」(でそれを無効にしても、ベースを使用することができます(); Bの実装を実行するために

いくつかのコード:。

public abstract class A{ 
    public abstract void b(); 
} 

public class B : A{ 
    public override void b() { //do stuff }; //overrides b from a 
    public virtual void c() { //do stuff }; //creates an implemented method c in B that can be overriden by childs. 
    public void d() { //do stuff}; 
} 

public class C : B{ 
    public override void b() { //do stuff}; //overrides b from A, works! 
    public override void c() {//do stuff}; //overrides c from B, works! 
    public override void d() {//do stuff}; //doesn't work since d from B isn't abstract or virtual (hides it) 
    public new void d() {//do stuff}; //works, hides d, but when you use inheritance this method will not be called, instead B's d() method will be called, only if you see C as the specific class C this will work 
} 

明示するには、abstract宣言されたメソッドをオーバーライドする必要があります(インターフェイスと同じように、抽象メソッドを宣言するクラスの直接の子のみがオーバーライドする必要があります)。virtual宣言されたメソッドはオーバーライドできますが、

+0

ちょっと - ありがとうございました... – Julesyw

+0

わかりますように、私は具体的なクラスA1と抽象A2 A2で私はそれを実装したくない - 私は....必要があります私はA2からBへの抽象メソッドを渡すことができません。 – Julesyw

4

私はこの種の問題は、抽象クラスではなくインターフェイスを使用して解決する方が良いと考えています。 例:

// 
interface IInterface1 
{ 
    void SameMethod(); 
} 

interface IInterface2 
{ 
    void SameMethod(); 
} 


class TestClass : IInterface1, IInterface2 
{ 
    void IInterface1.SameMethod() 
    { 
     // do one thing for Interface 1 
    } 

    void IInterface2.SameMethod() 
    { 
     // do something elsefor Interface 2 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     TestClass test = new TestClass(); 

     IInterface1 i1 = test; 
     i1.SameMethod(); // will call IInterface1.SameMethod() 

     IInterface2 i2 = test; 
     i2.SameMethod(); // will call IInterface2.SameMethod() 

    } 
} 
+0

私はインターフェイスを使用することを考えていましたが - ディスカッション掲示板の中で学生はそれらを言及し、講師はそこになかった実装を探すべきではない... – Julesyw

1
public class StudentName 
{ } 

public abstract class Course 
{ 
    public abstract void AddStudent(StudentName sn, int f); 
    public abstract decimal CalculateIncome(); 
} 

public abstract class WritingCourse : Course 
{ 
    override public void AddStudent(StudentName sn, int f) 
    { 
     //Add student 
    } 
    override public abstract decimal CalculateIncome(); // can only be claculated in concrete 
} 

public class BusinessWritCourse : WritingCourse 
{ 
    override public void AddStudent(StudentName sn, int f) 
    { base.AddStudent(sn, 0); } 

    override public decimal CalculateIncome() 
    { 
     return (decimal)3.4;//do stuff 
    } 
} 

public class SewingCourse : Course 
{ 
    public override void AddStudent(StudentName sn, int f) 
    { 
     //do stuff 
    } 

    public override decimal CalculateIncome() 
    { 
     return (decimal)10; //do stuff 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 

    } 
} 
関連する問題