请原谅它的长度,但是这里有两个程序,两者完全相同,但是一个有一个没有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(); } }
现在,对于那些没有二传手的人:
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(); } }
再次感谢!
无论哪种语言,getter和setter的目的都是隐藏基础变量。这样,您可以在尝试设置值时添加验证逻辑-例如,如果您有一个出生日期字段,则可能只希望将该字段设置为过去的某个时间。如果该字段是公共可访问和可修改的,则不能强制执行此操作-您需要使用getter和setter。
即使您不需要任何验证,将来也可能需要它。现在,编写getter和setter意味着接口保持一致,因此更改现有代码不会中断。