C#无法从抽象父类对象访问子公共方法 [英] C# Cannot access child public method from abstract parent class object

查看:100
本文介绍了C#无法从抽象父类对象访问子公共方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习OOAD,并尝试通过继承实现类关系,但是这里有一个问题

I'm learning OOAD and trying to implement class relationship with inheritance but there is an issue here is the code

父类

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";
        }
    }
}

现在在程序中.cs ,当我尝试从 PartTime

Classification classification = new PartTime("Software Engineer", 10000);
classification.printJobDescription();


错误CS1061'分类'不包含'printAccommodationDescription'的定义,找不到扩展方法'printAccommodationDescription'接受类型为'Classification'的第一个参数(您是否缺少using指令或程序集引用?)

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?)

如何解决此问题?

更新

我需要让对象在运行时更改其类的能力,因此我必须创建 Classification 并使用其他类中未实现的方法

I need the ability to let object change its class at runtime, so I have to create the object of type Classification and use either method that is not implemented in other class

推荐答案

您只能使用在您自己的类中声明的函数

You can only use the functions declared in the class you use.

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() {...}
}




  • 分类类型的对象只能使用type()

  • PartTime类型的对象可以使用type和Job1()

  • FullTime类型的对象可以使用type和Job2( )

  • 如果您有这样的对象:

    Classification classification = new PartTime();
    

    并且您不知道哪种特殊类型,必须将此对象强制转换为使用其他方法:

    and you don´t know which special type, you have to cast this object to use other methods:

    if (classification is PartTime)
    {
      ((PartTime)classification).Job1();
    }
    else if (classification is FullTime)
    {
      ((FullTime)classification).Job2();
    }
    

    希望这会有所帮助。

    这篇关于C#无法从抽象父类对象访问子公共方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆