Java中引用属性的问题
我一直在试图找出如何将这些属性从数据类正确地传递到可执行类,但我不确定如何传递。以下是数据类:Java中引用属性的问题,java,object,constructor,attributes,Java,Object,Constructor,Attributes,我一直在试图找出如何将这些属性从数据类正确地传递到可执行类,但我不确定如何传递。以下是数据类: public class Motor { private int cylinders; private int hp; private String type; public Motor(int cylinders, int hp, String type) { this.cylinders = cylinders
public class Motor {
private int cylinders;
private int hp;
private String type;
public Motor(int cylinders, int hp, String type) {
this.cylinders = cylinders;
this.hp = hp;
this.type = type;
}
public int getCylinders() {
return cylinders;
}
public int getHp() {
return hp;
}
public String getType() {
return type;
}
public String toString() {
return "Motor: cylinders=" + this.cylinders + ", hp=" +
this.hp + ", type=" + this.type;
}
}
现在,在另一个数据类中,我应该引用Motor并生成一系列其他属性。我没有任何(明显的)错误,但我认为如果我不发布它,所有这些都没有任何意义:
public class Vehicle {
private String make;
private String model;
private int year;
private double price;
private Motor motor;
public Vehicle(String make, String model, int year, double price, Motor motor) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.motor = motor;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return "Vehicle make=" + this.make + ", model=" +
this.model + ", year=" + this.year +
", price=" + this.price + ", motor=" +
this.motor;
}
}
这就是真正的问题所在,当我在TestVehicle中创建一个Vehicle对象时,我在尝试添加cylinder、hp和type from back in Motor的值时出错:
public class TestVehicle {
public static void main(String[] args) {
Vehicle v1 = new Vehicle("Toyota", "Corolla", 2015, 15999.0, 7, 300, "Gas");
System.out.println(v1.toString());
}
}
当我尝试执行它时,我得到的错误是:
线程“main”java.lang中出现异常。错误:未解决的编译问题:
构造函数载体(String,String,int,double,int,int,String)未定义
非常感谢
线程“main”java.lang中出现异常。错误:未解析编译
问题:构造工具(字符串,字符串,int,double,int,
int,String)未定义
改为
Motor motor = new Motor( 7, 300, "Gas");
Vehicle v1 = new Vehicle("Toyota", "Corolla", 2015, 15999.0, motor);
检查构造函数
public Vehicle(String make, String model, int year, double price, int cylinders, int hp, String type) {
this.(make, model, year, price, new Motor(cylinders, hp, type))
}
线程“main”java.lang中出现异常。错误:未解析编译
问题:构造工具(字符串,字符串,int,double,int,
int,String)未定义
改为
Motor motor = new Motor( 7, 300, "Gas");
Vehicle v1 = new Vehicle("Toyota", "Corolla", 2015, 15999.0, motor);
检查构造器。车辆参数传递构造器有问题。请尝试下面的代码,它工作正常
class Motor {
private int cylinders;
private int hp;
private String type;
public Motor(int cylinders, int hp, String type) {
this.cylinders = cylinders;
this.hp = hp;
this.type = type;
}
public int getCylinders() {
return cylinders;
}
public int getHp() {
return hp;
}
public String getType() {
return type;
}
public String toString() {
return "Motor: cylinders=" + this.cylinders + ", hp=" +
this.hp + ", type=" + this.type;
}
}
class Vehicle {
private String make;
private String model;
private int year;
private double price;
private Motor motor;
public Vehicle(String make, String model, int year, double price, Motor motor) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.motor = motor;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return "Vehicle make=" + this.make + ", model=" +
this.model + ", year=" + this.year +
", price=" + this.price + ", motor=" +
this.motor;
}
}
class TestVehicle {
public static void main(String[] args) {
Motor m1 = new Motor(7,300,"Gas");
Vehicle v1 = new Vehicle("Toyota", "Corolla", 2015, 15999.0,m1);
System.out.println(v1.toString());
}
}
public Vehicle(String make, String model, int year, double price, int cylinders, int hp, String type) {
this.(make, model, year, price, new Motor(cylinders, hp, type))
}
车辆参数传递中存在一些问题。请尝试下面的代码,它工作正常
class Motor {
private int cylinders;
private int hp;
private String type;
public Motor(int cylinders, int hp, String type) {
this.cylinders = cylinders;
this.hp = hp;
this.type = type;
}
public int getCylinders() {
return cylinders;
}
public int getHp() {
return hp;
}
public String getType() {
return type;
}
public String toString() {
return "Motor: cylinders=" + this.cylinders + ", hp=" +
this.hp + ", type=" + this.type;
}
}
class Vehicle {
private String make;
private String model;
private int year;
private double price;
private Motor motor;
public Vehicle(String make, String model, int year, double price, Motor motor) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.motor = motor;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return "Vehicle make=" + this.make + ", model=" +
this.model + ", year=" + this.year +
", price=" + this.price + ", motor=" +
this.motor;
}
}
class TestVehicle {
public static void main(String[] args) {
Motor m1 = new Motor(7,300,"Gas");
Vehicle v1 = new Vehicle("Toyota", "Corolla", 2015, 15999.0,m1);
System.out.println(v1.toString());
}
}
这是您的构造函数,您需要字符串、字符串、int、double和Motor
public Vehicle(String make, String model, int year, double price, Motor motor) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.motor = motor;
}
您的电机具有如下构造:
public Motor(int cylinders, int hp, String type) {
this.cylinders = cylinders;
this.hp = hp;
this.type = type;
}
因此,如果您想将电机参数直接作为int,int和String传递给车辆构造函数,您需要一个接受String,String,int,double,int,int,String的车辆构造函数
public Vehicle(String make, String model, int year, double price, Motor motor) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.motor = motor;
}
您可以向车辆类别添加第二个constructor,如:
public Vehicle(String make, String model, int year, double price, int cylinders, int hp, String type) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.motor = new Motor(cylinders, hp, type);
}
但这不是一个好的样式,如果某些内容发生了更改,您可能会忘记更改两个构造函数。最好是链接构造函数
public Vehicle(String make, String model, int year, double price, int cylinders, int hp, String type) {
this.(make, model, year, price, new Motor(cylinders, hp, type))
}
这是您的构造函数,您需要字符串、字符串、int、double和Motor
public Vehicle(String make, String model, int year, double price, Motor motor) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.motor = motor;
}
您的电机具有如下构造:
public Motor(int cylinders, int hp, String type) {
this.cylinders = cylinders;
this.hp = hp;
this.type = type;
}
因此,如果您想将电机参数直接作为int,int和String传递给车辆构造函数,您需要一个接受String,String,int,double,int,int,String的车辆构造函数
public Vehicle(String make, String model, int year, double price, Motor motor) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.motor = motor;
}
您可以向车辆类别添加第二个constructor,如:
public Vehicle(String make, String model, int year, double price, int cylinders, int hp, String type) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.motor = new Motor(cylinders, hp, type);
}
但这不是一个好的样式,如果某些内容发生了更改,您可能会忘记更改两个构造函数。最好是链接构造函数
public Vehicle(String make, String model, int year, double price, int cylinders, int hp, String type) {
this.(make, model, year, price, new Motor(cylinders, hp, type))
}