首页 > 解决方案 > 我想创建对象,将其传递到数组中,同时在运行时使用多态性

问题描述

我想在实现多态性的同时在运行时创建 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);
      
    

 }
}

标签: javaarraysooppolymorphism

解决方案


我不确定我是否明白了你的意思,但你可以简单地创建一个数组

Object[] account= new Object[3];

public Account(String string, String string2, int i) {
    account[0] = string;  
    account[1] = string2;
    account[2] = i;
}

请只发布回答您的问题所需的代码。看看SSCCE


推荐阅读