java中setter什么样子_在java中setter和getter的点是什么?

参见英文答案 >

Why use getters and setters?                                    38个答案                            请原谅长度,但这里有两个程序,两者完全相同,但一个与一个没有setters,getter和constructor。

我已经采取了一个基本的C类,不记得任何这些从它,在这一刻,我没有看到他们的点,如果任何人可以解释他们在lamen的话我很感激它。在他们似乎只是空间浪费,使我的代码看起来更长的时刻,但老师说,他们是重要的(到目前为止)。

提前致谢!现在这里的代码:

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和getters的人:

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();

}

}

再次感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值