[英]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.