简体   繁体   中英

Can I assign two different values to the same instance variable?

I am limited to using the specified instance variables, and the specified methods. How can I create 2 DateTime objects (one for current date, and one for future date) while using these same methods for both. currentDate and futureDate should have different values, but I can't figure out how to do that while using only these methods and variables.

public class DateTime {  //Enter and calculate data time

private int month; //Month instance variable
private int day; // Day instance variable
private int hour; // Hour instance variable
private int minute; // Minutes instance variable
private int second; // Second instance variable
private Scanner input = new Scanner(System.in); // Instance variable for all inputs



public void inputMonth() { // Input month value
    System.out.print("Enter the month (1-12): ");
    month = input.nextInt();
    }

public void inputDay() { // Input days value
    System.out.print("Enter the day (1-30): ");
    day = input.nextInt();
    }

public void inputHours() { // Input hours value
    System.out.print("Enter the hour (0-23): ");
    hour = input.nextInt();
    }

public void inputMinutes() { // Input minutes value
    System.out.print("Enter the minutes (0-59): ");
    minute = input.nextInt();
    }

public void inputSeconds() {  //  Input seconds value 
    System.out.print("Enter the seconds (0-59): ");
    second = input.nextInt();
    }
}
Create a new Class and create 2 objects of your DateTime Class as given 
below:
class Runner
{
    DateTime currentDate = new CurrentDate();
    currentDate.inputMonths();
    currentDate.inputDays();
    currentDate.inputHours();
    currentDate.inputMinutes();
    currentDate.inputSeconds();

//Now Create Another Object of DateTime Class:
    DateTime futureDate = new CurrentDate();
    futureDate.inputMonths();
    futureDate.inputDays();
    futureDate.inputHours();
    futureDate.inputMinutes();
    futureDate.inputSeconds();
}//end of class

Create ShowDate method in your DateTime class and call it with both the objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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