C#到Java的转换
我在转换中遇到了困难,尤其是接球手和二传手
public class CartItem : IEquatable<CartItem>
{
#region Attributes
public int Quantity { get; set; }
private int _productId;
public int ProductId
{
get { return _productId; }
set
{
_product = null;
_productId = value;
}
}
private Product _product = null;
public Product Prod
{
get
{
if (_product == null)
{
_product = new Product(ProductId);
}
return _product;
}
}
public string Name
{
get { return Prod.ProductName; }
}
public string Description
{
get { return Prod.Description; }
}
public float UnitPrice
{
get { return Prod.UnitPrice; }
}
public float TotalPrice
{
get { return UnitPrice * Quantity; }
}
#endregion
#region Methods
public CartItem(int productId)
{
this.ProductId = productId;
}
public bool Equals(CartItem item)
{
return item.ProductId == this.ProductId;
}
#endregion
}
# 1 楼答案
Java的getter和setter没有C#那么容易使用。在Java中,每个getter和setter都必须显式定义,而不是使用那里的速记
例如,对于代码“public int ProductId”,除了两个方法(getter和setter)之外,还需要一行定义变量,如下所示:
您需要为每个变量定义类似的变量声明和getter/setter方法
# 2 楼答案
Java中的getter和setter示例:
使用您的代码:
还有更多: