[英]equals() method and '==' operator for primitive data types and class data types
[英]Calling a print method from another class with primitive data types in it
我已經使用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
}
你的方法printPurchaseDate()
返回void
, println()
需要一些東西(即一個String)。 要解決這個問題,
public String printPurchaseDate() {
return "The purchase date is: " + String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day);
}
這應該夠了吧。
這兩種方法都可以解決您的問題:
public String getPurchaseDate() {
return "The purchase date is:" + " " + year + "-" + month + "-" + day;
}
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
System.out.println(PurchaseDate.getPurchaseDate());
}
要么
public void printPurchaseDate() {
System.out.println("The purchase date is:" + " " + year + "-" + month + "-" + day);
}
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
PurchaseDate.printPurchaseDate();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.