【问题标题】:Use a class from Class Library and use it to be the Base Class of other Class Library使用类库中的一个类并将其用作其他类库的基类
【发布时间】:2013-12-13 03:50:18
【问题描述】:

我正在尝试练习 OOP 的继承。我有 2 个类库。第一个是“BaseClass”,第二个是“DerivedClass”。在 BaseClass 库中,我有一个 Person 类。在 DerivedClass 库中,我有一个 Student 类。我想使用 Person 类作为 Student 的基类,但我不能。我已经添加了对 DerivedClass 的引用,但它仍然没有工作。怎么做?或者是否可以使用其他类库中的类作为其他类库中的类的基类?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseClass
{
    class Person
    {
        public String Firstname { get; set; }
        public String Middlename { get; set; }
        public String Surname { get; set; }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BaseClass.Person;

namespace DerivedClass
{
    class Student
    {
         public int StudentID {get; set;}
    }
}

【问题讨论】:

    标签: c# .net oop


    【解决方案1】:

    要从另一个类继承,请使用此表示法:class Student : Person {...}

    而且您可能必须将基类标记为公共的。

    class Student : Person {...}
    public class Person {...}
    

    【讨论】:

    • 我将公众包括在 Person 上。现在好了。谢谢。
    • 如果我的回答有帮助,请将其标记为对此主题的回答。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2012-07-20
    • 2016-01-03
    • 1970-01-01
    • 2012-11-09
    • 2011-04-05
    相关资源
    最近更新 更多