2016-05-27 5 views
0

明らかにバーチャルとオーバーライドを使用するのは通常の状況ですが、このテレコムのサンプルカウントはありますか?バーチャルキーワードを使用しない多態

public class Pipe 
{ 
    // whole bunch of protected member variables such as bandwidth, latency, download limit 
    // etc, 

    public int GetCost() 
    { 
    // work out cost based on above 
    } 
} 

public class BigFatPipe : Pipe 
{ 
    public BigFatPipe() 
    { 
    // sets up the member variables one way 
    } 
} 

public class CheapestPossiblePipe: Pipe 
{ 
    public CheapestPossiblePipe() 
    { 
    // sets up the member variables another way 
    } 
} 

その後、あなたは次の2つの異なる答えを得るでしょう

PrintPrice(new BigFatPipe()) 

PrintPrice(new CheapestPossiblePipe()) 

public void PrintPrice(Pipe pipe) 
{ 
    int a = pipe.GetCost(); 
    .... 
} 

を呼ぶかもしれません。これは最も有用な例ではありませんが、数えられますか?

+0

ですか?あなたはあなたの質問に関連するタグだけを使うべきです。 – Nasreddine

+2

多態性は、オブジェクトが多くの形を取る能力です。メソッド自体をオーバーライドする必要はありません。 –

+0

それはクロス言語であり、私はJavaと同様に他の言語を含む可能性があります。多形性は私が大いに注目すると思ったタグではなかった – tony

答えて

0

このpost hereは、正確に多型性が何であるかについての有用な議論を有する。

ほとんどの定義では、オブジェクトがポリモフィックであるために仮想関数を持っていなければならないと明示的には言わないと思います。

0

コンストラクタのオーバーロードは、静的多型を実装するための認識されたメソッドです。これは実際にコンストラクタオーバーロードではありませんが、それは近いです。だから、私はそれを多型と呼んでいます。

-1

私があなただったら、私はアプローチをfolowing使用します。

public interface IGetCost 
{ 
    int GetCost(); 
} 

public class Pipe : IGetCost 
{ 
    public int GetCost(){} 
} 

public class BigFatPipe : IGetCost 
{ 
    //aggregation 
    private readonly Pipe _pipe; 

    public BigFatPipe(Pipe pipe) 
    { 
     _pipe = pipe; 
    } 
    public int GetCost() { } 
} 

public class CheapestPossiblePipe : IGetCost 
{ 
    private readonly Pipe _pipe; 

    public CheapestPossiblePipe(Pipe pipe) 
    { 
     _pipe = pipe; 
    } 
    public int GetCost() { } 
} 

public static void PrintPrice(IGetCost obj) 
{ 
    int cost = obj.GetCost(); 
    Console.WriteLine(cost); 
} 

static void Main(string[] args) 
{ 
    IGetCost p; 

    p = new Pipe(); 
    PrintPrice(p); 

    p = new BigFatPipe(); 
    PrintPrice(p); 

    p = new CheapestPossiblePipe(); 
    PrintPrice(p); 
} 

私は、2つの異なるものがそこにいることを言う必要がある - 多型と

多型の過負荷

public class foo 
{ 
    public virtual void foo1{/*....*/} 
} 

public class fooA : foo 
{ 
    public override void foo1{/*....*/} 
} 

public class fooB : foo 
{ 
    public new void foo1{/*....*/} 
} 

public class fooC : foo 
{ 
    //new is the default modifier 
    public void foo1{/*....*/} 
} 

をオーバーロード

public class foo{ 
    public int foo1{/*....*/} 
    public int foo1(int a){/*....*/} 
    public int foo1(string a){/*....*/} 
    public int foo1(int a, string b){/*....*/} 
} 
0

このパターンはうまくいきますが、たくさんのクラスを導入すると、ユーザーは無用に混乱します。クラスが何を異なるのか不思議に思います。

いくつかの工場法は、同じ仕事をし、理解し、維持するために容易になります:それはC++質問やC#1

public class Pipe 
{ 
    // whole bunch of private member variables such as bandwidth, latency, download limit 
    // etc, 

    public int GetCost() 
    { 
    // work out cost based on above 
    } 

    public static Pipe MakeBigFatPipe() 
    { 
     var result = new Pipe(); 
     // sets up the member variables one way 
     return result; 
    } 

    public static Pipe MakeCheapestPossiblePipe() 
    { 
     var result = new Pipe(); 
     // sets up the member variables another way 
     return result; 
    } 
} 
関連する問題