短い回答:それは依存しています。
タイプA
のオブジェクトが必要な場合は、コンストラクタA()
を使用します。タイプB
のオブジェクトが必要な場合は、コンストラクタB()
を使用します。
さらに具体的にするには、ここでは動物クラスがあります。
public class Giraffe : Animal {}
public class Animal {}
次に、あなたのコードは、クラスのフィールドまたはプロパティを露出している場合を除き、多くのすべてのことを意味するものではありません
Animal a = new Giraffe(); // perfectly normal, imo
//Giraffe b = new Animal(); compiler error!
Animal a = new Giraffe() as Animal; // "as Animal" is redundant
Giraffe b = new Animal() as Giraffe; // this is strange, but compiler is OK with it... not sure of a use case for this exact piece of code.
変数の型になります。この場合、オブジェクトが外部者にどのように見えるかを考慮してください。外側にタイプAnimal
のオブジェクトとしてフィールド/プロパティを表示するには、それを使用します。タイプGiraffe
と同じです。
あなたは正確に何をしようとしていますか? –
@ AD.Net:これは模範試験の質問ですが、私はそれを確かめるために準備をしています。 :D –
@dbugger:1と同じですか?彼らは正しいのですか? –