[英]Calling a print method from another class with primitive data types in it
I have been programming in Java using BlueJ for 2 months now and I need some help with an assignment. 我已经使用BlueJ用Java编程了2个月,我需要一些帮助。 I am making a basic Vehicle Purchase data entry program. 我正在制作一个基本的车辆购买数据输入程序。 I need to call the printPurchaseDate()
method from the PurchaseDate
class. 我需要从PurchaseDate
类调用printPurchaseDate()
方法。 The problem I am faced with is that the print statement has three int values: year, month, and day. 我面临的问题是print语句有三个int值:年,月和日。 When I try to call this method in the Vehicle class in the printDetails()
method, it tells me that I need a return, if I take away the void I have to label the method as a string. 当我尝试在printDetails()
方法中的Vehicle类中调用此方法时,它告诉我需要返回,如果我带走了void,我必须将该方法标记为字符串。 However it doesn't work then because it holds three int variables inside it that conflict with the string method. 但是它不起作用,因为它在其中包含三个与字符串方法冲突的int变量。 How can I go about doing this? 我该怎么做呢? I basically want to print all of my information including the purchaseDate
. 我基本上想要打印包括purchaseDate
在内的所有信息。 My apologies in advance if I did not present my question properly, this is my first post. 如果我没有正确提出我的问题,我提前道歉,这是我的第一篇文章。 Thank you for your help. 谢谢您的帮助。
I have two classes: Purchase Date and Vehicle. 我有两个类:购买日期和车辆。
My vehicle class has this method, designed to print out my information: 我的车辆类有这种方法,旨在打印出我的信息:
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
PurchaseDate.printPurchaseDate();
}
Im having issues with printing the date from my PurchaseDate class in my Vehicle class's "printDetails()" method. 我在我的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);
}
}
I basically want my System.out.println
to print out the date that I have in my PurchaseDate
class. 我基本上希望我的System.out.println
打印出我在PurchaseDate
类中的日期。
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
PurchaseDate.printPurchaseDate();
}
The basic problem in your code is that your calling the printPurchaseDate() in static way but it is a non static method. 代码中的基本问题是以静态方式调用printPurchaseDate(),但它是一个非静态方法。 You have to create the refrence of PurchaseDate class and calll the method with the refrence 您必须创建PurchaseDate类的引用并使用引用来调用方法
PurchaseDate pd = new PurchaseDate();
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
pd.printPurchaseDate();
}
Other thing you can do it to declair method static. 你可以做的其他事情来声明方法静态。
public static void printPurchaseDate(){
// your code here
}
Your method printPurchaseDate()
returns void
and println()
expects something (ie a String). 你的方法printPurchaseDate()
返回void
, println()
需要一些东西(即一个String)。 To fix it, 要解决这个问题,
public String printPurchaseDate() {
return "The purchase date is: " + String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day);
}
That should do the trick. 这应该够了吧。
Either of these ways will solve your problem: 这两种方法都可以解决您的问题:
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());
}
OR 要么
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.