【问题标题】:How can I allow a user to create different types of objects, in order to use Java polymorphism?为了使用 Java 多态性,我如何允许用户创建不同类型的对象?
【发布时间】:2020-10-23 11:40:32
【问题描述】:

我目前正在学习 OOP java 课程。在下面的代码中,我在运行时实现多态性以及继承。我正在创建一个名为“平衡”的常量。 该程序的目标是创建一个名为 Account 的类,并使用 SBaccount 和 current 两种类型的类扩展该类。

与Account 共享的共同属性是name、number 和amount。到目前为止,我有一个工作代码,但它还没有完成我想要的。 一旦用户指定了我想要验证用户输入的类型,我希望能够询问用户需要创建的帐户类型。例如,如果用户存入 xx 金额,我还想将该金额添加到余额中然后存储。我想在“提款”方法中,当用户提款时,它应该从余额中取出。

我的代码:

 import java.util.Scanner;

    abstract class Account{
    String number;
    String name;
    int amount;
    static final int balance = 1000; 
    int bal;
    
    public  Account(){
        
        
    }
    
    int deposit(int i) {
        return i;
    
    }
    
    void withdrawal(int i){
    
    }
    
    
}

   final class sbaccount extends Account {
    
    
    public sbaccount() {
        
        // TODO Auto-generated constructor stub
    }
    int deposit (int money){
        bal = money + balance;
        
        System.out.println("You have deposited :$"+money );
        System.out.println("Your Account balance is now :"+bal);
        return bal;
    }
        void withdrawal(int withdraw){
        
        if (bal <= balance){
            System.out.println("Your balance is too low for withdrawal!");
        
        }
        else{
            
            amount = bal - withdraw;
            System.out.println("You have withdrawn :$"+ withdraw);
            System.out.println("Your Account balance is now :"+ amount);
        }
        
      }
    
}
  final class current extends Account {

    
    public current() {
        
    }
    
    int deposit ( int money){
        bal = money + balance;
        
        System.out.println("You have deposited :$"+money);
        System.out.println("Your Account balance is now :"+ bal);
        return bal;
    
    }
    void withdrawal(int withdraw){
        
        if (bal <= balance){
            System.out.println("Your balance is too low for withdrawal!");
        
        }
        else{
            
            amount = bal - withdraw;
            System.out.println("You have withdrawn :$"+ withdraw);
            System.out.println("Your Account balance is now :"+ amount);
        }
        
      }
     
    
    
}
   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();
        
        Account sb;
        sb = new sbaccount();
        sb.deposit(500);
        sb.withdrawal(100);
        sb = new current();
        sb.deposit(500000);
        sb.withdrawal(1000000);
        
        

      }
   }

【问题讨论】:

  • 什么是“聚合”?你的意思是多态性
  • 是的,我的意思是运行时的多态性**,对此感到抱歉
  • 仅供参考,您应该正确格式化您上传的代码。此外,这并不是一个真正的 OOP 问题,因此您应该调整标签和标题。您似乎想要的是一种存储用户输入的方法。
  • 感谢您的提示,我是 Stack 的新手,所以我目前只是掌握它的窍门。

标签: java oop object inheritance polymorphism


【解决方案1】:

所以你可以通过class.newInstance()方法完成下面的任务。

System.out.println("What type of account do you want to create? :");
String account_type = input.next();
Class c = Class.forName(account_type);
Account sb = (Account)c.getDeclaredConstructor().newInstance();

类 c 将根据传递的参数加载类。并且对象将通过强制转换创建(最初需要该对象为“Class”类型)。

所以最后你的代码将是(做了一些修改):

import java.util.Scanner;

abstract class Account {
    String number;
    String name;
    int amount;
    static final int balance = 1000;
    int bal;

    abstract int deposit(int i);

    abstract void withdrawal(int i);

}

final class sbaccount extends Account {

    int deposit(int money) {
        bal = money + balance;

        System.out.println("You have deposited :$" + money);
        System.out.println("Your Account balance is now :" + bal);
        return bal;
    }

    void withdrawal(int withdraw) {

        if (bal <= balance) {
            System.out.println("Your balance is too low for withdrawal!");

        } else {
            amount = bal - withdraw;
            System.out.println("You have withdrawn :$" + withdraw);
            System.out.println("Your Account balance is now :" + amount);
        }

    }

}

final class current extends Account {

    int deposit(int money) {
        bal = money + balance;

        System.out.println("You have deposited :$" + money);
        System.out.println("Your Account balance is now :" + bal);
        return bal;

    }

    void withdrawal(int withdraw) {

        if (bal <= balance) {
            System.out.println("Your balance is too low for withdrawal!");

        } else {

            amount = bal - withdraw;
            System.out.println("You have withdrawn :$" + withdraw);
            System.out.println("Your Account balance is now :" + amount);
        }

    }
}

public class oopassignment {

    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.println("What type of account do you want to create? :");
        String account_type = input.next();
        // The only valid optios are : sbaccount & current.
        // If any other class name is inputted. It will show exception
        Class c = Class.forName(account_type);
        Account sb = (Account)c.getDeclaredConstructor().newInstance();
        sb.deposit(500);
        sb.withdrawal(100);
        sb = new current();
        sb.deposit(500000);
        sb.withdrawal(1000000);
    }
}

让我知道是否需要任何修改才能回答。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 2020-03-06
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    相关资源
    最近更新 更多