提问人:Oquin123 提问时间:6/1/2023 更新时间:6/1/2023 访问量:47
尽量避免违反封装
trying to avoid violating encapsulation
问:
在 Java 中开始一些新东西,我有点困难。我觉得我走在正确的轨道上,只是出于某种原因,无法在不违反封装的情况下找到解决我所遇到的问题的方法。我有这两个类,分别称为 Weight 和 Project。我已经完成了这两个任务,但在 Project 类中出现错误。其中包括“toOunces”、“ounces”和“OUNCES_IN_A_POUND”。这些在 Weight 类中是私有的。我已经明白我不能继承私有变量、方法等。从一个班级到另一个班级。我只是不知道如何在不违反封装的情况下解决这个问题。下面我有 Weight 类和 Project 类以及作业详细信息。
我不是在寻找任何人为我做这项工作,因为我只是在寻找技巧、窍门和建议。另外,请耐心等待,因为我仍在学习 Java 语言,并且在 CMIS 242 的第一周。
public class Weight {
//Private variables
private static final int OUNCES_IN_A_POUND = 16;
private int pounds;
private double ounces;
//A public parameterized constructor, which initializes the private variables.
public Weight(int pounds, double ounces) {
this.pounds = pounds;
this.ounces = ounces;
normalize();
}
//instance methods
private double toOunces() {
return (pounds * OUNCES_IN_A_POUND) + ounces;
}
private void normalize() {
while (ounces >= OUNCES_IN_A_POUND) {
pounds++;
ounces -= OUNCES_IN_A_POUND;
}
}
public boolean lessThan(Weight weight) {
double thisOunces = toOunces();
double otherOunces = weight.toOunces();
return thisOunces < otherOunces;
}
public void addTo(Weight weight) {
double thisOunces = toOunces();
double otherOunces = weight.toOunces();
double totalOunces = thisOunces + otherOunces;
pounds = (int) (totalOunces / OUNCES_IN_A_POUND);
ounces = totalOunces % OUNCES_IN_A_POUND;
normalize();
}
public String toString() {
return pounds + " pounds and " + String.format("%.2f", ounces) + " ounces";
}
}
----------------------------------------------------------------------------------------------------
public class Project {
private static Weight findMinimum(Weight weight1, Weight weight2, Weight weight3) {
Weight minimumWeight = weight1;
if (weight2.lessThan(minimumWeight)) {
minimumWeight = weight2;
}
if (weight3.lessThan(minimumWeight)) {
minimumWeight = weight3;
}
return minimumWeight;
}
private static Weight findMaximum(Weight weight1, Weight weight2, Weight weight3) {
Weight maximumWeight = weight1;
if (weight2.toOunces() > maximumWeight.toOunces()) {
maximumWeight = weight2;
}
if (weight3.toOunces() > maximumWeight.toOunces()) {
maximumWeight = weight3;
}
return maximumWeight;
}
private static Weight findAverage(Weight weight1, Weight weight2, Weight weight3) {
double totalOunces = weight1.toOunces() + weight2.toOunces() + weight3.toOunces();
double averageOunces = totalOunces / 3.0;
int averagePounds = (int) (averageOunces / Weight.OUNCES_IN_A_POUND);
double averageOuncesRemaining = averageOunces % Weight.OUNCES_IN_A_POUND;
return new Weight(averagePounds, averageOuncesRemaining);
}
public static void main(String[] args) {
//instances of the weight class
Weight weight1 = new Weight(11, 3);
Weight weight2 = new Weight(7, 20);
Weight weight3 = new Weight(14, 6);
System.out.println("Weight 1: " + weight1.toString());
System.out.println("Weight 2: " + weight2.toString());
System.out.println("Weight 3: " + weight3.toString());
Weight minimumWeight = findMinimum(weight1, weight2, weight3);
System.out.println("The minimum weight is " + minimumWeight.toString());
Weight maximumWeight = findMaximum(weight1, weight2, weight3);
System.out.println("The maximum weight is " + maximumWeight.toString());
Weight averageWeight = findAverage(weight1, weight2, weight3);
System.out.println("The average weight is " + averageWeight.toString());
}
}
作业详情的 PDF 文件链接:https://learn.umgc.edu/d2l/common/viewFile.d2lfile/Database/NzQ5OTk1NjE/Assignment1.pdf?ou=1016228
答:
OUNCES_IN_A_POUND是私有的,应保持私密。这就是 Project 类无法访问它的原因。
你走在正确的轨道上:这确实是一个封装问题。您要做的是将所有数学运算封装在 Weight 类本身的公共方法中。
所以,而不是这个:
double totalOunces = weight1.toOunces() + weight2.toOunces() + weight3.toOunces();
double averageOunces = totalOunces / 3.0;
int averagePounds = (int) (averageOunces / Weight.OUNCES_IN_A_POUND);
double averageOuncesRemaining = averageOunces % Weight.OUNCES_IN_A_POUND;
你想写这样的东西:
Weight sum = new Weight(weight1);
sum.addTo(weight2);
sum.addTo(weight3);
return sum.divideBy(3);
这通过让 Weight 类管理所有计算操作来实现封装。调用方永远不需要通过直接操作其数值属性来破坏封装。
正如你所看到的,需要对权重进行一些补充:
/**
* Makes a copy of an existing Weight instance.
*
* @param other non-null Weight instance to be copied
*/
public Weight(Weight other) {
// ...
}
/**
* Divides this Weight by the given factor.
*
* @param divisor amount by which this Weight will be divided
*/
public void divideBy(double divisor) {
// ...
}
我把它留给你来实现那个构造函数和那个方法。
最后,您可能需要考虑将方法重命名为 .该方法不会向参数添加任何内容,也不会以任何方式更改参数;相反,它使用参数来增加实例自身的值。addTo
add
评论
new Weight(0, averageOunces)
findAverage()
Weight
Weight