本文介绍了由于继承了不同类的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于获得了一个类的值,并且该值将其显示在另一个类中.例如:
我有一个名为产品"的类,并且有一个名为价格"的字段.在另一个名为交易"的类中,我希望所选产品的价格出现.
感谢您的帮助.
---编辑kschuler:删除了代码块
Since a value of a class obtains and that it shows it in other one. For example:
I have a class called Product and there I have a Field called Price. In another class called Transaction I want that the price appears for the selected product.
I am grateful for any help.
---EDIT kschuler: removed code block
推荐答案
class Transaction
{
private Product _purchasedProduct;
public Product PurchasedProduct
{
get
{
return this._purchasedProduct;
}
set
{
this._purchasedProduct = value;
}
}
}
如果是这样,那么您可以这样做:
If that''s the case, then you could just do:
Transaction newTransaction = new Transaction();
newTransaction.PurchasedProduct = new Banana();
Console.WriteLine(newTransaction.PurchasedProduct.Price.ToString());
这篇关于由于继承了不同类的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!