转换或转换:为基类对象分配派生类引用 [英] Cast or conversion: assign a derived class reference to base class object

查看:130
本文介绍了转换或转换:为基类对象分配派生类引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类:

  abstract   class  CounterBase 
{
public abstract void Increment();
public abstract void 减量();
}



然后我实现它。

  class 计数器:CounterBase 
{
public int Count { get ; private set ; }
public 覆盖 void 增量()
{
Count ++;
}
public 覆盖 void 递减()
{
Count--;
}
}



在我的主课程中我有一个静态方法:

 静态  void  TestCounter(CounterBase c)
{
< span class =code-keyword> for ( int i = 0 ; i < span class =code-keyword>< 1000000 ; i ++)
{
c.Increment();
c.Decrement();
}
}



在main方法中,我用代码调用它。

< pre lang =c#> Console.WriteLine( 错误的计数器);
var c = new Counter();
var t1 = new 线程(()= > TestCounter(c));



您看到我将派生类实例传递给线程方法。但是在 TestCounter 签名中,我传递了基类实例。



这是一个好的写作吗?(有没有编译错误)那是什么样的标准术语?(显式或隐式转换)

解决方案

解决方案1的解释是正确的。除此之外:



正确的术语是多态性。根本没有铸造,也没有隐式铸造这样的东西。我猜你把它与隐式转换混合在一起。



Jon Skeet:

多态性允许表达某种契约,可能有许多类型以不同的方式实现该契约(无论是否通过类继承),每种类型都根据自己的目的。使用该合同的代码不应该(*)关心涉及哪个实现,只需要遵守合同。



(*)在理想情况下,无论如何 - 很明显,调用代码经常非常谨慎地选择适当的实现!

你的案例中的契约是抽象基类。


这是完全可以接受的。通常,这意味着您可以将任何派生自CounterBase的类传递给TestCounter函数,因为您的TestFunction只关心递增和递减。如果您使用



 static void TestCounter(Counter c)





那么你只能通过你的Counter类。如果你创建了一个不同的计数器



  class  BaseTenCounter:CounterBase 
{
public int 计数{获取; private set ; }
public 覆盖 void 增量()
{
计数+ = 10 ;
}
public 覆盖 void 递减()
{
计数 - = 10 ;
}
}





那么你可以将Counter或BaseTenCounter传递给你的TestCounter函数,但是如果你的TestCounter接受Counter作为参数,你无法将TestCounter传递给它。



不确定这是否属于隐式\ explicit铸造术语。


I have a abstract class:

abstract class CounterBase
{
   public abstract void Increment();
   public abstract void Decrement();
}


Then I implement it.

class Counter:CounterBase
{
     public int Count { get; private set; }
     public override void Increment()
     {
         Count++;
     }
     public override void Decrement()
     {
         Count--;
     }
}


In my main class I have a static method:

static void TestCounter(CounterBase c)
{
    for (int i = 0; i < 1000000; i++)
    {
        c.Increment();
        c.Decrement();
    }
}


In the main method, I call it with the code.

Console.WriteLine("Incorrect counter");
var c = new Counter();
var t1 = new Thread(() => TestCounter(c));


You see I pass the derived class instance to the thread method. However in the TestCounter signature I passed the base class instance.

Is it a good writing?(There is no compiling error) What kind of standard term for that?(Explicit or implicit cast)

解决方案

The explanation of Solution 1 is correct. Adding to that:

The correct term for why this is working is polymorphism. It's no casting at all and there is no such thing as implicit casting. I guess you mixed that up with implicit conversion.

Jon Skeet:

Polymorphism allows the expression of some sort of contract, with potentially many types implementing that contract (whether through class inheritance or not) in different ways, each according to their own purpose. Code using that contract should not(*) have to care about which implementation is involved, only that the contract will be obeyed.

(*) In the ideal case, anyway - obviously quite often the calling code has chosen the appropriate implementation very deliberately!

The contract in your case being the abstract base class.


That is perfectly acceptable to do and quite common, it means that you can pass any class that derives from CounterBase to your TestCounter function as your TestFunction only cares about incrementing and decrementing. If you used

static void TestCounter(Counter c)



then you could only pass your Counter class. If you created a different counter

class BaseTenCounter:CounterBase
{
     public int Count { get; private set; }
     public override void Increment()
     {
         Count+=10;
     }
     public override void Decrement()
     {
         Count-=10;
     }
}



then you could pass either Counter or BaseTenCounter to your TestCounter function, but if your TestCounter accepted "Counter" as an argument you couldn't pass TestCounter to it.

Not sure if this would come under the term of implicit\explicit casting though.


这篇关于转换或转换:为基类对象分配派生类引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆