【发布时间】:2010-11-30 12:02:38
【问题描述】:
请原谅篇幅,但这里有两个程序,完全相同,但一个有一个没有 setter、getter 和构造函数。
我之前上过一个基本的 C++ 课程,但不记得其中的任何内容,目前我没有看到它们的意义,如果有人能用蹩脚的术语解释它们,我将不胜感激它...目前它们似乎只不过是浪费空间让我的代码看起来更长,但老师说它们很重要(到目前为止就是这样)。
提前致谢!现在这里是代码: Mileage.java:
package gasMileage;
import java.util.Scanner; //program uses class Scanner
public class Mileage
{
public int restart;
public double miles, gallons, totalMiles, totalGallons, milesPerGallon;
public Mileage(int newRestart, double newMiles, double newGallons,
double newTotalMiles, double newTotalGallons, double newMilesPerGallon)
{
setRestart(newRestart);
setMiles(newMiles);
setGallons(newGallons);
setTotalMiles(newTotalMiles);
setTotalGallons(newTotalGallons);
setMilesPerGallon(newMilesPerGallon);
}
public void setRestart(int newRestart)
{
restart = newRestart;
}
public int getRestart()
{
return restart;
}
public void setMiles(double newMiles)
{
miles = newMiles;
}
public double getMiles()
{
return miles;
}
public void setGallons(double newGallons)
{
gallons = newGallons;
}
public double getGallons()
{
return gallons;
}
public void setTotalMiles(double newTotalMiles)
{
totalMiles = newTotalMiles;
}
public double getTotalMiles()
{
return totalMiles;
}
public void setTotalGallons(double newTotalGallons)
{
totalGallons = newTotalGallons;
}
public double getTotalGallons()
{
return totalGallons;
}
public void setMilesPerGallon(double newMilesPerGallon)
{
milesPerGallon = newMilesPerGallon;
}
public double getMilesPerGallon()
{
return milesPerGallon;
}
public void calculateMileage()
{
Scanner input = new Scanner(System.in);
while(restart == 1)
{
System.out.print("Please input number of miles you drove: ");
miles = input.nextDouble();
totalMiles = totalMiles + miles;
System.out.print("Please input number of gallons you used: ");
gallons = input.nextDouble();
totalGallons = totalGallons + gallons;
milesPerGallon = miles / gallons;
System.out.printf("Your mileage is %.2f MPG.\n", milesPerGallon);
System.out.print("Would you like to try again? 1 for yes, 2 for no: ");
restart = input.nextInt();
}
milesPerGallon = totalMiles / totalGallons;
System.out.printf("Your total mileage for these trips is: %.2f.\nYour total gas consumed on these trips was: %.2f.\n", totalMiles, totalGallons);
System.out.printf("Your total mileage for these trips is: %.2f MPG", milesPerGallon);
}
}
Mileagetest.java:
package gasMileage;
public class Mileagetest
{
public static void main(String[] args)
{
Mileage myMileage = new Mileage(1,0,0,0,0,0);
myMileage.calculateMileage();
}
}
现在对于没有 setter 和 getter 的人:
Testmileage.java:
package gasMileage;
import java.util.Scanner;
public class Testmileage
{
int restart = 1;
double miles = 0, milesTotal = 0, gas = 0, gasTotal = 0, mpg = 0;
Scanner input = new Scanner(System.in);
public void testCalculate()
{
while(restart == 1)
{
System.out.print("Please input miles: ");
miles = input.nextDouble();
milesTotal = milesTotal + miles;
System.out.print("Please input gas: ");
gas = input.nextDouble();
gasTotal = gasTotal + gas;
mpg = miles/gas;
System.out.printf("MPG: %.2f", mpg);
System.out.print("\nContinue? 1 = yes, 2 = no: ");
restart = input.nextInt();
}
mpg = milesTotal / gasTotal;
System.out.printf("Total Miles: %.2f\nTotal Gallons: %.2f\nTotal MPG: %.2f\n", milesTotal, gasTotal, mpg);
}
}
Testmileagetest.java:
package gasMileage;
public class Testmileagetest
{
/**
* @param args
*/
public static void main(String[] args)
{
Testmileage test = new Testmileage();
test.testCalculate();
}
}
再次感谢!
【问题讨论】:
-
+1,因为您显然正在尝试正确学习和理解这些概念。
-
呃...我想我有点像个怪胎...给我一个物理作业,看看我用谷歌搜索这个问题的速度有多快...给我一个编码分配并观察我花了多长时间搜索它工作的原因或为什么它更好。到目前为止,我已经大约 3 天了,我一直在思考/寻找这个答案。
-
我很惊讶您没有学习 C++ 中的封装。它在那里的应用与在 Java 或任何其他 OO 语言中的应用一样多。我会让你的 C++ 老师难过 ;-)。
-
顺便说一句,+1 表示您的态度。我以前看过这个问题,我尊重你处理这个问题的方式。其他人只是采取了这样的态度“setter/getter 是荒谬的,浪费时间,你为什么要使用它们。我的教授一定是个混蛋,强迫我做一些荒谬的事情。”你清楚地知道这一定是有原因的,否则他们不会那样教它,你是绝对正确的。
标签: java oop constructor getter-setter