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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26823946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:40:50  来源:igfitidea点击:

Calling a print method from another class with primitive data types in it

javamethodsintcallprimitive

提问by Stefan Timotic

I have been programming in Java using BlueJ for 2 months now and I need some help with an assignment. I am making a basic Vehicle Purchase data entry program. I need to call the printPurchaseDate()method from the PurchaseDateclass. The problem I am faced with is that the print statement has three int values: year, month, and day. 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. However it doesn't work then because it holds three int variables inside it that conflict with the string method. How can I go about doing this? I basically want to print all of my information including the purchaseDate. My apologies in advance if I did not present my question properly, this is my first post. Thank you for your help.

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

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.printlnto print out the date that I have in my PurchaseDateclass.

我基本上希望我System.out.println打印出我在PurchaseDate课堂上的日期。

回答by Avi

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. You have to create the refrence of PurchaseDate class and calll the method with the refrence

您代码中的基本问题是您以静态方式调用 printPurchaseDate() 但它是一个非静态方法。您必须创建 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
}

回答by hfontanez

Your method printPurchaseDate()returns voidand println()expects something (i.e. a String). To fix it,

您的方法printPurchaseDate()返回voidprintln()期待一些东西(即一个字符串)。要解决这个问题,

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

That should do the trick.

这应该够了吧。

回答by jrahhali

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