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

查看:26
本文介绍了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";
        }
    }
}

现在在 Program.cs 中,当我尝试从 PartTime 类访问方法 printJobDescription

Now in Program.cs when I tried to access method printJobDescription from PartTime class

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

它说

错误 CS1061分类"不包含printAccommodationDescription"的定义,并且找不到接受分类"类型的第一个参数的扩展方法printAccommodationDescription"(您是否缺少 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?)

我该如何解决这个问题?

How can I solve this issue?

更新

我需要能够让对象在运行时改变它的类,所以我必须创建 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() {...}
}

  • Classification 类型的对象只能使用 type()
  • PartTime 类型的对象可以使用 type 和 Job1()
  • FullTime 类型的对象可以使用 type 和 Job2()
  • 如果你有一个这样的对象:

    If you have an object like this:

    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天全站免登陆