简体   繁体   English

如何将类对象用于其他对象?

[英]How can I use a class object into other?

I'll give an example to explain what i need, imagine a Lamborghini database: 我将举一个例子来解释我的需求,想象一个Lamborghini数据库:

CarSeries (int identifier, String name)
CarModel (int modelIdentifier, String modelName, CarSeries (int identifier, String name);

Now I want to create an array where I should be able to directly create a carModel , for example a carModel (123, Gallardo, 345, Superleggera) 现在,我想创建一个数组,在该数组中应该可以直接创建carModel ,例如carModel (123, Gallardo, 345, Superleggera)

So I create the array for three cars: 因此,我为三辆车创建了数组:

CarModel carmodels[] = new CarModel[3];

And have to initialize them. 并且必须初始化它们。 Initializing a similar array 0 position for CarSeries is as simple as 为CarSeries初始化相似的数组0位置非常简单

carseries[0] = new CarSeries(0,"");

but I can't find the way to initialize a CarModel, and neither to use getters and setters for, for example, set carModel carSeries's identifier. 但是我找不到初始化CarModel的方法,也找不到将getter和setter用于设置carModel carSeries的标识符的方法。


Update: Based in RAZ_Muh_Taz's code (very helpful, thanks!) I've been able to initialize it! 更新:基于RAZ_Muh_Taz的代码(非常有帮助,谢谢!),我已经能够对其进行初始化! This way: carmodels[0] = new CarModel (0,"", new CarSeries(0,"")); 这样:carmodels [0] = new CarModel(0,“”,new CarSeries(0,“”)); Now i want to set a console output asking for CarSeries identifier, how should code ask for input? 现在我想设置一个控制台输出,询问CarSeries标识符,代码应如何询问输入? How can I use the getter now? 我现在如何使用吸气剂?

Update2: Finally just created a new array for each Person and just update both arrays simultaneously with same count to point both desired groups of data. Update2:最后,为每个人创建了一个新数组,并以相同的计数同时更新两个数组以指向两个所需的数据组。

Your third parameter is a CarSeries , so just pass that in. 您的第三个参数是CarSeries ,因此只需将其传入即可。

CarSeries accord = new CarSeries (0, "accord");
CarModel honda = new CarModel(0, "honda", accord);

Or in-line. 或在线。

CarModel honda = new CarModel(0, "Honda", new CarSeries (0, "Accord"));

Whether that is in an array or not is not a problem, though, I think you should ideally store some collection of series for each model, not only one. 不过,无论是将其放置在数组中,都不是问题,我认为您应该为每个模型(而不只是一个模型)存储一些系列的集合。

You need to create a new object when passing it to your parameter in your CarModel contructor. 在将新对象传递到CarModel构造函数中的参数时,需要创建一个新对象。

CarModel carmodels[] =  new CarModel[3];
carseries[0] = CarModel (0, "Lexus", new CarSeries (0, "RX350"));
carseries[1] = CarModel (1, "Toyota", new CarSeries (1, "Tacoma"));
carseries[2] = CarModel (2, "Honda", new CarSeries (2, "CR-V"));

You can do it the following way provided you have the appropriate constructors for CarModel and CarSeries defined. 如果已经为CarModel和CarSeries定义了适当的构造函数,则可以按照以下方式进行操作。

CarModel carmodels[] =  new CarModel[3] {
    new CarModel(1, "gallardo", new CarSeries(100, "xyz")), 
    new CarModel(2, "veneno", new CarSeries(200, "abc"), 
    new CarModel(3, "Huracan", new CarSeries(300, "pqr") 
};

Once you have declared the carmodel array, you have to access to the index of the array to insert each one car models. 声明carmodel数组后,您必须访问该数组的索引以插入每个汽车模型。

CarModel carModels[] = new CarModel[3];

carModels[0] = new CarModel(0, "model1", new CarSeries(0, "serie1"));
carModels[1] = new CarModel(1, "model2", new CarSeries(1, "serie2"));
carModels[2] = new CarModel(2, "model3", new CarSeries(2, "serie3"));

You can also declare the CarSeries before 您也可以在之前声明CarSeries

CarSeries carSerie1 = new CarSeries(0, "serie1");
CarSeries carSerie2 = new CarSeries(1, "serie2");
CarSeries carSerie3 = new CarSeries(2, "serie3");

and use the reference in the constructor of the CarModel 并在CarModel的构造函数中使用引用

carModels[0] = new CarModel(0, "model1", carSerie1);
carModels[1] = new CarModel(1, "model2", carSerie2);
carModels[2] = new CarModel(2, "model3", carSerie3);

You have to take into account that you have to declare the constructors properly or the code won´t compile 您必须考虑到必须正确声明构造函数,否则代码将无法编译

You'd bether to use Map interface. 您还可以使用Map界面。

Map<Object, Object>yourMapName = new HashMap<Object, Object>();

But I recommend to you to read much more for collections and their hierarchy . 但是我建议您阅读有关集合及其层次结构的更多信息

First of all you cannot instantiate your model like that> 首先,您不能像这样实例化模型>
What you are doing is: instantiate CarModel with name carmodels[] 你在做什么是:实例CarModel与名carmodels[]

You can do a arraylist 你可以做一个数组列表

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

相关问题 如何创建动画类以在 android 中的其他活动中使用? - How can I create an animation class to use in other activities in android? 如何将TextView声明为全局变量以在其他类中使用 - How can I declare TextView as global variable to use in other class 我可以告诉 Java 使用 Object 以外的类作为默认基类吗? - Can I tell Java to use a class other than Object as default base class? 如何在一个类中使用静态对象,以及如何在其他类中继承静态对象 - How to use static objects in a class and how can we inherit the static object in other class 如何在 JCombobox 中使用其他 class 中的 Object - How to use Object from other class in a JCombobox How can I copy existing class object to other class new class object without creating copy of original object(in memory) in Java - How can I copy existing class object to other class new class object without creating copy of original object(in memory) in Java 如何将对象中的 Arraylist 用于另一个类中的方法 - How can I use an Arraylist in an object for a method in another class 如何将第三方类对象用作Hashmap密钥? - How can I use a third party Class Object as Hashmap Key? 如何在参数对象类中使用@CookieValue? - How can I use @CookieValue in a Parameter Object Class? 如何使用Class对象作为参数化类型? - How can I use a Class object as a parameterized type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM