这是我创建和管理对象时的主程序.

public class ShoppingCartManager {

    public static void main(String[] args) {
       
           //when declaring the object it's not changing the values.
       ShoppingCart cart1 = new ShoppingCart("John Doe", "February 1, 2016");
       System.out.println(cart1.getCustomerName());
       
    }

}//end of class

/

这就是类,构造器应该写入类的私有变量,但它们不会更新.

import java.util.ArrayList;

public class ShoppingCart {

    private String customerName = "none";
    private String currentDate = "January 1, 2016";
    //ArrayList<ItemToPurchase> cartItems = new ArrayList<ItemToPurchase>();
    
    //constructor methods
        //this is constructer that it calls
    ShoppingCart(String Name, String Date) {
        //Parameterized constructor which takes the customer name and date as parameters
        String customerName = Name;
        String currentDate = Date;
    }
    ShoppingCart() {
        //String customerName - Initialized in default constructor to "none"
        //String currentDate - Initialized in default constructor to "January 1, 2016"
        String customerName = "none";
        String currentDate = "January 1, 2016";
    }

ShoppingCart(String Name) {
String customerName = Name;
}

    /////////////////////////////////////////////////////
    
    //Getter methods
    
    //getCustomerName
    public String getCustomerName(){
        return customerName;
    }
    //getDate
    public String getDate() {
        return currentDate;
    }
    
    /////////////////////////////////////////////////////

}//end of class

我try 重新创建构造函数并创建新对象.但是对象customerName变量将始终保持默认值"None".

推荐答案

public class ShoppingCart {

private String customerName = "none";
private String currentDate = "January 1, 2016";
//ArrayList<ItemToPurchase> cartItems = new ArrayList<ItemToPurchase>();

// Parameterized constructor
ShoppingCart(String name, String date) {
    this.customerName = name; // Assign the parameter to the class field
    this.currentDate = date; // Assign the parameter to the class field
}

// Default constructor
ShoppingCart() {
    // The class fields are already initialized with default values, so you could leave this empty or remove it if you don't need to do anything else.
}

ShoppingCart(String name) {
    this.customerName = name; // Assign the parameter to the class field
    // currentDate will retain its default value unless explicitly set
}

// Getter methods

public String getCustomerName() {
    return customerName;
}

public String getDate() {
    return currentDate;
}

您在ShoppingCart类中遇到的问题源于如何在构造函数中处理赋值.当您在构造函数中为CustomerName和CurrentDate赋值时,实际上是在创建并为局部变量赋值,而不是更新类字段.这就是为什么您的CustomerName始终保持其缺省值"None".若要解决此问题,应在设置值 timeshift 除构造函数内的数据类型声明.这样,您就可以确保将提供的值赋给类的字段,而不是赋给新的局部变量.这应该行得通.

Java相关问答推荐

是否可以从@ TrustMapping中删除特定方法的基路径?

try 使用Java 9或更高版本对特殊对象图进行解析时出现NullPointerException

弹簧靴和龙目岛

工件部署期间出错[Tomcat 8.5.45]

如何让JFileChooser(DIRECTORIES_ONLY)从FolderName中的空白开始?

Spring Boot@Cachebale批注未按预期工作

相同的Java SerializedLambda为implMethodKind返回不同的结果

计算两个浮点数之间的距离是否对称?

在Java 17中使用两个十进制数字分析时间时出错,但在Java 8中成功

使用Room Database删除Jetpack合成中的所有项目后,UI未重新合成

如何仅使用键/ID的一部分(组合)高效地返回映射值?

如何在JavaFX中制作鼠标透明stage

使用While循环打印素数,无法正常工作

有谁能帮我修一下这个吗?使输出变得更加整洁

找出承载Cargo 的最小成本

何时调用密封层次 struct 的switch 中的默认情况

在Java Spring JPA中插入包含对其他实体的引用的列

如何使用Java对随机生成的字母数字优惠券代码进行过期设置

Java System.getProperty在哪里检索user.home?

如何在 Android Studio 中删除 ImageView 和屏幕/父级边缘之间的额外空间?