gpt4 book ai didi

java - 这是对这段 Java 代码的正确封装吗?我很困惑

转载 作者:行者123 更新时间:2023-11-29 03:04:49 25 4
gpt4 key购买 nike

这段涉及 Book 类的 Java 代码是否使用了适当的封装?我觉得如果我省略一些方法会简单很多,但我们需要其中的每个方法 [尤其是 setter 和 getter]。

这是第一类:

public class Book
{
private String title;
private double price;
private final double SALES_TAX=0.075;

public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title=title;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price=price;
}
public double getSalesTax()
{
return SALES_TAX;
}
public double increasePrice(double incresePrice)
{
return incresePrice;
}
public double calculateSales(double sales)
{
return sales;
}
}

第二类:

public class BookDriver
{
public static void main(String[] args)
{
Scanner keyboard=new Scanner(System.in);

Book bookOne=new Book();
Book bookTwo=new Book();

bookOne.setTitle("Life of Pi");
System.out.print("Enter number to buy of "+bookOne.getTitle()+": ");
bookOne.setPrice(13.50*bookOne.calculateSales(keyboard.nextDouble()));
bookOne.setPrice((bookOne.getPrice()*bookOne.getSalesTax())+bookOne.getPrice());
System.out.print("Cost for "+bookOne.getTitle()+" $");
System.out.printf("%.2f"+"\n",bookOne.getPrice());

bookTwo.setTitle("Harry Potter: The Goblet Of Fire");
System.out.print("Enter number to buy of "+bookTwo.getTitle()+": ");
bookTwo.setPrice(22.00*bookTwo.calculateSales(keyboard.nextDouble()));
bookTwo.setPrice((bookTwo.getPrice()*bookTwo.getSalesTax())+bookTwo.getPrice());
System.out.print("Cost for "+bookTwo.getTitle()+" $");
System.out.printf("%.2f"+"\n",bookTwo.getPrice());

System.out.print("Enter percent increase of "+bookOne.getTitle()+": ");
bookOne.setPrice((bookOne.getPrice()*bookOne.increasePrice(keyboard.nextDouble()))+bookOne.getPrice());
System.out.printf("Cost of "+bookOne.getTitle()+": $"+"%.2f"+"\n",bookOne.getPrice());

System.out.print("Enter percent increase of "+bookTwo.getTitle()+": ");
bookTwo.setPrice((bookTwo.getPrice()*bookTwo.increasePrice(keyboard.nextDouble()))+bookTwo.getPrice());
System.out.printf("Cost of "+bookTwo.getTitle()+": $"+"%.2f"+"\n",bookTwo.getPrice());

keyboard.close();
}
}

我知道这很多,所以我对回复的期望并不高,但任何事情都会有所帮助。谢谢!!

最佳答案

再来看封装点。您有一个由属性和方法组成的类。封装背后的想法是,您希望类中的方法是更改​​属性值(状态)的唯一方法。可以这样想:如果程序中的某些其他代码想要更改其中一个属性的值,它自己无法完成,它必须请求它们所在的类上的方法来完成。这样,您就可以控制对属性的访问。

这是通过您创建的一些 getter 和 setter 方法实现的。 getter 方法返回属性的值,setter 方法将其更改为新值。

1.

您的 getter 和 setter 方法直到 increasePrice() 都很好。您正在阻止访问类中方法以外的属性。

2.

increasePrice() 只会吐出传入的内容。它不会更改任何属性的值,因此没有任何用处。如果您希望能够提高价格,您可以像这样更改方法:

  public void increasePrice(double amountOfPriceIncrease) {

price += amountOfPriceIncrease;
/*
price += amountOfPriceIncrease is the same as
price = price + amountOfPriceIncrease
*/

}

3.这条线有点麻烦。对于初学者来说,increasePrice() 除了吐出输入的内容之外什么都不做,其次,一行中发生了很多事情,这使得它变得复杂且难以理解。

bookTwo.setPrice((bookTwo.getPrice()*bookTwo.increasePrice(keyboard.nextDouble()))+bookTwo.getPrice());

关于java - 这是对这段 Java 代码的正确封装吗?我很困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32641617/

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