gpt4 book ai didi

java - 从另一个包含原始数据类型的类调用打印方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:46 24 4
gpt4 key购买 nike

我使用 BlueJ 进行 Java 编程已有 2 个月了,我需要一些帮助来完成作业。我正在制作一个基本的车辆购买数据输入程序。我需要从 PurchaseDate 类调用 printPurchaseDate() 方法。我面临的问题是 print 语句具有三个 int 值:年、月和日。当我尝试在 printDetails() 方法的 Vehicle 类中调用此方法时,它告诉我需要返回,如果我去掉 void,我必须将该方法标记为字符串。但是它不起作用,因为它在其中包含三个与字符串方法冲突的 int 变量。我该怎么做呢?我基本上想打印我的所有信息,包括 purchaseDate。如果我没有正确提出我的问题,我提前道歉,这是我的第一篇文章。感谢您的帮助。

我有两个类:购买日期和车辆。

我的车辆类有这个方法,旨在打印出我的信息:

public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
PurchaseDate.printPurchaseDate();
}

我在使用 Vehicle 类的“printDetails()”方法打印 PurchaseDate 类的日期时遇到问题。

/**
* The Purchase data class
*/

public class PurchaseDate {

private int year;
private int month;
private int day;

private static final int CURRENT_YEAR = 2014;
private static final int LAST_MONTH = 12;
private static final int LAST_DAY = 31;

/**
* default constructor
*/
public PurchaseDate() {
}

/**
* @param year to initialize theYear field
* @param month to initialize theMonth field
* @param day to initialize theDay field
*/

public PurchaseDate(int theYear, int theMonth, int theDay) {
setYear(theYear);
setMonth(theMonth);
setDay(theDay);

if (year < 1900 || year > 2014) {
System.out.println("The year value can be no greater than the current year");
} else {
this.year = year; }
if (month < 1 || month > 12) {
System.out.println("The month value must be between 1 and 12");
} else {
this.month = month; }
if (day < 1 || day > 31) {
System.out.println("The day value must be between 1 and 31");
} else {
this.day = day; }
}
//Mutators and Accessors
/**
* @return year
*/
public int getYear() {
return year;
}

/**
* @return month
*/
public int getMonth() {
return month;
}

/**
* @return day
*/
public int getDay() {
return day;
}

/**
* @param the year
*/
public final void setYear(int newYear) {
year = newYear;
}

/**
* @param the month
*/
public final void setMonth(int newMonth) {
month = newMonth;
}

/**
* @param the day
*/
public final void setDay(int newDay) {
day = newDay;
}

/**
* prints the purchase date
*/
public void printPurchaseDate() {
System.out.println("The purchase date is:" + " " + year + "-" + month + "-" + day);

}
}

我基本上希望我的 System.out.println 打印出日期在我的 PurchaseDate 类中。

最佳答案

public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
PurchaseDate.printPurchaseDate();
}

您代码中的基本问题是您以静态方式调用 printPurchaseDate()但它是一个非静态方法。您必须创建 PurchaseDate 类的引用并使用引用调用该方法

PurchaseDate pd = new PurchaseDate();
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
pd.printPurchaseDate();
}

您可以做的其他事情来声明方法是静态的。

public static void printPurchaseDate(){
// your code here
}

关于java - 从另一个包含原始数据类型的类调用打印方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26823946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com