为什么我原来的构造方法没有了
dog转换成cat之后,cat没有继承dog的名字(构造方法),名字变成了空
dog转换成cat之后,cat没有继承dog的名字(构造方法),名字变成了空
2018-11-04
using System;
namespace 装箱与拆箱
{ abstract public class pet
{
public string name;
public pet(string name)
{
Console.WriteLine("我叫" + name);
}
abstract public void speak();
}
public class Dog : pet
{
public Dog(string name):base(name)
{
}
public override void speak()
{
Console.WriteLine("wow");
}
public static explicit operator Cat(Dog dog)
{
return new Cat(dog.name);
}
}
public class Cat : pet
{
public Cat(string name) : base(name)
{
}
public override void speak()
{
Console.WriteLine("meow");
}
}
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog("hommy");
dog.speak();
Cat cat = (Cat)dog;
cat.speak();
}
}
}
举报