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

标签 java methods int call primitive

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

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

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

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

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

/**
  * 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/

相关文章:

python - 在 Python 中将方法动态绑定(bind)到类实例

char - 区分 Lisp 中的字符和整数

ios - 写入plist文件而不写入

c++ - 将二维数组打印为 C++ 中的方法

java - 使用 javaagent 获取 Web 应用程序类?

java - 创建给定类的实例

java - EJB WebService (JAX-WS) 的 WSDL 的 URL 是什么?

Android开发--输入法

裁剪图像数组

java - 什么是NullPointerException,我该如何解决?