2011-03-26 66 views
1

我在转换特别是getter和setter时遇到了麻烦。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 
    } 
+0

我对我的回答做了小小的更新 – dantuch 2011-03-26 10:07:43

回答

2

在Java的getter和setter的样本:

public class Employee { 
    private int empId; 
    private String name; 
    private int age; 

    public Employee(int empId, String name, int age) { 
     this.empId = empId; 
     this.name = name; 
     this.age = age; 
    } 

    // getters & setters 

    public int getEmpId() { 
     return empId; 
    } 

    public void setEmpId(int empId) { 
     this.empId = empId; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getAge() { 
     return age; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 
} 

使用代码:

public class Sample { 

    private int _productId; 

    public int get_productId() { 
     return _productId; 
    } 

    public void set_productId(int productId) { 
     _productId = productId; 
    } 

    private Product _product = null; 

    public Product get_product() { 
     if (_product == null) { 
      _product = new Product(); 
     } 
     return _product; 
    } 

    public void set_product(Product product) { 
     _product = product; 
    } 

} 

和更多的东西:

public class Product { 

    String desription; 

    public String getDesription() { 
     return desription; 
    } 

    public void setDesription(String desription) { 
     this.desription = desription; 
    } 
} 


//this is your hidding delegation getter only in main class (Sample in my samples) 
public String getDescription(){ 
    return _product.getDesription(); 
} 
2

Java的getter和setter AREN”与C#一样易于使用。在Java中,每个getter和setter必须被明确定义,而不是使用你在那里的简写。

例如,对于您的代码“公众诠释产品编号”,您需要定义变量行,除了两个方法(getter和setter)如下:

private int _productId; 
public void setProductId(int anId) 
{ 
    _productId = anId; 
} 

public int getProductId() 
{ 
    return _productId; 
} 

你需要为每个变量定义类似的变量声明和getter/setter方法。

+0

“Java获取器和设置器不像C#那么容易使用”。 - 使用Java getters和setters有什么困难?你能说一下这个句子吗? – ivorykoder 2011-08-02 10:52:13