未找到合适的构造函数(Java)
尝试使用聚合创建对象,但在使用数组创建对象时,错误显示没有合适的构造函数。我试图理解的是,当我不提供对象的信息时,为什么数组不能识别作为I实现的信息?它能够使用来自两个类的默认信息创建一个对象“no arg构造函数”未找到合适的构造函数(Java),java,arrays,object,aggregation,Java,Arrays,Object,Aggregation,尝试使用聚合创建对象,但在使用数组创建对象时,错误显示没有合适的构造函数。我试图理解的是,当我不提供对象的信息时,为什么数组不能识别作为I实现的信息?它能够使用来自两个类的默认信息创建一个对象“no arg构造函数” public class ComputerTest { public static void main(String[] args){ Computer c[] = new Computer[5]; c[0] = new Computer("OEM", "Basi
public class ComputerTest {
public static void main(String[] args){
Computer c[] = new Computer[5];
c[0] = new Computer("OEM", "Basic");
c[1] = new Computer("Dell", "Optiplex", "discrete", "PCIEx16", 375, "8-pin",16);
c[2] = new Computer("Lenova","IdeaCentre", "integrated", "N/A", 120, "N/A", 4);
c[3] = new Computer("HP", "Omen Obelisk", "discrete", "PCIEx8", 150,"6-pin", 8);
c[4] = new Computer("Lenovo" , "IdeaCentre", "integrated", "N/A", -110, "N/A", 2);
printComputers(c);
}
static void printComputers(Computer c[]){
for (int i=0; i<c.length; i++){
System.out.println(c[i].getInfo());
}
}
从这个类中聚合了哪些
public class Computer {
private String brand;
private String model;
private VideoCard VideoCard;
public Computer(){
this.setBrand("OEM");
this.setModel("Basic");
this.VideoCard = new VideoCard();
}
public Computer(String brand, String model, VideoCard VideoCard){
this.setBrand(brand);
this.setModel(model);
this.setVideoCard(VideoCard);
}
public void setBrand(String brand){
this.brand = brand;
}
public void setModel(String model){
this.model = model;
}
public void setVideoCard(VideoCard VideoCard){
this.VideoCard = VideoCard;
}
public String getBrand(){
return brand;
}
public String getModel(){
return model;
}
public VideoCard getVideoCard(){
return this.VideoCard;
}
public String getInfo(){
String info = String.format("%s " + "%s\n" + "Video Card Info:\n" + "%s\n", getBrand(), getModel(), VideoCard.getInfo());
return info;
}
public class VideoCard {
private String type;
private String connection;
private int power;
private String extPower;
private int memory;
public VideoCard(){
this.setType("integrated");
this.setConnection("N/A");
this.setPower(100);
this.setExtPower("N/A");
this.setMemory(1);
}
public VideoCard(String type, String connection, int power, String extPower, int memory){
this.setType(type);
this.setConnection(connection);
this.setPower(power);
this.setExtPower(extPower);
this.setMemory(memory);
}
public void setType(String type){
this.type = type;
}
public void setConnection(String connection){
this.connection = connection;
}
public void setPower(int power){
if(power>= 1 && power<=300){
this.power = power;
} if(power<1) {
this.power = 75;
} if(power>300){
this.power = 200;
}
}
public void setExtPower(String extPower){
this.extPower = extPower;
}
public void setMemory(int memory){
if(memory>=1 && memory<=12){
this.memory = memory;
} if(memory<1){
this.memory = 1;
} if(memory>12){
this.memory = 12;
}
}
public String getType(){
setType(this.type);
return type;
}
public String getConnection(){
setConnection(this.connection);
return connection;
}
public int getPower(){
setPower(this.power);
return power;
}
public String getExtPower(){
setExtPower(this.extPower);
return extPower;
}
public int getMemory(){
setMemory(this.memory);
return memory;
}
public String getInfo(){
String info = String.format("Type:\t\t%s\n " + "Connection:\t%s\n " + "Power:\t\t%sW \n " + "Ext Power:\t%s\n " + "Memory:\t%dGB \n ",
getType(), getConnection(), getPower(), getExtPower(), getMemory());
return info;
}
公共级视频卡{
私有字符串类型;
专用字符串连接;
私权;
私权;
私有int存储器;
公共视频卡(){
本文件为setType(“集成”);
本协议不适用;
这是设定功率(100);
本文件为setExtPower(“不适用”);
这是setMemory(1);
}
公共视频卡(字符串类型、字符串连接、int-power、String-extPower、int-memory){
这个.setType(type);
这个.setConnection(连接);
这是setPower(power);
这个.setExtPower(extPower);
这个.setMemory(内存);
}
公共void集合类型(字符串类型){
this.type=type;
}
公共void setConnection(字符串连接){
这个连接=连接;
}
公共权力(内部权力){
如果(power>=1&&power=1&&memoryJava没有自动将参数扩展为链式调用;如果您希望您的计算机
获取一张视频卡
,您需要向它传递一张新视频卡(…)
。我应该将新视频卡放在哪里()在设置的方法中?我也应该加入参数或方法吗?你的计算机构造函数是计算机(字符串品牌、字符串模型、显卡显卡)
——请注意,最后它需要一个显卡
对象,而不是一袋随机值。在那里使用新显卡
(带参数)如果您想以内联方式创建它,那么尝试了一下,但是参数会出现一个错误,说明它们找不到符号。这对于编程来说是一个重要的教训:您不会得到“错误”,而是在特定的位置(在Java中,您会得到一个检测到错误的特定字符)得到一个特定的错误(您已经描述过).如果你需要帮助,请描述一下。