C# 无法从抽象 parent class object 访问 child public 方法
C# Cannot access child public method from abstract parent class object
我正在学习 OOAD 并尝试实现 class 与继承的关系,但这里有一个问题是代码
Parent Class
namespace ConsoleApplication1
{
abstract class Classification
{
public abstract string type();
}
}
第一ChildClass
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";
}
}
}
第二个ChildClass
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
class
访问方法 printJobDescription
时
Classification classification = new PartTime("Software Engineer", 10000);
classification.printJobDescription();
它说
Error CS1061 'Classification' does not contain a definition for 'printAccommodationDescription' and no extension method 'printAccommodationDescription' accepting a first argument of type 'Classification' could be found (are you missing a using directive or an assembly reference?)
我该如何解决这个问题?
更新
我需要让 object 在运行时更改其 class 的能力,因此我必须创建 Classification
类型的 object 并使用以下任一方法未在其他 class
中实施
将您的对象转换为另一种对象类型时,称为 Polymorphism。这意味着您只能使用暴露给目标对象类型的方法和属性,该类型 Classification
不知道您的方法。
我做的简单例子:
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";
}
}
}
您只能使用在您使用的 class 中声明的函数。
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() {...}
}
- Classification类型的对象只能使用type()
- PartTime 类型的对象可以使用类型和 Job1()
- FullTime 类型的对象可以使用类型和 Job2()
如果你有这样的对象:
Classification classification = new PartTime();
而且您不知道是哪种特殊类型,您必须强制转换此对象才能使用其他方法:
if (classification is PartTime)
{
((PartTime)classification).Job1();
}
else if (classification is FullTime)
{
((FullTime)classification).Job2();
}
希望对您有所帮助。
我正在学习 OOAD 并尝试实现 class 与继承的关系,但这里有一个问题是代码
Parent Class
namespace ConsoleApplication1
{
abstract class Classification
{
public abstract string type();
}
}
第一ChildClass
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";
}
}
}
第二个ChildClass
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
class
printJobDescription
时
Classification classification = new PartTime("Software Engineer", 10000);
classification.printJobDescription();
它说
Error CS1061 'Classification' does not contain a definition for 'printAccommodationDescription' and no extension method 'printAccommodationDescription' accepting a first argument of type 'Classification' could be found (are you missing a using directive or an assembly reference?)
我该如何解决这个问题?
更新
我需要让 object 在运行时更改其 class 的能力,因此我必须创建 Classification
类型的 object 并使用以下任一方法未在其他 class
将您的对象转换为另一种对象类型时,称为 Polymorphism。这意味着您只能使用暴露给目标对象类型的方法和属性,该类型 Classification
不知道您的方法。
我做的简单例子:
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";
}
}
}
您只能使用在您使用的 class 中声明的函数。
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() {...}
}
- Classification类型的对象只能使用type()
- PartTime 类型的对象可以使用类型和 Job1()
- FullTime 类型的对象可以使用类型和 Job2()
如果你有这样的对象:
Classification classification = new PartTime();
而且您不知道是哪种特殊类型,您必须强制转换此对象才能使用其他方法:
if (classification is PartTime)
{
((PartTime)classification).Job1();
}
else if (classification is FullTime)
{
((FullTime)classification).Job2();
}
希望对您有所帮助。