求帮助对C++的类的概念不是很理解 不会用(有一家饭馆,来了一个人吃肉
有一家饭馆,来了一个人吃肉(这个人又可能是汉族人,满族人,回族人等),肉(猪肉,牛肉,狗肉,腊狗肉,扣肉等),因为风俗习惯的不同,汉族人什么都吃
满族人不吃狗肉,其他都吃
回族人不吃猪肉,狗肉。其他都吃
这个人(如果是满族人)就想判断这个肉是否是他们符合他们的习惯。
肉;里加一个 湖南腊狗肉
湖南人 只吃 湖南腊狗肉
C++的语法忘得差不多了,只能用C#写:
程序代码:
/*----------------------------
有一家饭馆,来了一个人吃肉(这个人又可能是汉族人,满族人,回族人等),肉(猪肉,牛肉,狗肉,腊狗肉,扣肉等),因为风俗习惯的不同,
汉族人什么都吃
满族人不吃狗肉,其他都吃
回族人不吃猪肉,狗肉。其他都吃
这个人(如果是满族人)就想判断这个肉是否是他们符合他们的习惯。
肉里加一个 湖南腊狗肉
湖南人 只吃 湖南腊狗肉
----------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test1
{
class Program
{
// 肉类字典
protected static Dictionary<Int32, String> Meats = new Dictionary<Int32, String>()
{
{ 1, "猪肉" },
{ 2, "牛肉" },
{ 3, "狗肉" },
{ 4, "腊狗肉" },
{ 5, "扣肉" }
};
// 人类(抽象类)
protected abstract class Person
{
// 种族属性
private String _ethnicity = "";
public virtual String Ethnicity
{
get { return _ethnicity; }
}
// 地域属性
public String Province { get; set; }
// 禁忌
public abstract Boolean is_taboo(Int32 meatID);
};
// 汉族
protected class Han_Chinese : Person
{
public override String Ethnicity
{
get { return "汉族"; }
}
public override Boolean is_taboo(Int32 meatID)
{
return (Province != "湖南人") ? false : (meatID != 4);
}
}
// 满族
protected class ManChu_Chinese : Person
{
public override String Ethnicity
{
get { return "满族"; }
}
// 肉类禁忌:狗肉
private static Int32[] meat_taboo = { 3 };
public override Boolean is_taboo(Int32 meatID)
{
return meat_taboo.Contains(meatID);
}
}
// 回族
protected class Hui_Chinese : Person
{
public override String Ethnicity
{
get { return "回族"; }
}
// 肉类禁忌:猪肉、狗肉
private static Int32[] meat_taboo = { 1, 3 };
public override Boolean is_taboo(Int32 meatID)
{
return meat_taboo.Contains(meatID);
}
}
static void Main(string[] args)
{
Person[] persons = new Person[4]
{
new Hui_Chinese(),
new ManChu_Chinese(),
new Han_Chinese(),
new Han_Chinese() { Province = "湖南人" }
};
Int32 meatID = 1;
Console.WriteLine("提供{0}\n", Meats[meatID]);
foreach (Person p in persons)
{
Console.Write("[{0}] ", p.Ethnicity);
if (p.is_taboo(meatID))
{
Console.WriteLine("对不起,我是{0},不吃{1}。", String.IsNullOrEmpty(p.Province) ? p.Ethnicity : p.Province, Meats[meatID]);
}
else
{
Console.WriteLine("谢谢!");
}
}
Console.WriteLine("\n按Enter键结束程序...");
Console.ReadLine();
}
}
}
