Java 将值传递给参数
当我尝试这样做时,我得到了问题的答案Java 将值传递给参数,java,Java,当我尝试这样做时,我得到了问题的答案 Constructor Product in class Product cannot be applied to given types; required: java.lang.String,int,double; found: java.lang.String; reason: actual and formal arguments lists differ in length 我有两门课: import java.text.* public cla
Constructor Product in class Product cannot be applied to given types;
required: java.lang.String,int,double; found: java.lang.String;
reason: actual and formal arguments lists differ in length
我有两门课:
import java.text.*
public class Product {
private String name;
private int stock;
private double price;
public Product(String name, int stock, double price) {
this.name = name;
this.stock = stock;
this.price = price;
}
public double sell(int n) {
stock = n;
return stock;
}
public void restock(int n) {
}
@Override
public String toString() {
return stock + name + "at $"+price;
}
}
public class Store {
public static void main(String[] args) {
new Store().use();
}
private Product product;
private Product cashRegister;
public Store() {
product = new Product("Sticky tape");
cashRegister = new Product("Cash register");
}
public void use() {
}
private void sell() {
}
private void restock() {
}
private void viewStock() {
}
private void viewCash() {
}
private void help() {
System.out.println("Menu options");
System.out.println("s = sell");
System.out.println("r = restock");
System.out.println("v = view stock");
System.out.println("c = view cash");
System.out.println("x = exit");
}
}
我知道我必须为
产品
构造函数声明。但我想我已经做到了。如果有人知道我哪里做错了,请解释。谢谢大家! 您没有带一个参数的构造函数,因此无法使用此表单
product = new Product("Sticky tape");
使用一个参数对多个构造函数进行十次分解或填充所有参数
product = new Product("Sticky tape", 10, 20.0);
您需要:
重载构造函数
public Product(String name){...}
或使用权限创建产品实例,并且只有一个构造函数uor具有:
public Product(String name, int stock, double price)
如果你超负荷,那么类似的事情就会发生
public Product(String name){
this(name, 0, 0.0);
}
因此,您从另一个构造函数调用一个构造函数,错误在以下代码行中:
public Product(String name){...}
product = new Product("Sticky tape");
cashRegister = new Product("Cash register");
定义的产品构造函数期望:
public Product(String name, int stock, double price)
产品类中没有接受单个字符串参数的构造函数。这样创建它:
public Product(String name) {
this.name = name;
}
在导入语句中,您忘记了分号:
import java.text.*;
现在是学习构造函数重载的时候了。重载来自OOP 可以对方法和构造函数使用重载。重载意味着对于相同的方法名,可以实现该方法
- 具有不同参数的多个时间(参数数量)
- 可以为参数使用不同的数据类型
- 也可以改变参数的顺序
//constructor with one parameter
public Product(String name) {
this.name = name;
this.stock = 0;//or whatever your default value
this.price = 0;//or whatever your default value
}
//constructor with two parameter
public Product(String name, , int stock) {
this.name = name;
this.stock = stock;
this.price = 0;//or whatever your default value
}
public Product(String name, int stock, double price) {
this.name = name;
this.stock = stock;
this.price = price;
}
这样,你可以添加任意数量的内容
或者,在创建对象时,可以使用一个构造函数和传递参数来匹配构造函数的实现。如下图所示:
product = new Product("Sticky tape", 0, 0);
这不是完整的描述,您可以阅读以了解更多信息您的程序有3个编码错误,包括
类产品{
私有字符串名称;
私人股票;
私人双价;
公共产品(字符串名称、整数库存、双倍价格){
this.name=名称;
这个。股票=股票;
这个价格=价格;
}
公共产品(字符串名称){
this.name=名称;
}
公开双重出售(int n){
股票=n;
退货;
}
公共空位补货(int n){
}
@凌驾
公共字符串toString(){
以“$”+价格返回股票+名称+”;
}
}
公共类商店{
公共静态void main(字符串[]args){
商店s=新商店();
系统输出打印项次(s.product);
系统输出打印LN(s.收银机);
}
私人产品;
私人产品收银机;
公共商店(){
产品=新产品(“胶带”);
收银机=新产品(“收银机”);
}
}
产品除了名称外,还有库存量和价格。但是您从未指定股票和价格的值。您的构造函数需要3个参数<代码>产品=新产品(“胶带”)代码>您正在发送1您的构造函数获取3个参数,没有一个参数这只是传递三个参数中的一个新产品(“胶带”)代码>