在我的程序中,我有类车辆和类车继承车辆。我创造了两个Car的对象c和c2。现在我必须在Calculate类中创建sumFuel()方法,该类对Car的对象使用的燃料求和。 c.fuel + c2.fuel;当我在main中编写它时它可以工作,但是如何在类方法中执行此操作?我也在考虑做一系列Car的物品,但我不知道应该把它放在哪里以及如何在sumFuel()中引用它。
package javaapplication25;
public class JavaAplication25 {
public static void main(String[] args) {
Car c= new Car();
Car c2= new Car();
c.setVehicle(200,5547,50);
c.display();
c2.setVehicle(150,5087,100);
c2.display();
}
}
class Vehicle
{
int speed;
int nr;
void setVehicle(int speed, int nr)
{
this.speed=speed;
this.nr=nr;
}
void display()
{
System.out.println("Speed: "+speed );
System.out.println("Nr: "+nr);
}
}
class Car extends Vehicle
{
int fuel;
void setVehicle(int speed, int nr, int fuel)
{
super.setVehicle(speed, nr);
this.fuel=fuel;
}
void display()
{
super.display();
System.out.println("Fuel: "+ fuel);
}
}
class Calculate extends Car
{
int sum=0;
/*int sumFuel()
{
}*/
}
答案 0 :(得分:1)
You do not need to extend class Car
to be able to access fuel
(the Calculation
class). General pattern is to make the fields private and, if needed, create a getter which exposes it. So it looks like:
class Car {
private int fuel;
int getFuel() {
return fuel;
}
}
and latter in code you can call it: c1.getFuel()
. Then if you want to sum the fuels of all cars, you use this accessor method.
Consider having list of cars:
Car c1 = new Car();
c.setVehicle(200,5547,50);
Car c2 = new Car();
c2.setVehicle(150,5087,100);
int fuelSum = Stream.of(c1, c2).mapToInt(Car::getFuel).sum();
Or in more ancient way:
List<Car> cars = Arrays.asList(c1, c2);
int sum = 0;
for(Car car : cars)
sum += car.getFuel();
Additionaly instead of having method setVehicle
I would create a constructor:
class Car {
Car(int speed, int nr, int fuel) {
//...
}
}
If you really want to wrap this in the Calculation
class, it might look like this:
class Calculation {
int calculateFuel(List<Cars> cars) {
return cars.stream().mapToInt(Car::getFuel).sum()
}
}
and usage in main (with constructor instead of setVehicle
method):
Car c1 = new Car(200,5547,50);
Car c2 = new Car(150,5087,100);
Calculation clac = new Calculation();
int fuelSum = calc.calculateFuel(Arrays.asList(c1, c2));
Also since it does not require any state, it might be a static
method (static int calculateFuel(List<Cars> cars)
), and you will not need to create an instance of calculation class:
//Calculation clac = new Calculation() - not needed
int fuelSum = Calculation.calculateFuel(Arrays.asList(c1, c2));
And one more thing - you created methods display
. It is ok, but there is a method toString()
on the Object
class which you can override instead of creating new one:
class Car {
// (...)
@Override
public String toString() {
System.out.println("Speed: "+speed );
System.out.println("Nr: "+nr);
}
}
The adventage is that it is automatically called when you will put in into a printing method such as below one:
Car c1 = new Car(200,5547,50);
System.out.println(c1); //automatically called c1.toString() here
答案 1 :(得分:0)
As I see you are very beginer at Java. To solve your problem you can make static method which can be called without creating object:
public class Calculator {
public void static calculate(int fuel1, int fuel2) {
System.out.println(fuel1 + fuel2);
}
}
I recommend you at first read some book about java basics because code which you posted is very very bad. :(
答案 2 :(得分:0)
The code snippet says you are a novice in java: Try to understand the uses of private, public and protected access modifiers and how to use constructors to instantiate the object with some data.
Coming back to your question just try this:
public class JavaAplication25 {
public static void main(String[] args) {
Car c = new Car(200,5547,50);
Car c2 = new Car(150,5087,100);
c.display();
c2.display();
Car cars[] = {c,c2}; //array of cars
Calculate calculateFuel = new Calculate();
System.out.println("Total fuel:" + calculateFuel.sumFuel(cars));
}
private static class Vehicle {
private int speed;
private int nr;
Vehicle(int speed, int nr) {
this.speed=speed;
this.nr=nr;
}
protected void display() {
System.out.println("Speed: "+speed );
System.out.println("Nr: "+nr);
}
}
private static class Car extends Vehicle {
private int fuel;
Car(int speed, int nr, int fuel) {
super(speed, nr);
this.fuel=fuel;
}
protected void display() {
super.display();
System.out.println("Fuel: "+ fuel);
}
}
private static class Calculate {
private int sum = 0;
private int sumFuel(Car arrayOfCars[]) {
for (int i=0; i<arrayOfCars.length; i++) {
sum = sum + arrayOfCars[i].fuel;
}
return sum;
}
}
}
Don't just try to get the solution to this question but also try to understand the access modifiers and constructors.