Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何创建具有不同实例的对象数组?_Java_Arrays_Class - Fatal编程技术网

Java 如何创建具有不同实例的对象数组?

Java 如何创建具有不同实例的对象数组?,java,arrays,class,Java,Arrays,Class,我有一个java应用程序,它有以下几个类:Car和Phone。每个类都有方法connectToBluetooth(),该方法打印一条关于类与蓝牙连接的消息 在另一个类中,我想创建一个对象数组,在其中添加已创建的每个对象的实例。然后,我想访问与每个实例相关的connectToBluetooth方法。我为每个类创建了一个实例: 我想创建这两个实例的数组,并访问与每个类相关的connectToBluetooth方法。(施工人员询问业主和设备的颜色) 您可以创建一个对象数组,在该数组中可以同时添加对象类

我有一个java应用程序,它有以下几个类:
Car
Phone
。每个类都有方法
connectToBluetooth()
,该方法打印一条关于类与蓝牙连接的消息

在另一个类中,我想创建一个对象数组,在其中添加已创建的每个对象的实例。然后,我想访问与每个实例相关的
connectToBluetooth
方法。我为每个类创建了一个实例:

我想创建这两个实例的数组,并访问与每个类相关的
connectToBluetooth
方法。(施工人员询问业主和设备的颜色)


您可以创建一个
对象数组
,在该数组中可以同时添加
对象
类型,这不是一个好方法。更好的方法是为
Phone
Car
创建一个超级类型,并创建该类型的数组(超级类型可以是接口或类)


例如,创建一个名为
BlueToothDevice
,并将该类扩展到Phone和Car。然后创建一个包含
BlueToothDevice
类型的数组,并同时添加这两种类型。

您可以创建一个包含
对象的数组,在该数组中可以同时添加
对象
类型,这不是一个好方法。更好的方法是为
Phone
Car
创建一个超级类型,并创建该类型的数组(超级类型可以是接口或类)


例如,创建一个名为
BlueToothDevice
,并将该类扩展到Phone和Car。然后创建一个Bluetooth设备数组,键入并添加这两个设备。

这是实际的解决方案,其中Bluetooth是实现的接口:

Bluetooth[] bluetooth= new Bluetooth[2];
bluetooth[0] = new Car("John", "blue"); 
bluetooth[0].connectToBluetooth(); //-> prints message coresponding to Car class 
bluetooth[1] = new Phone("Susan", "black");
bluetooth[1].connectToBluetooth(); //-> prints message coresponding to Phone class

这是实际的解决方案,其中蓝牙是实现的接口:

Bluetooth[] bluetooth= new Bluetooth[2];
bluetooth[0] = new Car("John", "blue"); 
bluetooth[0].connectToBluetooth(); //-> prints message coresponding to Car class 
bluetooth[1] = new Phone("Susan", "black");
bluetooth[1].connectToBluetooth(); //-> prints message coresponding to Phone class
对象[]arr=新对象[] 您可以使用
对象的数组
,但在这种情况下,您必须在调用
connectToBluetooth()之前对具体实例进行大小写:


蓝牙[]arr=新蓝牙[] 更正确的方法是使用
connectToBluetooth()
方法声明一个interace,并将其用作数组类型:

interface Bluetooth {
    void connectToBluetooth();
}

class Car implements Bluetooth {} 
class Phone implements Bluetooth {}

Bluetooth[] arr = { new Car("John", "red"), new Phone("Susan","black") };

for (Bluetooth bluetooth : arr)
    bluetooth.connectToBluetooth();
对象[]arr=新对象[] 您可以使用
对象的数组
,但在这种情况下,您必须在调用
connectToBluetooth()之前对具体实例进行大小写:


蓝牙[]arr=新蓝牙[] 更正确的方法是使用
connectToBluetooth()
方法声明一个interace,并将其用作数组类型:

interface Bluetooth {
    void connectToBluetooth();
}

class Car implements Bluetooth {} 
class Phone implements Bluetooth {}

Bluetooth[] arr = { new Car("John", "red"), new Phone("Susan","black") };

for (Bluetooth bluetooth : arr)
    bluetooth.connectToBluetooth();

请发布您迄今为止所做的尝试。我想您需要的是为这两种类定义一个公共接口,一个ITetar,一个调用connect to BlueTooth的接口数组。告诉我,如果这能给你一个idea@Victor,我已经创建了一个接口,并且在这两个类中我都实现了该接口。现在,在第三步中,你必须使用它。你能分享更多你的代码吗?@Victor,我已经发布了解决方案,谢谢!请发布您迄今为止所做的尝试。我想您需要的是为这两种类定义一个公共接口,一个ITetar,一个调用connect to BlueTooth的接口数组。告诉我,如果这能给你一个idea@Victor,我已经创建了一个接口,并且在这两个类中我都实现了该接口。现在,在第三步中,你必须使用它。你能分享更多你的代码吗?@Victor,我已经发布了解决方案,谢谢!是的,你明白了:)如果
BlueToothDevice
永远不会被实例化,那么我宁愿创建一个InterfaceTrue。界面会更好。就拿它作为一个例子来解释他,我已经提到超级类型可以是一个接口或一个类。是的,你得到了:)如果
BlueToothDevice
永远不会被实例化,那么我宁愿创建一个InterfaceTrue。界面会更好。只是以它为例来解释他,我已经提到超级类型可以是接口或类。