欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java String類(lèi)型對(duì)象轉(zhuǎn)換為自定義類(lèi)型對(duì)象的實(shí)現(xiàn)

 更新時(shí)間:2023年06月05日 10:56:27   作者:飛滕人生TYF  
本文主要介紹了java String類(lèi)型對(duì)象轉(zhuǎn)換為自定義類(lèi)型對(duì)象的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

問(wèn)題

java String類(lèi)型對(duì)象轉(zhuǎn)換為自定義類(lèi)型對(duì)象

詳細(xì)問(wèn)題

對(duì)于java自定義類(lèi)型對(duì)象提供了toString()方法,實(shí)現(xiàn)自定義類(lèi)型對(duì)象轉(zhuǎn)換為String類(lèi)型對(duì)象,如何將String類(lèi)型對(duì)象轉(zhuǎn)換為自定義類(lèi)型對(duì)象,譬如對(duì)于如下代碼所定義的Class類(lèi)

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類(lèi)來(lái)實(shí)現(xiàn)從字符串到自定義類(lèi)對(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)行匹配,然后通過(guò)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)));

作用:如果匹配成功,通過(guò)matcher.group()方法提取匹配到的屬性值,并將其轉(zhuǎn)換為相應(yīng)的類(lèi)型(如整數(shù)、雙精度浮點(diǎn)數(shù)、字符串等),然后使用對(duì)應(yīng)的set方法將屬性值設(shè)置到Class對(duì)象中。

5、返回解析后的Class對(duì)象:

eturn obj;

作用:返回經(jīng)過(guò)解析后的Class對(duì)象。

請(qǐng)注意,在使用這段代碼時(shí),確保輸入的字符串與正則表達(dá)式的格式匹配,并且屬性值的類(lèi)型與代碼中的設(shè)置相匹配。如果輸入的字符串不符合預(yù)期的格式,或者屬性值的類(lèi)型轉(zhuǎn)換失敗,可能會(huì)導(dǎo)致運(yùn)行時(shí)異常。

到此這篇關(guān)于java String類(lèi)型對(duì)象轉(zhuǎn)換為自定義類(lèi)型對(duì)象的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java String轉(zhuǎn)換為自定義類(lèi)型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論