java - 我想创建对象,将其传递到数组中,同时在运行时使用多态性
问题描述
我想在实现多态性的同时在运行时创建 obj 名称、数量和数量。在每个对象中,我想传递值并将这些值存储在数组中以供将来访问它们。我怎样才能实现它。到目前为止,这是我的代码。当我创建 obj 例如 sb = new sbaccount {"John","00333",5000}; 我想将这些值存储到一个数组中,以便稍后我可以更新字段的值。
我的代码如下'
import java.util.Scanner;
abstract class Account{
String number;
String name;
int amount;
static final int balance = 1000;
int bal;
public Account(String string, String string2, int i){
// used to store the parameter passed into constructor
this.name = string;
this.number= string2;
this.amount = i;
}
int deposit(int i) {
return i;
}
void withdrawal(int i){
}
public void setData(String string, String string2, int i) {
// TODO Auto-generated method stub
}
public void showData() {
// TODO Auto-generated method stub
}
}
final class sbaccount extends Account {
public sbaccount(String string, String string2, int i){
super (string, string2, i);
}
public void setData(String name, String number, int amount){
this.name = name;
this.number= number;
this.amount = amount;
}
public void showData(){
System.out.println("Account Owner: "+name);
System.out.println("Account number"+ number);
}
int deposit ( int money){
bal = money + balance;
System.out.println("Hi "+ name
+" \nAccountnumber: "+ number
+" \nYou have deposited :$"+money);
System.out.println("\nYour Account balance is now :"+ bal);
return bal;
}
void withdrawal(int withdraw){
int total = 0;
total = bal - withdraw;
if (total <= balance){
System.out.println("Your balance is too low for withdrawal!");
}
else{
System.out.println("\nHi "+ name
+" \nAccountnumber: "+ number
+" \nYou have Withdrawn :$"+withdraw);
System.out.println("\nYour Account balance is now :"+ total);
}
}
}
final class current extends Account {
public current(String name, String number, int amount) {
super (name, number, amount);
}
int deposit ( int money){
bal = money + balance;
System.out.println("Hi "+ name
+" \nAccountnumber: "+ number
+" \nYou have deposited :$"+money);
System.out.println("\nYour Account balance is now :"+ bal);
return bal;
}
void withdrawal(int withdraw){
int total = 0;
total = bal - withdraw;
if (total <= balance){
System.out.println("\nHi "+ name
+" \nAccountnumber: "+ number
+ "\nYour balance is too low to perform this transaction!");
}
else{
System.out.println("\nHi "+ name
+" \nAccountnumber: "+ number
+" \nYou have Withdrawn :$"+withdraw);
System.out.println("\nYour Account balance is now :"+ total);
}
}
}
public class oopassignment {
public static void main(String[] args) {
String type;
Scanner input = new Scanner (System.in);
System.out.println("What type of account do you want to create? :");
type=input.nextLine();
sbaccount[] sb;
sb = new sbaccount[3];
sb[0]=new sbaccount("John", "00234",50000);
//sb[0].setData("John", "00234",50000);
sb[0].showData();
//sb = new sbaccount[10];
//sb.deposit(500);
//sb.withdrawal(600);
//sb = new current[10];
//sb.deposit(500000);
//sb.withdrawal(10000);
}
}
解决方案
我不确定我是否明白了你的意思,但你可以简单地创建一个数组
Object[] account= new Object[3];
public Account(String string, String string2, int i) {
account[0] = string;
account[1] = string2;
account[2] = i;
}
请只发布回答您的问题所需的代码。看看SSCCE。
推荐阅读
- python - 你好,我是learnign pyton,我得到一个错误响应400,我在谷歌上搜索解决方案,但我没有找到
- c# - 如何修复未显示的 Visual Studio C# 项目属性?
- xml - Angular 11文件上传formdata到asp.net核心api为空
- azure - Azure 自动化帐户中的 Runbook 和变量数是否有限制?
- android - 单个Android App下的Firebase多个包名称
- javascript - 如何在不调用事件的情况下将数据从子级发送到父级(vue 2)
- c# - WPF DataGrid 模板列绑定
- ssl-certificate - 在 Biztalk 中为 WCF-BasicHttp 发送适配器使用 SSL 证书
- c++ - 这个 shared_ptr 实现正确吗?
- google-apps-script - 尝试从 Google 表格中的给定 URL 中提取重定向 URL,仅打印重定向 URL