集册 Java实例教程 使用构造函数创建Car类

使用构造函数创建Car类

欢马劈雪     最近更新时间:2020-01-02 10:19:05

788
汽车类有三个领域:

class Car {/* 来自 N o w J a v a . c o m - 时  代  Java*/

  String status;

  int speed;

  float temperature;


  Car(String inStatus, int inSpeed, float inTemperature) {

    status = inStatus;

    speed = inSpeed;

    temperature = inTemperature;

  }

  void checkTemperature() {

    if (temperature < -80) {

      status = "returning home";

      speed = 5;

    }// 来 自 时 代 J a v a

  }

  void showAttributes() {

    System.out.println("Status: " + status);

    System.out.println("Speed: " + speed);

    System.out.println("Temperature: " + temperature);

  }

}


public class Main {

  public static void main(String[] arguments) {

    Car spirit = new Car("exploring", 2, -80);

    spirit.showAttributes();

    System.out.println("Increasing speed to 3.");

    spirit.speed = 3;

    spirit.showAttributes();

    System.out.println(
展开阅读全文