1.定义一个抽象类Animal,其中包含一个抽象的只读属性Name和一个受包含字段_Name
答案:2 悬赏:0 手机版
解决时间 2021-02-20 09:10
- 提问者网友:泪痣哥哥
- 2021-02-19 23:18
1.定义一个抽象类Animal,其中包含一个抽象的只读属性Name和一个受包含字段_Name
最佳答案
- 五星知识达人网友:拜訪者
- 2021-02-20 00:46
public abstract class Animal
{
protected string _Name;
public abstract string Name
{
get;
}
public abstract void Show();
public void MakeVoice()
{
Console.WriteLine("{0}在叫",_Name);
}
}
public interface IAction
{
void Move();
}
public class Duck:Animal,IAction
{
public override string Name
{
get
{
return _Name;
}
}
public Duck(string name)//构造函数
{
_Name = name;
Console.WriteLine(_Name);
}
public override void Show() //重写
{
Console.WriteLine("{0}闪亮登场",Name);
}
public void Move() //接口的实现
{
Console.WriteLine("{0}快爬.", Name);
}
}
public class Dog : Animal, IAction
{
public override string Name
{
get
{
return _Name;
}
}
public Dog(string name)
{
_Name = name;
Console.WriteLine(_Name);
}
public override void Show()
{
Console.WriteLine("{0}闪亮登场", Name);
}
public void Move()
{
Console.WriteLine("{0}快爬", Name);
}
}
#endregion
class Program
{
static void Main(string[] args)
{
Duck duck = new Duck("鸭子");
duck.MakeVoice();
duck.Show();
duck.Move();
Dog dog = new Dog("小狗");
dog.MakeVoice();
dog.Show();
dog.Move();
IAction iaction = (IAction)duck;
iaction.Move();
IAction iaction2 = (IAction)dog;
iaction2.Move();
Console.ReadKey();
}
}
{
protected string _Name;
public abstract string Name
{
get;
}
public abstract void Show();
public void MakeVoice()
{
Console.WriteLine("{0}在叫",_Name);
}
}
public interface IAction
{
void Move();
}
public class Duck:Animal,IAction
{
public override string Name
{
get
{
return _Name;
}
}
public Duck(string name)//构造函数
{
_Name = name;
Console.WriteLine(_Name);
}
public override void Show() //重写
{
Console.WriteLine("{0}闪亮登场",Name);
}
public void Move() //接口的实现
{
Console.WriteLine("{0}快爬.", Name);
}
}
public class Dog : Animal, IAction
{
public override string Name
{
get
{
return _Name;
}
}
public Dog(string name)
{
_Name = name;
Console.WriteLine(_Name);
}
public override void Show()
{
Console.WriteLine("{0}闪亮登场", Name);
}
public void Move()
{
Console.WriteLine("{0}快爬", Name);
}
}
#endregion
class Program
{
static void Main(string[] args)
{
Duck duck = new Duck("鸭子");
duck.MakeVoice();
duck.Show();
duck.Move();
Dog dog = new Dog("小狗");
dog.MakeVoice();
dog.Show();
dog.Move();
IAction iaction = (IAction)duck;
iaction.Move();
IAction iaction2 = (IAction)dog;
iaction2.Move();
Console.ReadKey();
}
}
全部回答
- 1楼网友:纵马山川剑自提
- 2021-02-20 02:18
不好意思 俺们是学JAVA的
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯