简体   繁体   中英

Java: How to convert a String into an subclass data type?

I want to convert a String to an subclass data type, how can it be done? or it is possible?

I have a abstract class Acct

A public abstract class SinAcct extends Acct

A public class SavAcct extends SinAcct

In SavAcct, there is a constructor

public SavAcct(String acctNo, String name, ConsolidateAccount ownerAcct, double lastMonthBal){
        super(acctNo,name,ownerAcct,lastMonthBal);

    }

An abstract class ConsolidateAccount extends Account

I want to new a SavAcct,

new SavAcct(array[1],array[2],array[3],Double.parseDouble(array[4])

but it is error The constructor SavAcct(String, String, String, double) is undefined

anyone can help me? pls

If you really want to convert a String to an object of some other class. You can have three possible solutions :-

  1. Create a Constructor which takes String as an argument and does the appropriate conversion to create the Object.

  2. Have a factory method ie getInstance() which takes String parameter and returns an Object.

  3. You can have a method which can be used for the same purpose as above.

EDIT

Just forgot to mention that String is a final class which can't be modified or inherited in your application.

public Static SubClass getInstance(String str){
   SubClass obj = new SubClass(); 
   // ***the choice of constructor depends upon how u have created ur class
   //use str to form the SubClass object in following lines 
   return obj;
}

than you can use it as

SubClass newObj = SubClass.getInstance("String value here");

You should be having a concrete class(non abstract class) extending ConsolidateAccount lets say it being

public class ConcreteAccount extends ConsolidateAccount

Then you can use something like this.

ConsolidateAccount ownerAcct = new ConcreteAccount(array[3]);
new SavAcct(array[1],array[2],ownerAcct,Double.parseDouble(array[4]);

and create a constructor public ConcreteAccount(String str) in your ConcreteAccount class

Just to be sure you're not going on a wrong path, instead of adding a new constructor which will essentially need to call super(acctNo,name,ownerAcct,lastMonthBal); like this existing constructor, why don't you otherwise try and create or look up ConsolidateAccount instance using your array[3] key?

Eg

ConsolidateAccount consolidateAccount = new ConcreteConsolidateAccount(array[3]);
new SavAcct(array[1],array[2],consolidateAccount,Double.parseDouble(array[4]);

Where ConcreteConsolidateAccount is a concrete class extending ConsolidateAccount .

Looks like a more sensible thing to do.

Of course I don't know about logic around ConsolidateAccount , or even if it has a constructor that takes a String , but this is just to give you an idea, because it would appear that you need to call the constructor of the class that SavAcct is extending (this is indicated by the super call).

You can't do that because String class is final. Can you make composition instead of inheritance? http://www.artima.com/designtechniques/compoinh.html

不可能,String是最终的。

The java.lang.String class is final , so there's no way you can extend it. You could at most wrap it.

See Can I add new methods to the String class in Java?

You have an array of Strings and the constructor you are calling needs ConsolidateAccount at the third position. You need to instantiate a subtype of ConsolidateAccount based on that third String . There are no details to help you how to do that. Also, you are indexing the array starting from 1. Arrays are zero-based.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM