C#到Java的转换

C#到Java的转换,c#,java,setter,getter,C#,Java,Setter,Getter,我在转换中遇到了困难,尤其是接球手和二传手 public class CartItem : IEquatable<CartItem> { #region Attributes public int Quantity { get; set; } private int _productId; public int ProductId { get { return _pro

我在转换中遇到了困难,尤其是接球手和二传手

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
    }
公共类CartItem:IEquatable
{
#区域属性
公共整数数量{get;set;}
私有int_productId;
公共int ProductId
{
获取{return\u productId;}
设置
{
_product=null;
_productId=值;
}
}
私有产品_Product=null;
公共产品产品
{
得到
{
如果(_product==null)
{
_产品=新产品(产品ID);
}
退货产品;
}
}
公共字符串名
{
获取{return Prod.ProductName;}
}
公共字符串描述
{
获取{返回产品描述;}
}
公开浮动单价
{
获取{return Prod.UnitPrice;}
}
公开浮动总价格
{
获取{返回单价*数量;}
}
#端区
#区域方法
公共CartItem(int productId)
{
this.ProductId=ProductId;
}
公共布尔等于(CartItem)
{
return item.ProductId==this.ProductId;
}
#端区
}

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

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

Java的getter和setter不像C语言那么容易使用。在Java中,每个getter和setter都必须显式定义,而不是使用那里的速记

例如,对于代码“public int ProductId”,除了两个方法(getter和setter)之外,还需要一行定义变量,如下所示:

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

public int getProductId()
{
   return _productId;
}

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

Java getter和setter不如C#容易使用。在Java中,每个getter和setter都必须显式定义,而不是使用那里的速记

例如,对于代码“public int ProductId”,除了两个方法(getter和setter)之外,还需要一行定义变量,如下所示:

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

public int getProductId()
{
   return _productId;
}

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

我对我的回答做了一些更新我对我的回答做了一些更新“Java getter和setter不如C#容易使用”-使用Java getter和setter有什么困难?你能解释一下这个句子吗?“Java getter和setter不如C#容易使用。”-使用Java getter和setter有什么困难?你能解释一下这个句子吗?