继承麻烦c#

时间:2017-03-27 17:04:00

标签: c# inheritance

我似乎在继承方面遇到了一些麻烦。我不确定是什么问题,但我相信这是错误地调用了类。我仍然很擅长继承,所以我确定有些地方我错了。它应该从每个班级调用一些东西并在最后制作一个短语,但它似乎没有用。它也应该覆盖之前的Show Address。

class DSC
{
    private string schoolName { get; set; }


    string schoolName = "DSC";

    public virtual string ShowAddress();

    { return " 1420 W. Highway Blvd., Orlando, Florida 33268 "}        
}


class Campus
{
    private string campusName { get; set; }


    public string campusName
    {
        get
        {
            return campusName;
        }
        set
        {
            campusName = value;
        }

        public Campus(string cName);


    public virtual string ShowAddress()
    { return "1843 Bob Blvd., Orlando, Florida 33268"; }
    public string Departments()
    { return "Computer Scinece Department, Emergency Care Department, Police Academy"; }
}

class Program
{
    static void Main(string[] args)
    {
        Campus atc = new Campus("Advanced Technology College");
        Console.WriteLine(atc.ToString());
    }
}

输出应该是什么

Daytona State College Advanced Technology College 
    is located at  1843 Bob Blvd., Orlando, Florida 33268, 
    it has Computer Scinece Department, Emergency Care Department,  Police Academy

1 个答案:

答案 0 :(得分:1)

删除私人campusName的getter和setter,并将您的公共campusName重命名为CampusName。

在Campus课程中添加以下代码

public override string ToString()
{
  return CampusName() + "\n is located at " + ShowAddress() + "\n it has " + Departments();
}

你应该写一个基础学校课程

public class School
{
  private string schoolName;
  private string address;

  public string SchoolName
  {
     get
     {
       return schoolName;
     }
     set
     {
       schoolName = value;
     }
  }

  // same game with address
}

之后你只需要继承......

public Campus : School
{
   // override or add methods 
}