gpt4 book ai didi

java - 如何重置构造函数的 getter 和 setter

转载 作者:行者123 更新时间:2023-12-01 06:01:31 25 4
gpt4 key购买 nike

在我最近的大学工作中,我收到了一项任务,我认为该任务是重新分配由 getter 和 setter 设置的值。确切的问题是"编辑所有类别共享的产品主要信息,如ID、描述等”

我对这个问题的尝试如下。

 public void editProductInformation() { //Standard level task 2 method 3

this.ID = 456890;
this.description = "Eggs";
this.recommendedUnitPrice = 10;
this.Unit = 6;
this.weight = 750;
}

}

这也是该类其余部分的代码

public class Product {

Scanner input = new Scanner(System.in);

protected int ID;
protected String description;
protected double recommendedUnitPrice;
protected double actualPrice;
protected int Unit = 1;
protected double weight;
protected LocalDate expiaryDate;
protected LocalDate expireDate;

public Product(int ID, String description, double recommendedUnitPrice, int unit, double weight, LocalDate expiarayDate) {
this.ID = ID;
this.description = description;
this.recommendedUnitPrice = recommendedUnitPrice;
this.Unit = unit;
this.weight = weight;
this.expiaryDate = expiarayDate;

}

public int getID() {
return this.ID;
}

public void setID(int ID) {
this.ID = ID;
}

public String getDescription() {
return this.description;
}

public void setDescripption(String description) {
this.description = description;
}

public double getRecommendedUnitPrice() {
return this.recommendedUnitPrice;
}

public void setRecommendedUnitPrice(double recommendedUnitPrice) {
this.recommendedUnitPrice = recommendedUnitPrice;
}

public int getUnit() {
return this.Unit;
}

public void setUnit(int unit) {
this.Unit = unit;
}

public double getWeight() {
return this.weight;
}

public void setWeight(double Weight) {
this.weight = Weight;
}

public LocalDate getExpiaryDate() {
return expiaryDate;
}

public void setExpiaryDate(LocalDate expiaryDate) {
this.expiaryDate = expiaryDate;
}

public void setPrice() {
System.out.println("Enter the price: ");
this.actualPrice = input.nextDouble();
}

public void setExpireDate() {

System.out.println("Enter the given date: ");
int day = input.nextInt();
int month = input.nextInt();
int year = input.nextInt();
LocalDate date = LocalDate.of(day, month, year);
this.expireDate = date;
}

public void comparePrice() { // Standard level task 2 method 1

if (recommendedUnitPrice == actualPrice) {
System.out.println("Price is equal");
} else if (recommendedUnitPrice < actualPrice) {
System.out.println("Price is less");
} else if (recommendedUnitPrice > actualPrice) {
System.out.println("Price is more");
}

}

public void verifyExpireProduct() { //Standard level task 2 method 2

if(expireDate == expiaryDate) {
System.out.println("Product is expired");
}
else System.out.println("Product is not expired");
}

我知道这个问题可能解释得不够好,所以我知道人们是否希望投反对票。非常感谢任何见解或帮助!

最佳答案

您可以创建此类的对象,然后使用类中定义的 setter 函数来设置值。

Product p1 = new Product();
p1.setId(5);
//This will set the Id to 5

关于java - 如何重置构造函数的 getter 和 setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57555604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com