Creating private properties
Class properties are often made private to restrict external access. For instance, in a Passport class, certain fields should be set only under specific conditions. You will make a car’s registration number private in the Car class to hide it.
Diese Übung ist Teil des Kurses
Introduction to Object-Oriented Programming in Java
Anleitung zur Übung
- Create a new
privateproperty calledvehicleNumberthat is aninttype.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
class Main {
static class Car {
public String color;
public String model;
public int year;
// Create private property "vehicleNumber"
____ ____ ____;
Car(String color, String model, int year) {
this.color = color;
this.model = model;
this.year = year;
this.vehicleNumber = 101189;
}
public void turnEngineOn() {
System.out.println("engine is on");
}
public int calculateMPG(int milesDriven, int gallonsUsed) {
return milesDriven / gallonsUsed;
}
}
public static void main(String[] args) {
}
}