【发布时间】: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