java String類型對(duì)象轉(zhuǎn)換為自定義類型對(duì)象的實(shí)現(xiàn)
問題
java String類型對(duì)象轉(zhuǎn)換為自定義類型對(duì)象
詳細(xì)問題
對(duì)于java自定義類型對(duì)象提供了toString()方法,實(shí)現(xiàn)自定義類型對(duì)象轉(zhuǎn)換為String類型對(duì)象,如何將String類型對(duì)象轉(zhuǎn)換為自定義類型對(duì)象,譬如對(duì)于如下代碼所定義的Class類
package com.iflytek.bms.domain;
import java.math.BigDecimal;
import java.sql.Timestamp;
public class Class {
? ? private Integer integer;
? ? private Double aDouble;
? ? private String string;
? ? private Timestamp timestamp;
? ? private BigDecimal bigDecimal;
? ? public Integer getInteger() {
? ? ? ? return integer;
? ? }
? ? public void setInteger(Integer integer) {
? ? ? ? this.integer = integer;
? ? }
? ? public Double getaDouble() {
? ? ? ? return aDouble;
? ? }
? ? public void setaDouble(Double aDouble) {
? ? ? ? this.aDouble = aDouble;
? ? }
? ? public String getString() {
? ? ? ? return string;
? ? }
? ? public void setString(String string) {
? ? ? ? this.string = string;
? ? }
? ? public Timestamp getTimestamp() {
? ? ? ? return timestamp;
? ? }
? ? public void setTimestamp(Timestamp timestamp) {
? ? ? ? this.timestamp = timestamp;
? ? }
? ? public BigDecimal getBigDecimal() {
? ? ? ? return bigDecimal;
? ? }
? ? public void setBigDecimal(BigDecimal bigDecimal) {
? ? ? ? this.bigDecimal = bigDecimal;
? ? }
? ? @Override
? ? public String toString() {
? ? ? ? return "Class{" +
? ? ? ? ? ? ? ? "integer=" + integer +
? ? ? ? ? ? ? ? ", aDouble=" + aDouble +
? ? ? ? ? ? ? ? ", string='" + string + '\'' +
? ? ? ? ? ? ? ? ", timestamp=" + timestamp +
? ? ? ? ? ? ? ? ", bigDecimal=" + bigDecimal +
? ? ? ? ? ? ? ? '}';
? ? }
}解決方案
package com.iflytek.bms.domain;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Class {
? ? private Integer integer;
? ? private Double aDouble;
? ? private String string;
? ? private Timestamp timestamp;
? ? private BigDecimal bigDecimal;
? ? public Integer getInteger() {
? ? ? ? return integer;
? ? }
? ? public void setInteger(Integer integer) {
? ? ? ? this.integer = integer;
? ? }
? ? public Double getaDouble() {
? ? ? ? return aDouble;
? ? }
? ? public void setaDouble(Double aDouble) {
? ? ? ? this.aDouble = aDouble;
? ? }
? ? public String getString() {
? ? ? ? return string;
? ? }
? ? public void setString(String string) {
? ? ? ? this.string = string;
? ? }
? ? public Timestamp getTimestamp() {
? ? ? ? return timestamp;
? ? }
? ? public void setTimestamp(Timestamp timestamp) {
? ? ? ? this.timestamp = timestamp;
? ? }
? ? public BigDecimal getBigDecimal() {
? ? ? ? return bigDecimal;
? ? }
? ? public void setBigDecimal(BigDecimal bigDecimal) {
? ? ? ? this.bigDecimal = bigDecimal;
? ? }
? ? @Override
? ? public String toString() {
? ? ? ? return "Class{" +
? ? ? ? ? ? ? ? "integer=" + integer +
? ? ? ? ? ? ? ? ", aDouble=" + aDouble +
? ? ? ? ? ? ? ? ", string='" + string + '\'' +
? ? ? ? ? ? ? ? ", timestamp=" + timestamp +
? ? ? ? ? ? ? ? ", bigDecimal=" + bigDecimal +
? ? ? ? ? ? ? ? '}';
? ? }
? ? public static Class fromString(String str) {
? ? ? ? Class obj = new Class();
? ? ? ? Pattern pattern = Pattern.compile("Class\\{integer=(\\d+), aDouble=(\\d+\\.\\d+), string='(.*?)', timestamp=(.*?), bigDecimal=(\\d+\\.\\d+)\\}");
? ? ? ? Matcher matcher = pattern.matcher(str);
? ? ? ? if (matcher.matches()) {
? ? ? ? ? ? obj.setInteger(Integer.parseInt(matcher.group(1)));
? ? ? ? ? ? obj.setaDouble(Double.parseDouble(matcher.group(2)));
? ? ? ? ? ? obj.setString(matcher.group(3));
? ? ? ? ? ? obj.setTimestamp(Timestamp.valueOf(matcher.group(4)));
? ? ? ? ? ? obj.setBigDecimal(new BigDecimal(matcher.group(5)));
? ? ? ? }
? ? ? ? return obj;
? ? }
}解決原因
筆者使用正則表達(dá)式和Java的Matcher類來實(shí)現(xiàn)從字符串到自定義類對(duì)象的轉(zhuǎn)換,下面筆者將對(duì)fromString方法進(jìn)行詳細(xì)解析
1、創(chuàng)建一個(gè)新的Class對(duì)象:
Class obj = new Class();
作用:創(chuàng)建一個(gè)新的Class對(duì)象,用于存儲(chǔ)從字符串中解析的屬性值。
2、編譯正則表達(dá)式并創(chuàng)建Pattern對(duì)象:
Pattern pattern = Pattern.compile("Class\\{integer=(\\d+), aDouble=(\\d+\\.\\d+), string='(.*?)', timestamp=(.*?), bigDecimal=(\\d+\\.\\d+)\\}");使用正則表達(dá)式"Class\{integer=(\d+), aDouble=(\d+\.\d+), string='(.?)', timestamp=(.?), bigDecimal=(\d+\.\d+)\}"編譯創(chuàng)建一個(gè)Pattern對(duì)象,用于匹配包含Class對(duì)象屬性的字符串。
3、創(chuàng)建Matcher對(duì)象并進(jìn)行匹配:
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
// 執(zhí)行屬性值的提取和設(shè)置
}作用:使用創(chuàng)建的Pattern對(duì)象對(duì)輸入的字符串str進(jìn)行匹配,然后通過matcher.matches()方法判斷是否匹配成功。
4、提取并設(shè)置屬性值:
obj.setInteger(Integer.parseInt(matcher.group(1))); obj.setaDouble(Double.parseDouble(matcher.group(2))); obj.setString(matcher.group(3)); obj.setTimestamp(Timestamp.valueOf(matcher.group(4))); obj.setBigDecimal(new BigDecimal(matcher.group(5)));
作用:如果匹配成功,通過matcher.group()方法提取匹配到的屬性值,并將其轉(zhuǎn)換為相應(yīng)的類型(如整數(shù)、雙精度浮點(diǎn)數(shù)、字符串等),然后使用對(duì)應(yīng)的set方法將屬性值設(shè)置到Class對(duì)象中。
5、返回解析后的Class對(duì)象:
eturn obj;
作用:返回經(jīng)過解析后的Class對(duì)象。
請(qǐng)注意,在使用這段代碼時(shí),確保輸入的字符串與正則表達(dá)式的格式匹配,并且屬性值的類型與代碼中的設(shè)置相匹配。如果輸入的字符串不符合預(yù)期的格式,或者屬性值的類型轉(zhuǎn)換失敗,可能會(huì)導(dǎo)致運(yùn)行時(shí)異常。
到此這篇關(guān)于java String類型對(duì)象轉(zhuǎn)換為自定義類型對(duì)象的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java String轉(zhuǎn)換為自定義類型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java中char數(shù)組(字符數(shù)組)與字符串String類型的轉(zhuǎn)換方法
- Java List轉(zhuǎn)換成String數(shù)組幾種實(shí)現(xiàn)方式詳解
- java對(duì)象轉(zhuǎn)換String類型的三種方法
- Java如何將String轉(zhuǎn)換成json對(duì)象或json數(shù)組
- Java string類型轉(zhuǎn)換成map代碼實(shí)例
- java將String字符串轉(zhuǎn)換為List<Long>類型實(shí)例方法
- Java String轉(zhuǎn)換時(shí)為null的解決方法
- java string類型轉(zhuǎn)換boolean類型的方法
- java判斷String類型是否能轉(zhuǎn)換為int的方法
相關(guān)文章
Java動(dòng)態(tài)代理機(jī)制詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java動(dòng)態(tài)代理機(jī)制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
一個(gè)MIDP俄羅斯方塊游戲的設(shè)計(jì)和實(shí)現(xiàn)
一個(gè)MIDP俄羅斯方塊游戲的設(shè)計(jì)和實(shí)現(xiàn)...2006-12-12
java:無法訪問org.springframework.boot.SpringApplication的解決方法
這篇文章主要給大家介紹了關(guān)于java:無法訪問org.springframework.boot.SpringApplication的解決方法,文中通過實(shí)例代碼將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
詳解Java虛擬機(jī)30個(gè)常用知識(shí)點(diǎn)之1——類文件結(jié)構(gòu)
這篇文章主要介紹了Java虛擬機(jī)類文件結(jié)構(gòu),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

