繁体   English   中英

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

[英]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()返回voidprintln()需要一些东西(即一个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM