using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Chinese c = new Chinese();
//Console.WriteLine(c.Speak());
//如果编译器提示无法将A类型隐式转换为B类型,就需要开发人员自己动手强制转换
//Chinese p = (Chinese)GetPerson();
Person p = GetPerson();
Console.WriteLine(p.Speak());
Console.ReadKey();
}
static Person GetPerson()
{
Person p = null;
string content= File.ReadAllText(@"c:\person.txt");//文件路径不能写死成绝对路径,而应该放到项目中。
if (content.ToUpper()=="CHINESE")
{
p = new Chinese();
}
else if (content.ToUpper() == "ENGLAND")
{
p = new England();
}
return p;
}
}
class Person
{
public virtual string Speak()
{
return "问候";
}
}
class Chinese : Person
{
public override string Speak()
{
return "吃了吗";
}
}
class England : Person
{
public override string Speak()
{
return "今天天气不错";
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Chinese c = new Chinese();
//Console.WriteLine(c.Speak());
//如果编译器提示无法将A类型隐式转换为B类型,就需要开发人员自己动手强制转换
//Chinese p = (Chinese)GetPerson();
Person p = GetPerson();
Console.WriteLine(p.Speak());
Console.ReadKey();
}
static Person GetPerson()
{
Person p = null;
string content= File.ReadAllText(@"c:\person.txt");//文件路径不能写死成绝对路径,而应该放到项目中。
if (content.ToUpper()=="CHINESE")
{
p = new Chinese();
}
else if (content.ToUpper() == "ENGLAND")
{
p = new England();
}
return p;
}
}
class Person
{
public virtual string Speak()
{
return "问候";
}
}
class Chinese : Person
{
public override string Speak()
{
return "吃了吗";
}
}
class England : Person
{
public override string Speak()
{
return "今天天气不错";
}
}
}