C# Cannot access child public method from abstract

2020-04-16 03:49发布

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

Parent Class

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

1st Child Class

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

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

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

2条回答
我命由我不由天
2楼-- · 2020-04-16 04:02

When casting you're object into another object type, that called 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.

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";
        }
    }
}
查看更多
何必那么认真
3楼-- · 2020-04-16 04:03

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()
  • A object of the type PartTime can use type and Job1()
  • A object of the type FullTime can use type and 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.

查看更多
登录 后发表回答