【问题标题】:Object of a class (x) as an attribute of a class (y)类 (x) 的对象作为类 (y) 的属性
【发布时间】:2019-08-08 08:44:09
【问题描述】:

我无法找到答案,因为我不知道如何表达。

我有一个类 Car() 和一个类 Owner()。我需要的是拥有一个“所有者”对象,作为我的 Car() 类的一个简单属性,因此一旦我实例化我的 Car() 对象,我就可以将它作为参数传递。

我的所有者()类:

class Owner
{
    public Owner(string address){
        this.address = address;
    }
}

我的汽车()类:

class Car
{
    public Car(object owner){ // what type to use?
        this.owner = owner; 
    }

    private object owner; // what type to use?
}

还有我的 Main() 类:

static void Main(string[] args){
    Owner owner1 = new Owner("street foo city bar");
    Car car1 = new Car(owner1); // this needs to work
}

显然,对属性使用“对象”类型并没有做到这一点。一旦我打印我得到'myProjectName.Owner'。提前谢谢你。

【问题讨论】:

  • public Car(Owner owner)?

标签: c# oop object constructor attributes


【解决方案1】:

您可以编写如下代码,编写公共方法来返回您想要的对象。

 class Program
    {
        static void Main(string[] args)
        {
            Owner owner1 = new Owner("street foo city bar");
            Car car1 = new Car(owner1); // this needs to work

            Console.WriteLine("Car 1 owner address : " + car1.getOwner().getAddress());
            Console.ReadKey();


        }
    }


    class Car
    {
        private Owner owner;  // same here\
        public Car(Owner owner)
        { // use Owner class
            this.owner = owner;
        }

        public Owner getOwner() // write a public method to return owner
        {
            return this.owner;
        }


    }


    class Owner
    {

        private string address; // this 
        public Owner(string address)
        {

            this.address = address;
        }

        public string getAddress() // write a public method to return address
        {
            return this.address;
        }
    }

【讨论】:

  • 天哪,我已经创建了我的 getter 和 setter 来尝试一下,幸运的是我仔细检查了它,我的“public Owner getOwner()”实际上是“public Object getOwner()”(我以前的在问问题之前尝试)。我的 IDE 让我改变了我的 setter,但没有改变我的 getter。感谢您让我意识到这一点。
【解决方案2】:
public class Car
{
    public Car(Owner owner)
    {
        this.Owner = owner;
    }

    //Since Owner is public, you don't have to create a getter method for this. i.e GetOwner()
    public Owner Owner;
}

public class Owner
{
    //Since address is private, you'll have to create a public getter for this
    private string address;

    public Owner(string address)
    {
        this.address = address;
    }

    //public getter for the address
    public string GetAddress()
    {
        return this.address;
    }
}

public class Main
{
    static void main(string[] args)
    {
        Owner owner1 = new Owner("street address");
        Car car1 = new Car(owner1);

        car1.Owner.GetAddress();
    }
}

【讨论】:

  • 嗯,这似乎有效,但我仍然得到'myProjectName.Owner',这是有道理的。老实说,我不想对它做任何进一步的事情。我只需要完成这个:在创建对象时将 Car 与其所有者相关联。关于如何验证的任何提示?就像打印所有者变量的名称,或访问它的属性之一。类似'car1.getOwner().getAddress()'。
  • @JhonatanCruz 您必须在您的Owner 类中创建getAddress() 方法,该方法返回this.address。我已经更新了您问题的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
  • 2016-11-22
  • 2020-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多