我有一个叫做制造商的类,在其中我有一系列汽车。在我的主类中,我声明了一个Manufacturer对象数组。汽车具有模型,价格等属性。如何访问我在Car类中声明的getter方法?假设我的数组已填充。
public class Main
{
public static void main(String[] args)
{
Manufacturer[] objManufacturer = new Manufacturer[10];
System.out.println(objManufacturer[0].getManufacturer());
// how do a print out the year of the first car in the first manufacturer?
}
}
汽车类
public class Car
{
private int year; // year of the car
private String model; // model of the car
private double price; // price of the car
private int kmTravelled; // the KM travelled by the car
private String extras; // extra information about the car
public Car()
{
}
// Parameterized constructor
public Car(int year, String model, double price,int kmTravelled, String extras )
{
this.year = year;
this.model = model;
this.price = price;
this.kmTravelled = kmTravelled;
this.extras = extras;
}
// getter method for Year
public int getYear()
{
return year;
}
}
类制造商
public class Manufacturer
{
private String manufacturerName; // manufacturer of the car
private Car[] cars; // an array to store the cars
private int numOfCars;
//default constructor initialize car array size to 10
//and the current numOfCar to 0
public Manufacturer()
{
numOfCars = 0;
cars = new Car[10];
}
// getter methods for manufacturer
public String getManufacturerName()
{
return manufacturerName;
}
public void setManufacturer(String aManufacturer)
{
manufacturerName = aManufacturer;
}
}
答案 0 :(得分:0)
objManufacturer [0] .getCars()[0] .getYear()
在manufacterer类中为数组创建getter方法后:
public getCars(){return cars; }
答案 1 :(得分:0)
首先,你没有有效的制定者和吸气剂。
public class Manufacturer{
private String manufacturerName; // manufacturer of the car
private Car[] cars; // an array to store the cars
private int numOfCars;
//default constructor initialize car array size to 10
//and the current numOfCar to 0
public Manufacturer(){
numOfCars = 0;
cars = new Car[10];
}
// getter methods for manufacturer
public String getManufacturerName(){
return manufacturerName;
}
public void setManufacturerName(String aManufacturer){
manufacturerName = aManufacturer;
}
public Car[] getCars(){
return cars;
}
public void setCars(Car[] cars){
this.cars = cars;
}
}
主要方法:
public static void main(String[] args){
Manufacturer[] objManufacturer = new Manufacturer[10];
//Assuming that you have populated data properly
System.out.println(objManufacturer[0].getCars()[0].getYear());
//Gives year of first car of first manufacturer
}
答案 2 :(得分:0)
首先,你没有在你的Car类中定义getter和setter,所以这样做:
public class Car
{
private int year; // year of the car
private String model; // model of the car
private double price; // price of the car
private int kmTravelled; // the KM travelled by the car
private String extras; // extra information about the car
public Car()
{
}
// Parameterized constructor
public Car(int year, String model, double price,int kmTravelled, String extras )
{
this.year = year;
this.model = model;
this.price = price;
this.kmTravelled = kmTravelled;
this.extras = extras;
}
// getter method for Year
public int getYear()
{
return year;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getKmTravelled() {
return kmTravelled;
}
public void setKmTravelled(int kmTravelled) {
this.kmTravelled = kmTravelled;
}
public String getExtras() {
return extras;
}
public void setExtras(String extras) {
this.extras = extras;
}
public void setYear(int year) {
this.year = year;
}
}
然后您必须在制造商中创建 addCars 和 getNumOfCars :
public class Manufacturer
{
private String manufacturerName; // manufacturer of the car
private Car[] cars; // an array to store the cars
private int numOfCars;
//default constructor initialize car array size to 10
//and the current numOfCar to 0
public Manufacturer()
{
numOfCars = 0;
cars = new Car[10];
}
// getter methods for manufacturer
public String getManufacturerName()
{
return manufacturerName;
}
public void setManufacturer(String aManufacturer)
{
manufacturerName = aManufacturer;
}
public Manufacturer getManufacturer() {
return this;
}
public Car[] getCars() {
return cars;
}
public void addCar(Car car){
cars[numOfCars] = car;
numOfCars++;
}
public int getNumOfCars() {
return numOfCars;
}
}
然后你可以从你的主要课程中获取:
public class Main {
public static void main(String[] args) throws ParseException, IOException, InterruptedException {
Manufacturer[] objManufacturer = new Manufacturer[10];
Manufacturer obj1 = new Manufacturer();
objManufacturer[0] = obj1;
System.out.println(objManufacturer[0].getManufacturer());
Car car1 = new Car(2014, "Gol", 20000, 0, null);
obj1.addCar(car1);
int numOfCars = obj1.getNumOfCars();
Car[] cars = obj1.getCars();
for (int i = 0; i < numOfCars; i++) {
Car car = cars[i];
System.out.println(car.getModel());
}
}
}
就是这样!