//成人继承自居民,多包含学历、职业两项数据;官员则继承自成人,多包含党派、职务两项数据。
//要求每个类中都提供数据输入输出的功能。
class resident
{
string name,id,birthDate;
public string Name
{
get { return name; }
set { name = value; }
}
public string ID
{
get { return id; }
set { id = value; }
}
public string DOB
{
get { return birthDate; }
set { birthDate = value; }
}
public virtual bool IO()
{
try
{
Console.WriteLine("Please input name");
string _name = Console.ReadLine();
Console.WriteLine("Please input id");
string _id = Console.ReadLine();
Console.WriteLine("Please input DOB format as yy/mm/dd");
string s_dob = Console.ReadLine();
DateTime dt_dob;
if (DateTime.TryParse(s_dob,out dt_dob))
{
Console.WriteLine("Name:{0}", _name);
Console.WriteLine("Id:{0}", _id);
Console.WriteLine("DOB:{0}", s_dob);
}
else
throw new Exception();
}
catch
{
return false;
}
return true;
}
}
class adult : resident
{
string studyExp, career;
public string StudyExp
{
get { return studyExp; }
set { studyExp = value; }
}
public string Career
{
get { return career; }
set { career = value; }
}
public override bool IO()
{
try
{
Console.WriteLine("Please input name");
string _name = Console.ReadLine();
Console.WriteLine("Please input id");
string _id = Console.ReadLine();
Console.WriteLine("Please input DOB format as yy/mm/dd");
string s_dob = Console.ReadLine();
Console.WriteLine("Please input study experience");
string _StudyExp = Console.ReadLine();
Console.WriteLine("Please input Career");
string _career = Console.ReadLine();
DateTime dt_dob;
if (DateTime.TryParse(s_dob, out dt_dob))
{
Console.WriteLine("Name:{0}", _name);
Console.WriteLine("Id:{0}", _id);
Console.WriteLine("DOB:{0}", s_dob);
Console.WriteLine("StudyExp:{0}", _StudyExp);
Console.WriteLine("Carrer:{0}", _career);
}
else
throw new Exception();
}
catch
{
return false;
}
return true;
}
}
class governer : resident
{
string party, position;
public string Party
{
get { return party; }
set { party = value; }
}
public string Position
{
get { return position; }
set { position = value; }
}
public override bool IO()
{
try
{
Console.WriteLine("Please input name");
string _name = Console.ReadLine();
Console.WriteLine("Please input id");
string _id = Console.ReadLine();
Console.WriteLine("Please input DOB format as yy/mm/dd");
string s_dob = Console.ReadLine();
Console.WriteLine("Please input study experience");
string _StudyExp = Console.ReadLine();
Console.WriteLine("Please input Career");
string _career = Console.ReadLine();
Console.WriteLine("Please input party");
string _party = Console.ReadLine();
Console.WriteLine("Please input position");
string _position = Console.ReadLine();
DateTime dt_dob;
if (DateTime.TryParse(s_dob, out dt_dob))
{
Console.WriteLine("Name:{0}", _name);
Console.WriteLine("Id:{0}", _id);
Console.WriteLine("DOB:{0}", s_dob);
Console.WriteLine("StudyExp:{0}", _StudyExp);
Console.WriteLine("Carrer:{0}", _career);
Console.WriteLine("Party:{0}", _party);
Console.WriteLine("Position:{0}", _position);
}
else
throw new Exception();
}
catch
{
return false;
}
return true;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Create a resident or an adult or a governer:");
string type=Console.ReadLine();
bool result;
switch (type)
{
case "adult":
adult myAdult = new adult();
result = myAdult.IO();
break;
case "governer":
governer myGoverner = new governer();
result = myGoverner.IO();
break;
case "resident":
resident myResident = new resident();
result = myResident.IO();
break;
default:
result = true;
Console.WriteLine("you didn't create any instance, the program will end.");
break;
}
if (!result)
{
Console.WriteLine("Your input is invalid,the program will end.");
}
}
}
要求每个类中都提供数据输入输出的功能,我理解对了吗?那么有没有关于方法的继承?我知道用super.method,不过就我目前的代码好像很难实现:P
还请各位不吝赐教。
9 个解决方案
#1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~帮你顶吧。。。。
#2
public override IO(....
{
base.IO(); //这样可以吗?不是太了解你意思
}
{
base.IO(); //这样可以吗?不是太了解你意思
}
#3
支持下~~~~~~
#4
代码改良了,谢谢各位回帖的朋友,又学到了新东西base.methord()继承方法
//建立三个类:居民、成人、官员。居民包含身份证号、姓名、出生日期;
//成人继承自居民,多包含学历、职业两项数据;官员则继承自成人,多包含党派、职务两项数据。
//要求每个类中都提供数据输入输出的功能。
class resident
{
string name,id,birthDate;
public string Name
{
get { return name; }
set { name = value; }
}
public string ID
{
get { return id; }
set { id = value; }
}
public string DOB
{
get { return birthDate; }
set { birthDate = value; }
}
public virtual bool IO()
{
try
{
Console.WriteLine("Please input name");
string _name = Console.ReadLine();
Console.WriteLine("Please input id");
string _id = Console.ReadLine();
Console.WriteLine("Please input DOB format as yy/mm/dd");
string s_dob = Console.ReadLine();
DateTime dt_dob;
if (DateTime.TryParse(s_dob,out dt_dob))
{
Console.WriteLine("Name:{0}", _name);
Console.WriteLine("Id:{0}", _id);
Console.WriteLine("DOB:{0}", s_dob);
}
else
throw new Exception();
}
catch
{
return false;
}
return true;
}
}
class adult : resident
{
string studyExp, career;
public string StudyExp
{
get { return studyExp; }
set { studyExp = value; }
}
public string Career
{
get { return career; }
set { career = value; }
}
public override bool IO()
{
try
{
base.IO();
Console.WriteLine("Please input study experience");
string _StudyExp = Console.ReadLine();
Console.WriteLine("Please input Career");
string _career = Console.ReadLine();
Console.WriteLine("StudyExp:{0}", _StudyExp);
Console.WriteLine("Carrer:{0}", _career);
}
catch
{
return false;
}
return true;
}
}
class governer : resident
{
string party, position;
public string Party
{
get { return party; }
set { party = value; }
}
public string Position
{
get { return position; }
set { position = value; }
}
public override bool IO()
{
try
{
base.IO();
Console.WriteLine("Please input study experience");
string _StudyExp = Console.ReadLine();
Console.WriteLine("Please input Career");
string _career = Console.ReadLine();
Console.WriteLine("Please input party");
string _party = Console.ReadLine();
Console.WriteLine("Please input position");
string _position = Console.ReadLine();
Console.WriteLine("StudyExp:{0}", _StudyExp);
Console.WriteLine("Carrer:{0}", _career);
Console.WriteLine("Party:{0}", _party);
Console.WriteLine("Position:{0}", _position);
}
catch
{
return false;
}
return true;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Create a resident or an adult or a governer:");
string type=Console.ReadLine();
bool result;
switch (type)
{
case "adult":
adult myAdult = new adult();
result = myAdult.IO();
break;
case "governer":
governer myGoverner = new governer();
result = myGoverner.IO();
break;
case "resident":
resident myResident = new resident();
result = myResident.IO();
break;
default:
result = true;
Console.WriteLine("you didn't create any instance, the program will end.");
break;
}
if (!result)
{
Console.WriteLine("Your input is invalid,the program will end.");
}
}
}
改动不大,就是adult和governer两个类的IO()方法都继承了基类的IO()。不过这样就行了吗?还有哪里可以改进的?
//建立三个类:居民、成人、官员。居民包含身份证号、姓名、出生日期;
//成人继承自居民,多包含学历、职业两项数据;官员则继承自成人,多包含党派、职务两项数据。
//要求每个类中都提供数据输入输出的功能。
class resident
{
string name,id,birthDate;
public string Name
{
get { return name; }
set { name = value; }
}
public string ID
{
get { return id; }
set { id = value; }
}
public string DOB
{
get { return birthDate; }
set { birthDate = value; }
}
public virtual bool IO()
{
try
{
Console.WriteLine("Please input name");
string _name = Console.ReadLine();
Console.WriteLine("Please input id");
string _id = Console.ReadLine();
Console.WriteLine("Please input DOB format as yy/mm/dd");
string s_dob = Console.ReadLine();
DateTime dt_dob;
if (DateTime.TryParse(s_dob,out dt_dob))
{
Console.WriteLine("Name:{0}", _name);
Console.WriteLine("Id:{0}", _id);
Console.WriteLine("DOB:{0}", s_dob);
}
else
throw new Exception();
}
catch
{
return false;
}
return true;
}
}
class adult : resident
{
string studyExp, career;
public string StudyExp
{
get { return studyExp; }
set { studyExp = value; }
}
public string Career
{
get { return career; }
set { career = value; }
}
public override bool IO()
{
try
{
base.IO();
Console.WriteLine("Please input study experience");
string _StudyExp = Console.ReadLine();
Console.WriteLine("Please input Career");
string _career = Console.ReadLine();
Console.WriteLine("StudyExp:{0}", _StudyExp);
Console.WriteLine("Carrer:{0}", _career);
}
catch
{
return false;
}
return true;
}
}
class governer : resident
{
string party, position;
public string Party
{
get { return party; }
set { party = value; }
}
public string Position
{
get { return position; }
set { position = value; }
}
public override bool IO()
{
try
{
base.IO();
Console.WriteLine("Please input study experience");
string _StudyExp = Console.ReadLine();
Console.WriteLine("Please input Career");
string _career = Console.ReadLine();
Console.WriteLine("Please input party");
string _party = Console.ReadLine();
Console.WriteLine("Please input position");
string _position = Console.ReadLine();
Console.WriteLine("StudyExp:{0}", _StudyExp);
Console.WriteLine("Carrer:{0}", _career);
Console.WriteLine("Party:{0}", _party);
Console.WriteLine("Position:{0}", _position);
}
catch
{
return false;
}
return true;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Create a resident or an adult or a governer:");
string type=Console.ReadLine();
bool result;
switch (type)
{
case "adult":
adult myAdult = new adult();
result = myAdult.IO();
break;
case "governer":
governer myGoverner = new governer();
result = myGoverner.IO();
break;
case "resident":
resident myResident = new resident();
result = myResident.IO();
break;
default:
result = true;
Console.WriteLine("you didn't create any instance, the program will end.");
break;
}
if (!result)
{
Console.WriteLine("Your input is invalid,the program will end.");
}
}
}
改动不大,就是adult和governer两个类的IO()方法都继承了基类的IO()。不过这样就行了吗?还有哪里可以改进的?
#5
可以尝试一下 把你的switch case 结构转换成一个函数,单独调用
那样才可以看出 虚函数的用处
那样才可以看出 虚函数的用处
#6
lz很有才,钱图很光明
#7
super.method这个是Java的写法
C#里是base.method
C#里是base.method
#8
这是面向对象编程的三大特性之一:多态性,派生类可以重写基类的方法,base.method,保留了原来基类的功能,new 则重新定义了方法
#9
主要就是继承了。
#1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~帮你顶吧。。。。
#2
public override IO(....
{
base.IO(); //这样可以吗?不是太了解你意思
}
{
base.IO(); //这样可以吗?不是太了解你意思
}
#3
支持下~~~~~~
#4
代码改良了,谢谢各位回帖的朋友,又学到了新东西base.methord()继承方法
//建立三个类:居民、成人、官员。居民包含身份证号、姓名、出生日期;
//成人继承自居民,多包含学历、职业两项数据;官员则继承自成人,多包含党派、职务两项数据。
//要求每个类中都提供数据输入输出的功能。
class resident
{
string name,id,birthDate;
public string Name
{
get { return name; }
set { name = value; }
}
public string ID
{
get { return id; }
set { id = value; }
}
public string DOB
{
get { return birthDate; }
set { birthDate = value; }
}
public virtual bool IO()
{
try
{
Console.WriteLine("Please input name");
string _name = Console.ReadLine();
Console.WriteLine("Please input id");
string _id = Console.ReadLine();
Console.WriteLine("Please input DOB format as yy/mm/dd");
string s_dob = Console.ReadLine();
DateTime dt_dob;
if (DateTime.TryParse(s_dob,out dt_dob))
{
Console.WriteLine("Name:{0}", _name);
Console.WriteLine("Id:{0}", _id);
Console.WriteLine("DOB:{0}", s_dob);
}
else
throw new Exception();
}
catch
{
return false;
}
return true;
}
}
class adult : resident
{
string studyExp, career;
public string StudyExp
{
get { return studyExp; }
set { studyExp = value; }
}
public string Career
{
get { return career; }
set { career = value; }
}
public override bool IO()
{
try
{
base.IO();
Console.WriteLine("Please input study experience");
string _StudyExp = Console.ReadLine();
Console.WriteLine("Please input Career");
string _career = Console.ReadLine();
Console.WriteLine("StudyExp:{0}", _StudyExp);
Console.WriteLine("Carrer:{0}", _career);
}
catch
{
return false;
}
return true;
}
}
class governer : resident
{
string party, position;
public string Party
{
get { return party; }
set { party = value; }
}
public string Position
{
get { return position; }
set { position = value; }
}
public override bool IO()
{
try
{
base.IO();
Console.WriteLine("Please input study experience");
string _StudyExp = Console.ReadLine();
Console.WriteLine("Please input Career");
string _career = Console.ReadLine();
Console.WriteLine("Please input party");
string _party = Console.ReadLine();
Console.WriteLine("Please input position");
string _position = Console.ReadLine();
Console.WriteLine("StudyExp:{0}", _StudyExp);
Console.WriteLine("Carrer:{0}", _career);
Console.WriteLine("Party:{0}", _party);
Console.WriteLine("Position:{0}", _position);
}
catch
{
return false;
}
return true;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Create a resident or an adult or a governer:");
string type=Console.ReadLine();
bool result;
switch (type)
{
case "adult":
adult myAdult = new adult();
result = myAdult.IO();
break;
case "governer":
governer myGoverner = new governer();
result = myGoverner.IO();
break;
case "resident":
resident myResident = new resident();
result = myResident.IO();
break;
default:
result = true;
Console.WriteLine("you didn't create any instance, the program will end.");
break;
}
if (!result)
{
Console.WriteLine("Your input is invalid,the program will end.");
}
}
}
改动不大,就是adult和governer两个类的IO()方法都继承了基类的IO()。不过这样就行了吗?还有哪里可以改进的?
//建立三个类:居民、成人、官员。居民包含身份证号、姓名、出生日期;
//成人继承自居民,多包含学历、职业两项数据;官员则继承自成人,多包含党派、职务两项数据。
//要求每个类中都提供数据输入输出的功能。
class resident
{
string name,id,birthDate;
public string Name
{
get { return name; }
set { name = value; }
}
public string ID
{
get { return id; }
set { id = value; }
}
public string DOB
{
get { return birthDate; }
set { birthDate = value; }
}
public virtual bool IO()
{
try
{
Console.WriteLine("Please input name");
string _name = Console.ReadLine();
Console.WriteLine("Please input id");
string _id = Console.ReadLine();
Console.WriteLine("Please input DOB format as yy/mm/dd");
string s_dob = Console.ReadLine();
DateTime dt_dob;
if (DateTime.TryParse(s_dob,out dt_dob))
{
Console.WriteLine("Name:{0}", _name);
Console.WriteLine("Id:{0}", _id);
Console.WriteLine("DOB:{0}", s_dob);
}
else
throw new Exception();
}
catch
{
return false;
}
return true;
}
}
class adult : resident
{
string studyExp, career;
public string StudyExp
{
get { return studyExp; }
set { studyExp = value; }
}
public string Career
{
get { return career; }
set { career = value; }
}
public override bool IO()
{
try
{
base.IO();
Console.WriteLine("Please input study experience");
string _StudyExp = Console.ReadLine();
Console.WriteLine("Please input Career");
string _career = Console.ReadLine();
Console.WriteLine("StudyExp:{0}", _StudyExp);
Console.WriteLine("Carrer:{0}", _career);
}
catch
{
return false;
}
return true;
}
}
class governer : resident
{
string party, position;
public string Party
{
get { return party; }
set { party = value; }
}
public string Position
{
get { return position; }
set { position = value; }
}
public override bool IO()
{
try
{
base.IO();
Console.WriteLine("Please input study experience");
string _StudyExp = Console.ReadLine();
Console.WriteLine("Please input Career");
string _career = Console.ReadLine();
Console.WriteLine("Please input party");
string _party = Console.ReadLine();
Console.WriteLine("Please input position");
string _position = Console.ReadLine();
Console.WriteLine("StudyExp:{0}", _StudyExp);
Console.WriteLine("Carrer:{0}", _career);
Console.WriteLine("Party:{0}", _party);
Console.WriteLine("Position:{0}", _position);
}
catch
{
return false;
}
return true;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Create a resident or an adult or a governer:");
string type=Console.ReadLine();
bool result;
switch (type)
{
case "adult":
adult myAdult = new adult();
result = myAdult.IO();
break;
case "governer":
governer myGoverner = new governer();
result = myGoverner.IO();
break;
case "resident":
resident myResident = new resident();
result = myResident.IO();
break;
default:
result = true;
Console.WriteLine("you didn't create any instance, the program will end.");
break;
}
if (!result)
{
Console.WriteLine("Your input is invalid,the program will end.");
}
}
}
改动不大,就是adult和governer两个类的IO()方法都继承了基类的IO()。不过这样就行了吗?还有哪里可以改进的?
#5
可以尝试一下 把你的switch case 结构转换成一个函数,单独调用
那样才可以看出 虚函数的用处
那样才可以看出 虚函数的用处
#6
lz很有才,钱图很光明
#7
super.method这个是Java的写法
C#里是base.method
C#里是base.method
#8
这是面向对象编程的三大特性之一:多态性,派生类可以重写基类的方法,base.method,保留了原来基类的功能,new 则重新定义了方法
#9
主要就是继承了。