简体   繁体   English

如何在类数组中分配元素

[英]How to Assign Elements in Class Array

I have a class named Tire and it has a function.我有一个名为Tire的类,它有一个函数。 I call it in Car class.我在Car课上称之为。 A car has 4 tires, so I need to declare the tires as an array in Car class so I can call the function that is implemented in Tire class.一辆汽车有 4 个轮胎,所以我需要在Car类中将轮胎声明为一个数组,以便我可以调用在Tire类中实现的函数。 How exactly can I do it?我该怎么做?

class Tire {
    public void pumpAir(int psi) {}
}

public class Car {
    private Tire[] tires = new Tire[4];
}

public static void main(String[] args) {

    Car car = new Car();

    car.tires ... // how to call 3rd tire and pump it?
}

One option is to create a getter for it:一种选择是为它创建一个吸气剂:

public class Car {
    private Tire[] tires = new Tire[4];

    public Tire getTire(int i) {
        if (i >= 0 && i < tires.length) {
           return tires[i];
        }
        return null;
    }
}

And in your main code:在你的主要代码中:

Tire t = car.getTire(3);
if (t != null) {
   t.pumpAir(42);
}

Create a getter for the tires and access it using subscript as its private .为轮胎创建一个 getter 并使用下标作为其私有访问它。
Or change the access specifier to public and access it using subscript operator.或者将访问说明符更改为public并使用下标运算符访问它。

public class Car {
    private Tire[] tires = new Tire[4];

    public Tire getTire(int number) {
         if(number >= 0 && number < tires.length) {
              return tires[number];
         } else {
              return null;
         }
    }

}

public static void main(String[] args) {

    Car car = new Car();

    car.getTire(2) ... // how to call 3rd tire and pump it?
}
 public class Tire 
    {
        public void pumpAir(int psi) {
                 System.out.println("Tire number " + psi + " pumped.");
         }
    }



 public class Car {
        private Tire[] tires = new Tire[4];



 public Car()
    {
     for(int i=0;i<4;i++)
         this.tires[i]=new Tire(); 
    }

public Tire[] getTiers()

   {
     return tires;
   }
}

public static void main(String[] args) {



 Car car = new Car();

// how to call 3rd tire and pump it?
car.getTiers()[2].pumpAir(2 + 1);

}

The best option is to create a getter in the Car class最好的选择是在 Car 类中创建一个 getter

public class Car 
{
    private Tire[] tires = new Tire[4];

    public Tire[] getTires() 
    {
        return tires;
    }
}

Then in main, you can access the 3rd tire by然后在主要,您可以通过以下方式访问第三个轮胎

car.getTires()[2].pumpAir(someInt);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数组元素分配给私有变量并在另一个类中使用它们? - How to assign array elements to private variable and use them in another class? 如何在Java中分配数组中的元素数? - How to assign the number of elements in an array in Java? 尝试将值分配给数组元素并在另一个类中返回它以使用但不起作用 - Trying to assign value to array elements and return it in another class to work with but not working 如何为类数组对象分配值? - How to assign value for a class array object? 如何将创建的对象分配到不同类中的数组中 - How to assign objects created into an array in different class 如何创建一个程序,为数组元素随机分配0和1并为其找到十进制值 - How to create a program for randomly assign 0 and 1 to array elements and find the decimal value for it 将字符分配给不是int,string,double等类的数组元素。 - assign characters to elements of an array which isnt of the int, string, double, etc. class 将具有Integer元素的Object数组分配给Integer数组 - Assign Object array with Integer elements to Integer array 如何在Java中编辑类类型数组中的元素? - How to edit elements in array of class type in java? 如何使用Scala / Java中的索引从另一个数组分配一个数组的元素? - How to assign elements of an array from another array using indexes in Scala/Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM