简体   繁体   English

如何使用另一个类的实例变量接受用户输入?

[英]How do I accept user input with a instance variable of another class?

I have an assignment for college in which we must serialize a subclass called Current . 我有一个大学的任务,我们必须序列化一个名为Current的子类。 we must ask a user to enter details for a new Current account so it can be serialized. 我们必须要求用户输入新的当前帐户的详细信息,以便可以序列化。

I have a class hierarchy built up with the Account class being the super class. 我有一个类层次结构,其中Account类是超类。 It should be possible to add, view , list, edit and delete Current objects. 应该可以添加,查看,列出,编辑和删除当前对象。 We can't ask the user to add their account number as that is done via a static variable. 我们不能要求用户添加他们的帐号,因为这是通过静态变量完成的。 eg: **accountNo** = **nextAvailableNumber++** ; 例如: **accountNo** = **nextAvailableNumber++** ;

I have to take in the users name and dateOpened details and I have already written a Name class but the Name class has a variable of type Name called name so i cant use it as a String in the subclass. 我必须接受用户名和dateOpened的详细信息,我已经编写了一个Name类,但Name类有一个名为name的Name类型的变量,所以我不能将它用作子类中的String。 It is giving me a mismatch exception, cannot convert String to Name and mismatch exception, cannot convert from int to Date for dateOpened . 它给了我一个不匹配的异常,无法将String转换为Name和mismatch异常,无法将date转换为Date为DateOpened

My question then is. 我的问题是。 If I have to take user input. 如果我必须接受用户输入。 should i be using the name variable of Type Name in the Name class to take input and should i be doing the same with the **dateOpened** variable of type Date ? 我应该在Name类中使用Type Name的name变量来获取输入,我应该对Date类型的**dateOpened**变量做同样的**dateOpened**吗? Should i be casting. 我应该铸造吗? Here is my code, its a menu based system that allows a user to set up an account. 这是我的代码,它是一个基于菜单的系统,允许用户设置帐户。

// Name class with name variable of type Name && Date variable **dateOpened** of type Date. //名称类名称变量类型Name && Date变量**dateOpened**类型为Date。

package ie.lyit.bank;

public abstract class Account
{
    protected Name name;        // COMPOSITION - Account HAS-A name (object of class Name)
    protected String address;   
    protected double balance;
    protected Date dateOpened;  // COMPOSITION - Account HAS-A dateOpened (object of class Date)
    protected int accountNo;

    private static int nextUniqueNumber = 1;    // Next available unique Account number 

    // Default Constructor - set Instance Variables to null
    public Account()
    {
        name = new Name();      
        address= new String();  // OR address = ""; OR address = null;
        balance = 0.0;      
        dateOpened = new Date();
        // Set accountNo to static nextUniqueNumber & increment it for next accountNo
        accountNo = nextUniqueNumber++; 
    }

    // Current class with read() method
    // Read method to read in the account details from the user

    public void read()
    {
        Scanner keyboardIn = new Scanner(System.in);        
        System.out.println("ENTER ACCOUNT DETAILS ==>");
        System.out.print("ACCOUNT NAME : ");
        name = keyboardIn.nextLine();
        System.out.print("ACCOUNT ADDRESS : ");
        address = keyboardIn.nextLine();
        System.out.print("ACCOUNT BALANCE : ");
        balance = keyboardIn.nextDouble();
        System.out.print("ACCOUNT DATE OPENED : ");
        dateOpened = keyboardIn.nextInt();
    }
}

You dont need to use Name type, You can simply declare it as String and access it as class variable. 您不需要使用Name类型,您可以简单地将其声明为String并将其作为类变量访问。

If you have a derived class say xyz who needs to access name variable of superclass say Account. 如果你有一个派生类,说xyz谁需要访问超类的名称变量说Account。

You can access it like Account.Name saying you have defined it as below. 你可以像Account.Name一样访问它,说你已经定义如下。

class Account
{
 protected String Name;

}

class xyz extends Account
{
// here u can access using Account.Name; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将用户输入保存为类实例的名称? - How do I save user input as the name of a class instance? 如何接受用户输入并将其放入 while 条件 - How do I accept the user input and put it into the while condition 如何在不创建新实例的情况下从另一个类访问变量 - How do I access a variable from another class without it creating a new instance 如何要求用户输入变量? - How do I ask user to input a variable? 如何从一个 class 中的扫描仪获取用户输入,然后将其应用于同一 package 中的另一个 class? - How do I take an user input from Scanner in one class and then apply it to another class in the same package? 如何在另一个类中杀死一个类的实例? - How do I kill an instance of a class in another class? 如何将Groovy类的实例分配给Java类中的变量 - How do I assign instance of Groovy class to variable in Java class 如何获取用户输入并将其存储到另一个类中的数组中? || 爪哇 - How do I take user input and store it into an array that is in another class? || Java 如何将文本输入从一个JTextField传递到另一个类中的变量? - How do I pass text input from one JTextField to a variable in another class? 如何过滤用户输入以接受 char 和 int,但不接受 specialchar。 然后只将 int 放入一个数组中 - How do I filter a user input to accept char and int, but not specialchar. Then only takes the int into an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM