是否可以从类employee中的类person创建对象并访问C#net中的方法

是否可以从类employee中的类person创建对象并访问C#net中的方法,c#,class,oop,object,C#,Class,Oop,Object,我是否可以从类employee中的类person创建对象,并使用我在类employee中创建的person对象通过employee类访问类person的方法和成员 public class Person { protected string _name; public int _age; public string Name { get { return _name; } set { _name = value; } }

我是否可以从类employee中的类person创建对象,并使用我在类employee中创建的person对象通过employee类访问类person的方法和成员

public class Person
{
    protected string _name;
    public int _age;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public int Age
    {
        get { return _age; }
        set { _age = value; }
    }
}

class Employee
{
    Person person = new Person();
}
你是说这个吗

public class Employee
{
    private Person person = new Person();

    public string Name
    {
        get { return person.Name; }
        set { person.Name = value; }
    }

    public int Age
    {
        get { return person.Age; }
        set { person.Age = value; }
    }
}

是的,因为班级人员是公开的。

您可以:

class Employee
{
    Person person = new Person();

    public string Name 
    {
        get { return person.Name; }
        set { person.Name = value } 
    }

    public int Age
    {
        get { return person.Age; }
        set { person.Age = value; }
    }
}

您可以考虑提供一个“person”接口,并让employee类实现它。

是的,您可以。问之前你试过了吗?为什么不干脆
public class Employee{public personalinfo{get;set;}/*其他员工属性*/}
谢谢。我忘了将对象放入方法中。因为我把它放在了方法之外。