[英]Can I assign two different values to the same instance variable?
我僅限於使用指定的實例變量和指定的方法。 如何創建 2 個 DateTime 對象(一個用於當前日期,一個用於未來日期)同時對兩者使用相同的方法。 currentDate 和 futureDate 應該有不同的值,但我不知道如何在只使用這些方法和變量的情況下做到這一點。
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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.