代码之家  ›  专栏  ›  技术社区  ›  stack.name

访问其他类Java继承中的对象

  •  1
  • stack.name  · 技术社区  · 7 年前

    在我的程序中,我有类车和继承自类车的类车。我已经创建了两个汽车对象c和c2。现在我必须 sumFuel() 计算类中的方法,该方法对汽车对象使用的燃油进行求和。

    c、 燃油+c2。燃料当我在main中编写它时,它是有效的,但如何在class方法中做到这一点呢?我也在考虑做汽车对象的数组,但我不知道应该放在哪里以及如何在中引用它 Sumfoure()

    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()
       {
           
          
       }*/
    }
    
    3 回复  |  直到 4 年前
        1
  •  1
  •   Michal Lonski Steve    7 年前

    您不需要扩展类 Car 能够访问 fuel (the Calculation 类)。一般的模式是将字段设置为私有,如果需要,创建一个getter来公开它。所以看起来像:

    class Car {
      private int fuel;
    
      int getFuel() { 
        return fuel; 
      } 
    }
    

    后者在代码中可以称为: c1.getFuel() 。然后,如果要对所有汽车的燃油进行求和,请使用此访问器方法。

    考虑拥有汽车列表:

    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();
    

    或者以更古老的方式:

    List<Car> cars = Arrays.asList(c1, c2);
    int sum = 0;
    for(Car car : cars)
      sum += car.getFuel();
    

    额外的,而不是有方法 setVehicle 我会创建一个构造函数:

    class Car {
      Car(int speed, int nr, int fuel) {
        //... 
      }
    }
    

    如果你真的想用 计算 类,它可能如下所示:

    class Calculation {
      int calculateFuel(List<Cars> cars) {
        return cars.stream().mapToInt(Car::getFuel).sum()
      }
    }
    

    和在main中的用法(使用构造函数而不是 设置车辆 方法):

    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));
    

    此外,由于它不需要任何状态,因此它可能是 static 方法( static int calculateFuel(List<Cars> cars) ),并且不需要创建计算类的实例:

    //Calculation clac = new Calculation() - not needed
    int fuelSum = Calculation.calculateFuel(Arrays.asList(c1, c2));
    

    还有一件事-您创建了方法 display 。没关系,但有一种方法 toString() Object 类,您可以重写该类而不是创建新类:

    class Car {
      // (...)
      @Override
      public String toString() {
          System.out.println("Speed: "+speed );
          System.out.println("Nr: "+nr);
      }
    }
    

    其优点是,当您将其放入以下打印方法时,会自动调用它:

    Car c1 = new Car(200,5547,50);
    System.out.println(c1); //automatically called c1.toString() here
    
        2
  •  0
  •   Mohan Munisifreddy    7 年前

    代码片段说您是java新手: 尝试了解私有、公共和受保护访问修饰符的用法,以及如何使用构造函数用一些数据实例化对象。

    回到你的问题,试试这个:

    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;
        }
    }
    }
    

    不要只是试图找到这个问题的解决方案,还要尝试理解访问修饰符和构造函数。

        3
  •  0
  •   Kamil W    7 年前

    正如我所看到的,您非常熟悉Java。要解决您的问题,可以创建静态方法,无需创建对象即可调用该方法:

    public class Calculator {
    public void static calculate(int fuel1, int fuel2) {
    System.out.println(fuel1 + fuel2);
    }   
    }
    

    我建议您首先阅读一些关于java基础知识的书籍,因为您发布的代码非常糟糕(