Pipi大魔王
2017-09-29 16:26
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;
}
}
}
}
public class dog:pet
{
public dog(string name): base(name)
{
pets[i].speak();
最后一行是调用函数,要加括号
C#面向对象编程
68857 学习 · 153 问题
相似问题