C# 到 Java 的转换

发布于 2024-10-26 17:36:20 字数 1397 浏览 1 评论 0原文

我在转换时遇到问题,尤其是 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
    }

I'm having trouble converting especially the getter and 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
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别把无礼当个性 2024-11-02 17:36:20

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

sample of getters and setters in Java:

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

using your code:

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

}

and something more:

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();
}
陌伤ぢ 2024-11-02 17:36:20

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 getters and setters aren't as easy to use as C#'s. In Java, every getter and setter has to be explicitly defined, rather than using the shorthand you have there.

For example, for your code "public int ProductId", you would need a line defining the variable, in addition two methods (a getter and setter) as follows:

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

public int getProductId()
{
   return _productId;
}

You'd need to define similar variable declarations and getter/setter methods for each variable you have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文