C# 到 Java 的转换

标签 c# java setter getter

我在转换时遇到了问题,尤其是 getter 和 setter。

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
    }

最佳答案

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

关于C# 到 Java 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5441505/

相关文章:

c# - 如何使用 Roslyn 在 C# 中获取 lambda 类型?

javascript - 将 Javascript getter/setter 复制到另一个原型(prototype)对象

c# - Nsubstitute 检查 setter 是否被调用

c# - 如何根据条件将 VIew 模型的属性绑定(bind)到 DataTrigger Setter?

java - 使用 SLF4J 桥接 JUL 不起作用

java - Android Studio if 语句与 OR (||) 函数

c# - WPF下载文件并同时保存

c# - TextRenderer.DrawText 在 XP 和 Vista 上以不同方式呈现 Arial

c# - Visual Studio 不在目标框架下拉列表中显示 .NET Core 2.2

java - 安卓耳机插孔