为什么这样编译会出错呢?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class pet
{
public pet(string name)
{
_name=name;
}
protected string _name;
public void nameprint()
{
Console.WriteLine("pet's name is " +_name);
}
virtual public void speak()
{
Console.WriteLine("宠物在说话:");
}//类的重写
}
public class dog:pet
{
public dog(string name): base(name)
{
}
new public void nameprint()
{
Console.WriteLine("宠物的名字是:" + _name);
}//对基类方法的隐藏
override public void speak()
{
Console.WriteLine(_name + "的叫声为:汪汪");
}//对基类方法的重写
}
class Program
{
static void Main(string[] args)
{
pet[] pets=new pet[]{new dog("ja"),new dog("lol")};
for(int i=0;i<pets.Length;i++)
{
pets[i].speak;
}
}
}
}