![ืจืฆืฃ ืคืขืืืืช ืืืืื ืืฆืืจืช ืืืืืืงื - 1](https://cdn.codegym.cc/images/article/89a4c42b-2587-43f2-8b1e-01cee9e32506/800.jpeg)
new
ืืืื ืืืื :) ืืื ื ืืืจ ืขื ืื ืฉืงืืจื ืืชืื ืืืืฉื ืืืืื ืช ื-Java ืืฉืื ืื ื ืืืชืืื, ืืืฉื:
Cat cat = new Cat();
ืืืืจื ื ืขื ืื ืืขืืจ, ืืื ืืืงืจื ืฉื ืืืืจ ืื:
- ืจืืฉืืช, ืืืืจืื ืืืืกืื ืืืืืืืงื ืืืงืฆื.
- ืืืืจ ืืื, ืืืื ืช ื-Java ืืืฆืจืช ืืคื ืื ืืืืืืืงื (ืืืงืจื ืฉืื ื ืืืชืืืืกืืช ืืื Cat cat).
- ืืืกืืฃ, ืืฉืชื ืื ืืืืชืืืื ืืืงืื ืกืืจืืงืืืจ ื ืงืจื (ืื ืื ื ืืืืืื ืืืกืชืื ืขื ืชืืืื ืื ืืืชืจ ืคืืจืื).
![ืจืฆืฃ ืคืขืืืืช ืืืืื ืืฆืืจืช ืืืืืืงื - 2](https://cdn.codegym.cc/images/article/512880ad-4591-4e57-99f2-31336164a0ef/800.jpeg)
public class Vehicle {
public static int vehicleCounter = 0;
private String description = "Vehicle";
public Vehicle() {
}
public String getDescription() {
return description;
}
}
public class Truck extends Vehicle {
private static int truckCounter = 0;
private int yearOfManufacture;
private String model;
private int maxSpeed;
public Truck(int yearOfManufacture, String model, int maxSpeed) {
this.yearOfManufacture = yearOfManufacture;
this.model = model;
this.maxSpeed = maxSpeed;
Vehicle.vehicleCounter++;
truckCounter++;
}
}
ืืืืืงื Truck
ืืื ืืืฉืื ืฉื ืืฉืืืช ืขื ืฉืืืช ืืืืืฆืืื ืืช ืืฉื ื, ืืืื ืืืืืืจืืช ืืืจืืืช ืฉืื. ืืขืช ืื ื ืจืืฆืื ืืืฆืืจ ืืืืืืงื ืืื ืืื:
public class Main {
public static void main(String[] args) throws IOException {
Truck truck = new Truck(2017, "Scania S 500 4x2", 220);
}
}
ืืืืื ืช Java, ืืชืืืื ืืืจืื ืื:
-
ืืืืจ ืืจืืฉืื ืฉืงืืจื ืืื ืฉืืืฉืชื ืื ืืกืืืืื ืฉื
Vehicle
ืืืืืงื ืืืืชืืืื . ืื, ืืืจืชื ืืืืชืVehicle
, ืืTruck
. ืืฉืชื ืื ืกืืืืื ืืืืชืืืื ืืคื ื ืงืจืืื ืฉื ืื ืืื, ืืื ืืชืืื ืืืืืงืช ืืื. ืืืื ื ื ืกื ืืืืช ืืืช. ืื ืื ื ืืืืืจืื ืืชvehicleCounter
ืืฉืื ืืืืืงืVehicle
ืฉืืื ื-10 ืืื ืกืื ืืืฆืื ืืืชื ืื ืืื ืืืVehicle
ืืืTruck
.public class Vehicle { public static int vehicleCounter = 10; private String description = "Vehicle"; public Vehicle() { System.out.println(vehicleCounter); } public String getDescription() { return description; } } public class Truck extends Vehicle { private static int truckCount = 0; private int yearOfManufacture; private String model; private int maxSpeed; public Truck(int yearOfManufacture, String model, int maxSpeed) { System.out.println(vehicleCounter); this.yearOfManufacture = yearOfManufacture; this.model = model; this.maxSpeed = maxSpeed; Vehicle.vehicleCounter++; truckCount++; } }
ืฉืื ื ืืืืื ื ืืช ืืืฆืืจื println ืืืฉ ืืชืืืืช ืืงืื ืกืืจืืงืืืจ
Truck
ืืื ืืืืืช ืืืืืื ืฉืืฉืืืช ืฉื ืืืฉืืืช ืขืืืื ืื ืืืืื ืืืฉืจvehicleCounter
ืืืฆืืช.ืืื ื ืืชืืฆืื:
10 10
-
ืืืืจ ืืชืืื ืืืฉืชื ืื ืืกืืืืื ืฉื ืืืืงืช ืืื, ืืชืืื ืืืฉืชื ืื ืืกืืืืื ืฉื ืืืืงืช ืืืื. ืืืงืจื ืฉืื ื ืื ืืชืืื
truckCounter
ืฉืTruck
ืืืืชื.ืืืื ื ืขืฉื ื ืืกืื ื ืืกืฃ ืฉืื ื ื ืกื ืืืฆืื ืืช ืืขืจื ืฉื
truckCounter
ืืชืืTruck
ืืื ืื ืืคื ื ืืชืืื ืืฉืืืช ืืืืจืื:public class Truck extends Vehicle { private static int truckCounter = 10; private int yearOfManufacture; private String model; private int maxSpeed; public Truck(int yearOfManufacture, String model, int maxSpeed) { System.out.println(truckCounter); this.yearOfManufacture = yearOfManufacture; this.model = model; this.maxSpeed = maxSpeed; Vehicle.vehicleCounter++; truckCounter++; } }
ืืคื ืฉืืชื ืืืื ืืจืืืช, ืืขืจื 10 ืืืจ ืืืงืฆื ืืืฉืชื ื ืืกืืื ืฉืื ื ืืืฉืจ
Truck
ืืื ืื ืืชืืื. -
ืขืืืื ืื ืืืืข ืืืื ืืื ืืื! ืืชืืื ืืืฉืชื ืื ื ืืฉื.โฏืืืฉืชื ืื ืืื ืกืืืืื ืฉื ืืืืงืช ืืื ืืืืชืืืื ืฉืืืฉืืช.โฏืืคื ืฉืืชื ืืืื ืืจืืืช, ืชืืจืฉื ืืกืืืช ืืืืคื ืืฉืืขืืชื ืืช ืชืืืื ืืฆืืจืช ืืืืืืืงื, ืืื ืืื ืฉืื ืืืจ ืฉืืชื ืืืื ืืขืฉืืช ืืงืฉืจ ืืื: ืืชื ืจืง ืฆืจืื ืืฉื ื ืืื ืืืจืื ืืชืื ืืช :)
ืื ืืกืื, ืื ื ืืืืืื ืืืงืฆืืช ืขืจื ืจืืฉืื ื ืืืฉืื ืืืฉืชื ื
description
ืืืืชืVehicle
, ืืืืืจ ืืื ืืฉื ืืช ืืืชื ืืื ืื.public class Vehicle { public static int vehicleCounter = 10; private String description = "Initial value of the description field"; public Vehicle() { System.out.println(description); description = "Vehicle"; System.out.println(description); } public String getDescription() { return description; } }
ืืืื ื ืจืืฅ ืืช
main()
ืืฉืืื ืฉืื ื ืฉืืืฆืจืช ืืฉืืืช:public class Main { public static void main(String[] args) throws IOException { Truck truck = new Truck(2017, "Scania S 500 4x2", 220); } }
ืื ื ืืงืืืื ืืช ืืชืืฆืื ืืืื:
Initial value of the description field Vehicle
ืื ืืืืื ืฉืืืฉืจ
Vehicle
ืืื ืื ืืชืืืdescription
ืืฉืื ืืืจ ืืืงืฆื ืขืจื. -
ืกืืฃ ืกืืฃ, ืืืืข ืืืื ืืื ืืื!โฏืืืชืจ ืืืืง, ืืืืข ืืืื ืืื ืื ืืืืงืืช ืืืกืืก. ืืื ืืืคืขื ืืฉืื ืืจืืืขื ืฉื ืชืืืื ืืฆืืจืช ืืืืืืืงื.
ืื ืื ืื ืงื ืืืืช. ืืืื ื ื ืกื ืืืืฆืื ืฉืชื ืฉืืจืืช ืืงืื ืกืืื: ืืืช ืืชืื
Vehicle
ืืื ืื ืืืืงื ืืืกืืกืืช, ืืฉื ืืื ืืชืืTruck
ืืื ืื. ืขืืื ื ืืืืืช ืืฉืืื ืขืื ืฉืืงื ืืคื ืืVehicle
ืืืฆื ืชืืืื:public Vehicle() { System.out.println("Hello from the Vehicle constructor!"); } public Truck(int yearOfManufacture, String model, int maxSpeed) { System.out.println("Hello from the Truck constructor!"); this.yearOfManufacture = yearOfManufacture; this.model = model; this.maxSpeed = maxSpeed; Vehicle.vehicleCounter++; truckCounter++; }
ื ืจืืฅ ืืช
main()
ืืฉืืื ืฉืื ื ืื ืกืชืื ืขื ืืชืืฆืื:Hello from the Vehicle constructor! Hello from the Truck constructor!
ืึฐืขืึผืึถื. ืื ืืืืจ ืฉืื ืื ื ืื ืืืขืื :) ืืืื ื ืืฉืื ืืืื.
-
ืขืืฉืื ืืืืข ืืืื ืืืชืืื ืฉื ืืฉืืืช ืืื ืกืืืืื ืฉื ืืืชืช ืืืื, ืืืืืจ
Truck
ืืืืชื ืฉืื ื. ืืฉืืืช ืืื ืืชืื ืืืืืงื ืืืืืฆืขืช ืื ืืืืชืืืื ืขื ืืฉืื ืืืืืฉื! ืืคืชืืข, ืืื ื ืืื :) ืฉืื, ื ืขืฉื ืืืืงื ืคืฉืืื - ืืืืืง ืืื ืขื ืืืืงืช ืืื: ื ืืฆืข ืืืื ืขืจื ืจืืฉืื ื ืืืฉืชื ืmaxSpeed
ืืืงืื ืกืืจืืงืืืจTruck
ื ืืืืง ืฉืืขืจื ืืืงืฆื ืืคื ื ืฉืืงืื ืกืืจืืงืืืจ ืืชืืื:public class Truck extends Vehicle { private static int truckCounter = 10; private int yearOfManufacture; private String model; private int maxSpeed = 150; public Truck(int yearOfManufacture, String model, int maxSpeed) { System.out.println("Initial value of maxSpeed = " + this.maxSpeed); this.yearOfManufacture = yearOfManufacture; this.model = model; this.maxSpeed = maxSpeed; Vehicle.vehicleCounter++; truckCounter++; } }
ืคืื ืืกืืฃ:
Initial value of maxSpeed = 150
ืืคื ืฉืืชื ืืืื ืืจืืืช,โฏ ืืืฉืจ
Truck
โฏืืงืื ืกืืจืืงืืืจ ืืชืืื,maxSpeed
โฏืื ืืืจ ืฉืืื ื-150! -
ืืงืื ืกืืจืืงืืืจ ืฉื
Truck
ืืืชืช ืืืื ื ืงืจื.ืืจืง ืืฉืื ืื, ืืืจืื ืืืืื, ืืืงืจื ืืื ืื ืฉื ืืืืืงื ืฉืื ื ืืืืฆืจืื!
ืจืง ืืฉืื ืืฉืืฉื ืืืงืฆื ืืฉืืืช ืืช ืืขืจืืื ืฉืื ื ืืขืืืจืื ืืืืขืื ืื ืืืฉืืืช ืฉืื ื.
ืืคื ืฉื ืืชื ืืจืืืช, "ืื ืืืช" ืืฉืืืช, ืืืืืจ ืชืืืื ืืฆืืจืช ืืืืืืืงื, ืืื ื ืคืฉืื. ืืื ื ืจืื ืฉืคืืจืงื ื ืืช ืื ืืืืงืื ืืื ืงืื ืื :)
![ืจืฆืฃ ืคืขืืืืช ืืืืื ืืฆืืจืช ืืืืืืงื - 3](https://cdn.codegym.cc/images/article/4fc12ec4-0fc2-43f5-aabc-98b56fed09ef/800.jpeg)
GO TO FULL VERSION