简体   繁体   English

C#无法从抽象父类对象访问子公共方法

[英]C# Cannot access child public method from abstract parent class object

I'm learning OOAD and trying to implement class relationship with inheritance but there is an issue here is the code我正在学习 OOAD 并试图通过继承来实现类关系,但这里有一个问题是代码

Parent Class家长班

namespace ConsoleApplication1
{
    abstract class Classification
    {
        public abstract string type();
    }
}

1st Child Class 1 年级儿童

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

2nd Child Class儿童二班

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

Now in Program.cs when I tried to access method printJobDescription from PartTime class现在在Program.cs 中,当我尝试从PartTime类访问方法printJobDescription

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

it says它说

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?)错误 CS1061“分类”不包含“printAccommodationDescription”的定义,并且找不到接受“分类”类型的第一个参数的扩展方法“printAccommodationDescription”(您是否缺少 using 指令或程序集引用?)

How can I solve this issue?我该如何解决这个问题?

UPDATE更新

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我需要让对象在运行时改变它的类的能力,所以我必须创建类型Classification的对象并使用其他类中未实现的方法

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() {...}
}
  • A object of type Classification can only use the type()分类类型的对象只能使用 type()
  • A object of the type PartTime can use type and Job1() PartTime 类型的对象可以使用 type 和 Job1()
  • A object of the type FullTime can use type and Job2() 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();
}

Hope this helps.希望这可以帮助。

When casting you're object into another object type, that called Polymorphism .将对象转换为另一种对象类型时,称为Polymorphism This translate that you can only use the methods and properties that exposed to the destination object type, which is Classification which doesn't know your method.这意味着您只能使用公开给目标对象类型的方法和属性,即Classification ,它不知道您的方法。

Simple example i made:我做的简单例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM