我正在学习 OOAD 并尝试通过继承来实现类关系,但这里有一个问题是代码
家长班
namespace ConsoleApplication1
{
abstract class Classification
{
public abstract string type();
}
}
第一儿童班
namespace ConsoleApplication1
{
class FullTime : Classification
{
bool inCampus;
string roomDetail;
float rent;
public FullTime(string studentRoomDetail, float studentRent)
{
this.inCampus = true;
this.roomDetail = studentRoomDetail;
this.rent = studentRent;
}
public FullTime()
{
this.inCampus = false;
}
public string printAccommodationDescription()
{
if (!this.inCampus)
{
return "Not in campus";
}
else
{
return "Room: " + this.roomDetail + " Rent: " + this.rent.ToString();
}
}
public override string type()
{
return "fulltime";
}
}
}
第二儿童班
namespace ConsoleApplication1
{
class PartTime : Classification
{
bool onJob;
string jobTitle;
float salary;
public PartTime(string studentJobTitle, float studentSalary)
{
this.onJob = true;
this.jobTitle = studentJobTitle;
this.salary = studentSalary;
}
public PartTime()
{
this.onJob = false;
}
public string printJobDescription()
{
if (!this.onJob)
{
return "Not on job";
}
else
{
return "JobTitle: " + this.jobTitle + " Salary: " + this.salary.ToString();
}
}
public override string type()
{
return "parttime";
}
}
}
现在在Program.cs中,当我尝试printJobDescription
从PartTime
类中访问方法时
Classification classification = new PartTime("Software Engineer", 10000);
classification.printJobDescription();
它说
错误 CS1061 'Classification' 不包含'printAccommodationDescription' 的定义,并且找不到接受'Classification' 类型的第一个参数的扩展方法'printAccommodationDescription'(您是否缺少 using 指令或程序集引用?)
我该如何解决这个问题?
更新
我需要让对象在运行时更改其类的能力,因此我必须创建类型的对象Classification
并使用其他类中未实现的任一方法