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

时间:2015-11-28 17:13:44

标签: c# inheritance abstract-class class-relationship

我正在学习OOAD并试图用继承实现类关系,但这里有一个问题是代码

父类

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";
        }
    }
}
当我尝试从printJobDescription

访问方法PartTime时,

现在在 Program.cs

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

它说

  

错误CS1061'分类'不包含' printAccommodationDescription'的定义没有扩展方法' printAccommodationDescription'接受类型'分类'的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

我该如何解决这个问题?

更新

我需要能够让对象在运行时更改它的类,所以我必须创建类型为Classification的对象并使用其他类中未实现的方法

2 个答案:

答案 0 :(得分:1)

您只能使用您所使用的类中声明的函数。

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();

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

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

希望这有帮助。

答案 1 :(得分:0)

当你将对象转换为另一个对象类型时,它会调用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";
        }
    }
}