由于继承了不同类的值 [英] Since inheriting values of different classes

查看:100
本文介绍了由于继承了不同类的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于获得了一个类的值,并且该值将其显示在另一个类中.例如:
我有一个名为产品"的类,并且有一个名为价格"的字段.在另一个名为交易"的类中,我希望所选产品的价格出现.
感谢您的帮助.

---编辑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

推荐答案

如何设置Transaction类?

我认为它至少允许一种产品...对吗?如

How is your Transaction class set up?

I would assume it allows for at least one product...is that correct? As in

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());


这篇关于由于继承了不同类的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆