简体   繁体   English

我可以为同一个实例变量分配两个不同的值吗?

[英]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.如何创建 2 个 DateTime 对象(一个用于当前日期,一个用于未来日期)同时对两者使用相同的方法。 currentDate and futureDate should have different values, but I can't figure out how to do that while using only these methods and variables. 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.

相关问题 我可以将 2 个值分配给 char 变量吗? - Can I assign 2 values into a char variable? 为什么我不能将具有其他类型的实例分配给参数化变量? - Why can't I assign an instance with another type to a parameterized variable? 实例变量的值在两个类中是不同的。 为什么? (更多问题) - values of an instance variable is different in two classes. Why? (+ more questions) 什么时候适合在 Java 中为实例变量赋值? - When is it appropriate to assign values to an instance variable in Java? 如何为两个不同的表分配不同的值而不为其他表编写几乎相同的代码? - How to assign different values to two different table without writing almost same code for other table? 我可以为同一个变量使用两个名字吗? - Can I have two names for the same variable? 使用Varargs,我可以根据变量名称为变量赋值吗? - Using Varargs, can I assign values to variables based on the variable name? 如何在同一个 Mock 上获得两个方法调用以返回不同的值? - How can I get two method calls on the same Mock to return different values? 我可以只用一种类型的变量在两种不同类型的 arrays 中搜索值吗? - Can I search for values in two different type of arrays with just one type of variable? 尽管分配了不同的值,但不同对象的实例变量具有相同的值 - Instance variable with the same value for different objects although different values were assigned
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM