这是我创建和管理对象时的主程序.
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".