从其他类中调用print方法,其中包含原始数据类型

时间:2014-11-09 01:20:53

标签: java methods int call primitive

我使用BlueJ进行了2个月的Java编程,我需要一些帮助。我正在制作一个基本的车辆购买数据输入程序。我需要从printPurchaseDate()类调用PurchaseDate方法。我面临的问题是print语句有三个int值:年,月和日。当我尝试在printDetails()方法的Vehicle类中调用此方法时,它告诉我需要返回,如果我删除了空白,我必须将方法标记为字符串。但是它不起作用,因为它在其中包含三个与字符串方法冲突的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课程中。

3 个答案:

答案 0 :(得分:2)

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
}

答案 1 :(得分:0)

您的方法printPurchaseDate()返回voidprintln()需要一些东西(即字符串)。要解决它,

public String printPurchaseDate() {
    return "The purchase date is: " + String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day);
}

这应该可以解决问题。

答案 2 :(得分:0)

这两种方法都可以解决您的问题:

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();
}