C#一般不明确地按类型转发

时间:2017-02-16 13:59:11

标签: c# typeof downcast gettype

你可以向我解释一下吗? 我如何解决下面的简单任务?

class Base{}
class Derived1: Base { void Method(); }
class Derived2: Base { void Method();}

static void Main(string[] args)
{   
    Base object; //it is just a declaring

    if (some condition is true)
        object = new Derived1();
    else
        object = new Derived2();

    //now i want to call one of methods of one of my derived classes
    //object.MyMethod(); //of course wrong, object has no that method

    //ok, i have to downcast it but i don't know which class to
    //((object.GetType())object).Method(); //wrong

    //is there only one way is to repeat conditions 
    //and to downcast explicitly?

    if (some condition is true again)
        (object as Derived1).Method();
    else
        (object as Derived2).Method();
}

基类当然对Method()一无所知。

5 个答案:

答案 0 :(得分:2)

我建议在两个派生的Base覆盖中使用抽象方法:

abstract class Base { public abstract void Method();}
class Derived1: Base { public override void Method(); }
class Derived2: Base { public override void Method(); }

然后

static void Main(string[] args) {   
  Base instance; 

  if (some condition is true)
    instance = new Derived1();
  else
    instance = new Derived2();

  instance.Method();
}

实施接口是另一种选择:

interface IBase {void Method();}

class Derived1: IBase { void Method(); }
class Derived2: IBase { void Method(); }

static void Main(string[] args) {   
  IBase instance; 

  if (some condition is true)
    instance = new Derived1();
  else
    instance = new Derived2();

  instance.Method();
}

答案 1 :(得分:1)

如果班级仅用于申报,则使用interface&没有任何具体方法:

interface Base{
void Method();
}

class Derived1: Base { void Method(){} }
class Derived2: Base { void Method(){}}

    static void Main(string[] args)
{   
    Base obj; //it is just a declaring

    if (some condition is true)
        obj = new Derived1();
    else
        obj = new Derived2();

 //Call it directly
        obj.Method();    
}

答案 2 :(得分:0)

您需要了解的是继承多态的用途。

<强>继承

允许相关对象派生自共同的祖先。

abstract class Shape
{
}

sealed class Rectangle : Shape
{
}

sealed class Circle : Shape
{
}

<强>多态性

允许派生类型具有不同的实现来执行常见行为。

abstract class Shape
{
    abstract double GetArea();
}

sealed class Rectangle : Shape
{
    override double GetArea() // same behaviour
    {
        return x * y; // different implementation
    }
}

sealed class Circle : Shape
{
    override double GetArea() // same behavior
    {
        return PI * r * r; // different implementation
    }
}

接口与抽象类

接口定义对象可以做什么。在下面的示例中,DogCat实现了IAudible接口,该接口公开了MakeNoise方法。请注意,IAudible不是对象,它可以做什么

class Dog : IAudible
{
    public void MakeNoise()
    {
        Console.WriteLine("Woof");
    }
}

class Cat : IAudible
{
    public void MakeNoise()
    {
        Console.WriteLine("Meow")
    }
}

IAudible d = new Dog();
d.MakeNoise(); // Woof

IAudible c = new Cat();
c.MakeNoise(); // Meow

抽象类定义对象是什么。在下面的示例中,DogCat扩展Animal,因为狗和猫是动物

abstract class Animal
{
    public string Noise { get; protected set; }
}

sealed class Dog : Animal
{
    public Dog()
    {
        Noise = "Woof";
    }
}

sealed class Cat : Animal
{
    public Cat()
    {
        Noise = "Meow";
    }
}

Animal d = new Dog();
d.Noise; // Woof;

Animal c = new Cat();
c.Noise; // Meow

要回答您的问题,请确定对象的关联方式和原因。如果它们与他们能做的事情有关,那么使用界面。如果它们与它们相关,则使用抽象类。

答案 3 :(得分:0)

同意每个答案。谢谢大家。

但是还有一个..crutch ..通过使用委托

class Base
{
    public delegate string Method();
    public Method m;
}
class Derived1 : Base
{
    public string ParticularMethod()
    {
        return "Particular method of derived 1";
    }
}
class Derived2 : Base
{
    public string ParticularMethod()
    {
        return "Particular method of derived 2";
    }
}

class Program
{
    static void Main(string[] args)
    {
        Base obj; //it is just a declaring

        if (true)
        {
            obj = new Derived1();
            obj.m = (obj as Derived1).ParticularMethod;
        }
        else
        {
            obj = new Derived2();
            obj.m = (obj as Derived2).ParticularMethod;
        }

        Console.WriteLine(obj.m());
        //"Particular method of derived 1"
        Console.ReadKey();

    }
}

答案 4 :(得分:0)

或者

cassandra@cqlsh:demo> CREATE TABLE int_test (id int, name text, primary key(id));
cassandra@cqlsh:demo> SELECT * FROM int_test;

 id | name
----+------

(0 rows)
cassandra@cqlsh:demo> INSERT INTO int_test (id, name) VALUES ( 215478936541111, 'abc');
cassandra@cqlsh:demo> SELECT * FROM int_test ;

 id                  | name
---------------------+--------- 
     215478936541111 |  abc

(1 rows)
cassandra@cqlsh:demo> ALTER TABLE demo.int_test ALTER id TYPE varint;
cassandra@cqlsh:demo> INSERT INTO int_test (id, name) VALUES ( 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999, 'abcd');
cassandra@cqlsh:demo> SELECT * FROM int_test ;

 id                                                                                                                           | name
------------------------------------------------------------------------------------------------------------------------------+---------
                                                                                                              215478936541111 |  abc                                                                                                     
 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 | abcd

(2 rows)
cassandra@cqlsh:demo>

它也有效