久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<small id='P84mg'></small><noframes id='P84mg'>

  • <legend id='P84mg'><style id='P84mg'><dir id='P84mg'><q id='P84mg'></q></dir></style></legend>
      <bdo id='P84mg'></bdo><ul id='P84mg'></ul>

    <i id='P84mg'><tr id='P84mg'><dt id='P84mg'><q id='P84mg'><span id='P84mg'><b id='P84mg'><form id='P84mg'><ins id='P84mg'></ins><ul id='P84mg'></ul><sub id='P84mg'></sub></form><legend id='P84mg'></legend><bdo id='P84mg'><pre id='P84mg'><center id='P84mg'></center></pre></bdo></b><th id='P84mg'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='P84mg'><tfoot id='P84mg'></tfoot><dl id='P84mg'><fieldset id='P84mg'></fieldset></dl></div>

    1. <tfoot id='P84mg'></tfoot>

        如何從 String 轉(zhuǎn)換為原始類型或標(biāo)準(zhǔn) java Wrapper 類

        How to convert from String to a primitive type or standard java Wrapper types(如何從 String 轉(zhuǎn)換為原始類型或標(biāo)準(zhǔn) java Wrapper 類型)
      1. <tfoot id='rCMnp'></tfoot>

        <legend id='rCMnp'><style id='rCMnp'><dir id='rCMnp'><q id='rCMnp'></q></dir></style></legend>
          <tbody id='rCMnp'></tbody>
        <i id='rCMnp'><tr id='rCMnp'><dt id='rCMnp'><q id='rCMnp'><span id='rCMnp'><b id='rCMnp'><form id='rCMnp'><ins id='rCMnp'></ins><ul id='rCMnp'></ul><sub id='rCMnp'></sub></form><legend id='rCMnp'></legend><bdo id='rCMnp'><pre id='rCMnp'><center id='rCMnp'></center></pre></bdo></b><th id='rCMnp'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='rCMnp'><tfoot id='rCMnp'></tfoot><dl id='rCMnp'><fieldset id='rCMnp'></fieldset></dl></div>

              <bdo id='rCMnp'></bdo><ul id='rCMnp'></ul>

                <small id='rCMnp'></small><noframes id='rCMnp'>

                1. 本文介紹了如何從 String 轉(zhuǎn)換為原始類型或標(biāo)準(zhǔn) java Wrapper 類型的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有一個(gè) java.lang.reflect.InvocationHandler 我需要實(shí)現(xiàn)方法 invoke()

                  I have a java.lang.reflect.InvocationHandler and I need to implement the method invoke()

                  我的詳細(xì)說明中有一個(gè) java.lang.String 類型的值,我需要將此值轉(zhuǎn)換為該方法期望的適當(dāng) returnType(它可以是一個(gè)原始類型,如 int、boolean、double 或包裝類,如 Boolean、Integer、Double、Float 等).

                  I have a value of type java.lang.String from my elaboration and I need to convert this value to the appropriate returnType expected by the method (it can be a primitive like int, boolean, double or wrapper classes like Boolean, Integer, Double, Float, etc).

                  例子:

                  public Object invoke(Object proxy, Method method, Object[] args) 
                          throws Throwable {
                      String computedValue = compute(...);
                      return convert(method.getReturnType(), computedValue);
                  }
                  
                  private Object convert(Class<?> returnType, String stringValue) {
                      return ...; // what's the simplest way?
                  }
                  

                  我不希望簡(jiǎn)單地實(shí)現(xiàn)復(fù)雜對(duì)象之間的自動(dòng)轉(zhuǎn)換,但我希望有一種簡(jiǎn)單的方法來從 String 轉(zhuǎn)換為標(biāo)準(zhǔn) java 類型.

                  I am not expecting to simply implement an automatic conversion between complex objects, but I expect a simple way to convert from String to the standard java types.

                  我已經(jīng)(太)見過很多次這樣的東西,但它似乎不適合我:

                  I've seen (too) many times stuff like this, but it doesn't seem appropriate to me:

                  public static Object toObject( Class clazz, String value ) {
                      if( Boolean.class.isAssignableFrom( clazz ) ) return Boolean.parseBoolean( value );
                      if( Byte.class.isAssignableFrom( clazz ) ) return Byte.parseByte( value );
                      if( Short.class.isAssignableFrom( clazz ) ) return Short.parseShort( value );
                      if( Integer.class.isAssignableFrom( clazz ) ) return Integer.parseInteger( value );
                      if( Long.class.isAssignableFrom( clazz ) ) return Long.parseLong( value );
                      if( Float.class.isAssignableFrom( clazz ) ) return Float.parseFloat( value );
                      if( Double.class.isAssignableFrom( clazz ) ) return Double.parseDouble( value );
                      return value;
                  }
                  

                  到目前為止,以上還不是我看到的最糟糕的一個(gè):)

                  and the above is not even the worse one I saw, so far :)

                  這里有什么秘訣嗎?

                  推薦答案

                  據(jù)我所知,您提供的版本沒有真正的替代方案.您可以稍微簡(jiǎn)化一下(因?yàn)榘b器類型都是 final),但您本質(zhì)上需要使用 ifswitch 或散列來切換在課堂上.

                  As far as I'm aware, there is no real alternative to the version you presented. You can simplify it a bit (since the wrapper types are all final), but you essentially need to use if or switch or hashing to switch on the class.

                  我的建議是像上面那樣編寫代碼.丑陋的代碼本身只是一個(gè)問題,如果你不得不看的話.所以把它放在一個(gè)實(shí)用方法中,不要再看它了.

                  My advice is to code it like the above. Ugly code is only a problem per se if you have to look at it. So put it inside a utility method and don't look at it again.

                  FWIW - 這是我簡(jiǎn)化方法的方式:

                  FWIW - this is how I'd simplify the method:

                  public static Object toObject( Class clazz, String value ) {
                      if( Boolean.class == clazz ) return Boolean.parseBoolean( value );
                      if( Byte.class == clazz ) return Byte.parseByte( value );
                      if( Short.class == clazz ) return Short.parseShort( value );
                      if( Integer.class == clazz ) return Integer.parseInt( value );
                      if( Long.class == clazz ) return Long.parseLong( value );
                      if( Float.class == clazz ) return Float.parseFloat( value );
                      if( Double.class == clazz ) return Double.parseDouble( value );
                      return value;
                  }
                  

                  這樣更簡(jiǎn)單、更高效.并且它等同于原始版本,因?yàn)轭惗际?final 并且因?yàn)橐?guī)范聲明 Class 對(duì)象的相等性是對(duì)象身份.

                  This is simpler and more efficient. And it is equivalent to the original version because the classes are all final and because the specs state that equality for Class objects is object identity.

                  可以說,我們應(yīng)該使用直接返回包裝器對(duì)象的 <wrapper>.valueOf(String) 方法.

                  Arguably, we should be using the <wrapper>.valueOf(String) methods which return the wrapper objects directly.

                  我并沒有聲稱這不那么丑……但是美"并不是衡量代碼質(zhì)量的有用指標(biāo),因?yàn)樗侵饔^的,因?yàn)樗荒芨嬖V您代碼是否易于理解和/或維護(hù).

                  I make no claim that this is less ugly ... but "beauty" is not a useful measure of code quality, because it is subjective and because it doesn't tell you whether the code is easy to understand and / or maintain.

                  更新

                  為了也支持原始類型,將相應(yīng)的類添加到 if 條件中;例如

                  To support primitive types as well, add the corresponding classes to the if conditions; e.g.

                      if (Boolean.class == clazz || Boolean.TYPE == clazz) {
                          return Boolean.parseBoolean(value);
                      }
                  

                  現(xiàn)在可能已經(jīng)到了在類型名稱上進(jìn)行 String 切換更有效的地步,盡管需要考慮一些稍微棘手的類型標(biāo)識(shí)問題.(理論上,您可以擁有由不同類加載器加載的具有相同全名的多個(gè)類型.我認(rèn)為您需要在類加載器中快速而松散地"使用原始包裝類來做到這一點(diǎn)......但是我認(rèn)為這仍然是可能的.)

                  It may now be getting to the point where doing a String switch on the type's name is more efficient, though there are some slightly knotty issues of type identity that need to be thought through. (In theory, you can have multiple types with the same full name that have been loaded by different classloaders. I think you'd need to "play fast and loose" in a classloader to do that with the primitive wrapper classes ... but I think it might still be possible.)

                  這篇關(guān)于如何從 String 轉(zhuǎn)換為原始類型或標(biāo)準(zhǔn) java Wrapper 類型的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用錯(cuò)誤)
                  Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 語句 - 是“或/“和可能的?)
                  Java Replace Character At Specific Position Of String?(Java替換字符串特定位置的字符?)
                  What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作數(shù)的三元表達(dá)式的類型是什么?)
                  Read a text file and store every single character occurrence(讀取文本文件并存儲(chǔ)出現(xiàn)的每個(gè)字符)
                  Why do I need to explicitly cast char primitives on byte and short?(為什么我需要在 byte 和 short 上顯式轉(zhuǎn)換 char 原語?)
                  <i id='iInBM'><tr id='iInBM'><dt id='iInBM'><q id='iInBM'><span id='iInBM'><b id='iInBM'><form id='iInBM'><ins id='iInBM'></ins><ul id='iInBM'></ul><sub id='iInBM'></sub></form><legend id='iInBM'></legend><bdo id='iInBM'><pre id='iInBM'><center id='iInBM'></center></pre></bdo></b><th id='iInBM'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='iInBM'><tfoot id='iInBM'></tfoot><dl id='iInBM'><fieldset id='iInBM'></fieldset></dl></div>
                      <tbody id='iInBM'></tbody>

                    <small id='iInBM'></small><noframes id='iInBM'>

                          <tfoot id='iInBM'></tfoot>
                        • <legend id='iInBM'><style id='iInBM'><dir id='iInBM'><q id='iInBM'></q></dir></style></legend>

                            <bdo id='iInBM'></bdo><ul id='iInBM'></ul>
                            主站蜘蛛池模板: 国产精品国产精品国产专区不片 | 男女在线网站 | 国产精品久久久免费 | 欧美在线a| 免费在线国产视频 | 欧美区日韩区 | 91 在线| 小视频你懂得 | 国产精品久久 | 久久久国产一区二区 | 国产激情91久久精品导航 | 国产乱码精品一区二三赶尸艳谈 | 亚洲不卡| 国产一区久久 | 天堂网色 | 欧美福利视频 | 成人免费三级电影 | 国产女人与拘做视频免费 | 成人av一区二区三区 | 久久国产精品免费一区二区三区 | 国产精品免费av | 成年人免费网站 | 久久久久久久电影 | 日韩精品一区二区三区 | 国产精品99久久久久久久久久久久 | 1000部精品久久久久久久久 | 国产精品久久久久久久久久久久久 | 天堂精品 | 国产福利在线视频 | 婷婷成人在线 | 99re在线播放 | 中文字幕av第一页 | 91免费版在线观看 | 日韩精品视频在线 | 久久午夜剧场 | 九九久视频 | 日韩一区二区三区在线 | 国产精品成人一区二区三区 | 涩涩导航| 成人免费网站视频 | 午夜精品久久久久久 |