C#无法从抽象父类对象访问子公共方法
我正在学习OOAD并尝试用继承实现类关系,但这里有一个问题是代码 父类C#无法从抽象父类对象访问子公共方法,c#,inheritance,abstract-class,class-relationship,C#,Inheritance,Abstract Class,Class Relationship,我正在学习OOAD并尝试用继承实现类关系,但这里有一个问题是代码 父类 namespace ConsoleApplication1 { abstract class Classification { public abstract string type(); } } namespace ConsoleApplication1 { class FullTime : Classification { bool inCampu
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";
}
}
}
第一儿童班
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";
}
}
}
第二儿童班
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中,我试图从PartTime
类访问方法printjobsdescription
Classification classification = new PartTime("Software Engineer", 10000);
classification.printJobDescription();
上面说
错误CS1061“Classification”不包含“PrintAccountationDescription”的定义,并且找不到接受“Classification”类型的第一个参数的扩展方法“PrintAccountationDescription”(是否缺少using指令或程序集引用?)
我如何解决这个问题
更新
我需要能够让对象在运行时更改其类,因此我必须创建类型为
分类的对象,并在将对象强制转换为另一个对象类型时使用另一个类中未实现的方法。这意味着您只能使用暴露于目标对象类型的方法和属性,目标对象类型是分类
,它不知道您的方法
我举了一个简单的例子:
using System;
namespace Program
{
public class Program
{
public static void Main()
{
Dog rex = new Dog();
Animal rexAsAnimal = rex;
// Can access 'MakeSound' due the fact it declared at Dog (Inherited by Animal)
Console.WriteLine(rex.MakeSound()); // Output: Bark
// Compilation error: rexAsAnimal is defined as 'Animal' which doesn't have the 'Bark' method.
//Console.WriteLine(rexAsAnimal.Bark()); // Output when uncomment: Compilation error.
// Explicitly telling the compiler to cast the object into "Dog"
Console.WriteLine(((Dog)rexAsAnimal).Bark()); // Output: Bark
}
}
public abstract class Animal
{
public abstract string MakeSound();
}
public class Dog : Animal
{
public override string MakeSound() { return Bark(); }
public string Bark()
{
return "Bark";
}
}
}
只能使用在所用类中声明的函数
abstract class Classification
{
public abstract string type();
}
class PartTime : Classification
{
public override string type() {...}
public Job1() {...}
}
class FullTime : Classification
{
public override string type() {...}
public Job2() {...}
}
- 类型分类的对象只能使用类型()
- PartTime类型的对象可以使用type和Job1()
- FullTime类型的对象可以使用type和Job2()
如果您有这样一个对象:
Classification classification = new PartTime();
如果您不知道是哪种特殊类型,则必须强制转换此对象才能使用其他方法:
if (classification is PartTime)
{
((PartTime)classification).Job1();
}
else if (classification is FullTime)
{
((FullTime)classification).Job2();
}
希望这有帮助。您正在使用类型分类。此类型没有printJobDescription()方法。所以你不能用它。要使用此方法,它必须是PartTime类型。但我还需要能够让对象在运行时更改其类,因此我必须创建类型分类的对象,并使用未在其他classI上实现的任何一种方法。我将添加一些代码。。。请稍等:-)谢谢@Fruchtzwerg…当然可以。是的,谢谢,先生:)