java代码块流行风格 java代码块的用处(12)


下面我写一下car的类来描述一个编程风格好的java类应该是怎样的 。当然这仅仅是一个小例子(apart from selector and mutator methods),仅仅是在考虑JAVA编程风格上一个参考而已 。
import java.awt.Color;
/**
* This is a class representing cars. A car has certain features, such
* as color, age, number of doors etc and a car can be repainted,
* the tank can be filled etc.
*
* @author Jerry Meng
* @version %I%, %G%
*/
public class Car {
/**
* The maximum size of the tank in litres.
*/
private static final double TANK_SIZE = 100.0;
/**
* The color of the car.
*/
private Color color;
/**
* The age of the car.
*/
private int age;
/**
* The number of doors of the car.
*/
private int doors;
/**
* The amount of gasoline in the tank.
*/
private double gasoline;
/**
* Class constructor, which constructs a brand new, black car with
* five doors and a full tank.
*/
public Car() {
this(Color.black, 0, 5, TANK_SIZE);
}
/**
* Class constructor specifying the color, age, number of doors
* and litres of gasoline
*
* @param colorThe color of the car
* @param ageThe age of the car
* @param doorsThe number of doors
* @param kmKilometres driven
* @param gasoline The litres of gasoline
*/
public Car(Color color, int age, int doors, double gasoline) {
this.color = color;
this.age = age;
this.doors = doors;
this.gasoline = gasoline;
}
/**
* Returns the color of the car
*/
public Color getColor() {
return color;
}
/**
* Repaints the car (i.e. changes its color)
*/
public void setColor(Color color) {
this.color = color;
}
/**
* Returns the age of the car
*/
public int getAge() {
return age;
}
/**
* Returns the number of doors of the car
*/
public int getDoors() {
return doors;
}
/**
* Returns the amount of gasoline in the tank
*/
public double getGasoline() {
return gasoline;
}
/**
* Fills the tank. The amount of gasoline cannot exceed
【java代码块流行风格 java代码块的用处】* the size of the tank. In that case, the tank will be
* filled to the maximum and the rest will run out in
* the sand.
*
* @param gasThe amount of gasoline to put in the tank
*/
public void setGasoline(double gas) {
if(gasoline + gas = TANK_SIZE)
gasoline+=gas;
else
gasoline = TANK_SIZE;
}
}
关于java代码块流行风格和java代码块的用处的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读