【问题标题】:Calling a print method from another class with primitive data types in it从另一个具有原始数据类型的类调用打印方法
【发布时间】:2015-01-05 14:02:12
【问题描述】:

我已经使用 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()”方法中从 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 课程中。

【问题讨论】:

    标签: java methods int call primitive


    【解决方案1】:

    您的方法 printPurchaseDate() 返回 void 并且 println() 需要一些东西(即字符串)。要修复它,

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

    这应该可以解决问题。

    【讨论】:

    • 怎么样?你为什么不发布你的代码,这样我们就不必猜测了。您最初的问题是“为什么 println 方法不起作用。我们向您解释了为什么不起作用。现在,您遇到了另一个问题。发布您的代码,这样我们就不必玩 '20 个问题'了”跨度>
    • 如果我使用这种方法会遇到问题,因为“年”、“月”和“日”是 int 值,而方法是字符串。有什么办法让它工作吗?
    • 我刚刚编辑了方法。 String.valueOf() 将这些原始值转换为字符串。
    【解决方案2】:

    以下任何一种方式都可以解决您的问题:

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

    【讨论】:

      【解决方案3】:
      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
      }
      

      【讨论】:

      • 不能使用类名调用非静态方法,它们需要超出类引用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多