在创建重载的构造函数时遇到问题,实例化对象

时间:2016-02-07 03:32:01

标签: java methods constructor overloading instantiation

我是Java的新手,我在老师的视频讲座上遇到了麻烦(我正在参加在线课程),而我在完成这项任务时遇到了麻烦:

  

创建一个FitnessTracker类,其中包括健身活动的数据字段,参与的分钟数,使用的卡路里和日期。该类包括获取每个字段的方法。此外,创建一个默认构造函数,自动将活动设置为“正在运行”,将分钟设置为0,将卡路里设置为0,将日期设置为当前年份的1月1日。将文件另存为FitnessTracker.java。创建一个演示每个方法正常工作的应用程序,并将其另存为TestFitnessTracker.java。

     

湾为练习3a中创建的Fitness Tracker类创建一个额外的重载构造函数。此构造函数接收每个数据字段的参数并适当地分配它们。将任何所需的语句添加到TestFitnessTracker应用程序,以确保重载的构造函数正常工作,保存,然后测试它。

     

℃。修改Fitness Tracker类,以便默认构造函数调用四参数构造函数。将课程保存为FitnessTracker2.java。创建一个应用程序来测试该类的新版本,并将其命名为TestFitnessTracker2.java。

我也一直在观看一些youtube视频,他们已经帮助了解了一些,但我仍然没有完全理解它。 我不断收到错误“修改日的非法参数,只允许最终版本”,“日期方法”旁边的错误以及其他错误。 这是我到目前为止所拥有的,但我现在有点失落,不知道如何前进:

package javaProgramming;

import java.util.Scanner;

public class fitnessTracker {
    public static void main(String[] args){ 
       tracker trackerObject = new tracker("January 1st", "running", 0, 0);
    }
}

class tracker {
    public tracker(String date, String active, int mins, int cal){
       Scanner input = new Scanner(System.in);

       System.out.println("What day are you recording this activity for?");
       public String day = input.nextLine();

       System.out.println("What fitness exersise did you complete?");
       public String active = input.nextLine();

       System.out.println("How many minutes did you spend " + active + "?");
       public int mins = input.nextInt();

       System.out.println("How many calories did you burn?");
       public int cal = input.nextInt();
    }

    public static void date(String day){
           System.out.println("I did this on " + day + ".");

    public static void activity(String active){
        System.out.println("Today I plan on " + active);
    }

    public static void minutesSpent(int mins){
        System.out.println("I spent " + mins + " minutes doing so.");
    }

    public static void caloriesUsed(int cal){
        System.out.println("I burned " + cal + " calories.");
    }
}

如果有人可以帮我解决这个问题,我将不胜感激!

2 个答案:

答案 0 :(得分:1)

你不能在函数中使用public,只能在类上下文中使用:

public String day = input.nextLine();

删除public

String day = input.nextLine();

或将变量的声明移到类:

class tracker{
    public String day;
    public tracker(String date, String active, int mins, int cal){
        day = input.nextLine();

答案 1 :(得分:0)

试试这个。

第1类(包含主要方法)

package javaProgramming;

import java.util.Scanner;

public class FitnessTracker {

    public static void main(String[] args){ 
        Tracker trackerObject = new Tracker("January 1st", "running", 0, 0);
        Scanner input = new Scanner(System.in);

        String day;
        String active;
        int mins;
        int cal;


        System.out.println("What day are you recording this activity for?");
        day = input.nextLine();

        System.out.println("What fitness exersise did you complete?");
        active = input.nextLine();

        System.out.println("How many minutes did you spend " + active + "?");
        mins = input.nextInt();

        System.out.println("How many calories did you burn?");
        cal = input.nextInt();

        trackerObject.date(day);
        trackerObject.activity(active);
        trackerObject.minutesSpent(mins);
        trackerObject.caloriesUsed(cal);
    }
}

第2课

package javaProgramming;

class Tracker {

    private String day;
    private String active;
    private int mins;
    private int cal;

    public Tracker(String date, String active, int mins, int cal){
        this.day = date;
        this.active = active;
        this.mins = mins;
        this.cal = cal;     
    }

    public void date(String day){
        System.out.println("I did this on " + day + ".");
    }
    public void activity(String active){
        System.out.println("Today I plan on " + active);
    }

    public void minutesSpent(int mins){
        System.out.println("I spent " + mins + " minutes doing so.");
    }

    public void caloriesUsed(int cal){
        System.out.println("I burned " + cal + " calories.");
    }
}

我的版本:

    按照惯例,
  1. 类名以大写字母
  2. 开头
  3. 扫描程序输入仅在main方法中发生,以将用户输入值传递给Tracker类,以便构造函数可以将值初始化为每个变量。设定值将传递给类中的date,activity,minuteSpent,caloriesUsed方法的每个参数。
  4. 这是我的控制台中的输出:

    What day are you recording this activity for?
    3
    What fitness exersise did you complete?
    squat
    How many minutes did you spend squat?
    5
    How many calories did you burn?
    100
    I did this on 3.
    Today I plan on squat
    I spent 5 minutes doing so.
    I burned 100 calories.
    

    我希望这有帮助!