在C#

时间:2015-04-22 12:04:45

标签: c# class

我有两个类,一个叫做Degree,另一个叫Uprogram。我想在UProgram中添加一个变量来保存Degree对象。我该怎么做?

class UProgram
    {
       //private member variables for UProgram
        private string programName;
        private string departmentName; 

    //public properties for UProgram
    public string ProgramName
    {
        get { return programName; }
        set { programName = value; }
    }
    public string DepartmentName
    {
        get { return departmentName; }
        set { departmentName = value; }
    }  
}

class Degree { //private member variables for Degree private string degreeType; private string degreeName;

//public properties for Degree public string DegreeType { get { return degreeType; } set { degreeType = value; } } public string DegreeName { get { return degreeName; } set { degreeName = value; } } }

3 个答案:

答案 0 :(得分:4)

以同样的方式添加了字符串属性:

class UProgram
{
    //private member variables for UProgram
    private string programName;
    private string departmentName;
    private Degree degree;

    //public properties for UProgram
    public string ProgramName
    {
        get { return programName; }
        set { programName = value; }
    }
    public string DepartmentName
    {
        get { return departmentName; }
        set { departmentName = value; }
    }

    public Degree Degree
    {
        get { return degree; }
        set { degree = value; }
    }
}

现在,您可以提供适当的构造函数来传递Degree对象,或者只使用属性:

UProgram prog = new UProgram();
Degree degree = new Degree { DegreeName = "Foo", DegreeType = "type" };
prog.Degree = degree;

答案 1 :(得分:1)

Degree

中创建UProgram类型的媒体资源

答案 2 :(得分:0)

class UProgram
{
   //private member variables for UProgram
    private string programName;
    private string departmentName; 
    private Degree __degree;

//public properties for UProgram
public string ProgramName
{
    get { return programName; }
    set { programName = value; }
}
public string DepartmentName
{
    get { return departmentName; }
    set { departmentName = value; }
}  
public Degree UProgDegree
{
    get{return __degree;}
    set {__degree = value;}
}

}