国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

主類型的過載

2019-11-18 12:09:58
字體:
供稿:網(wǎng)友

  主(數(shù)據(jù))類型能從一個“較小”的類型自動轉(zhuǎn)變成一個“較大”的類型。涉及過載問題時,這會稍微造成一些混亂。下面這個例子揭示了將主類型傳遞給過載的方法時發(fā)生的情況:
  
  //: PRimitiveOverloading.java
  // Promotion of primitives and overloading
  
  public class PrimitiveOverloading {
   // boolean can't be automatically converted
   static void prt(String s) {
    System.out.println(s);
   }
  
   void f1(char x) { prt("f1(char)"); }
   void f1(byte x) { prt("f1(byte)"); }
   void f1(short x) { prt("f1(short)"); }
   void f1(int x) { prt("f1(int)"); }
   void f1(long x) { prt("f1(long)"); }
   void f1(float x) { prt("f1(float)"); }
   void f1(double x) { prt("f1(double)"); }
  
   void f2(byte x) { prt("f2(byte)"); }
   void f2(short x) { prt("f2(short)"); }
   void f2(int x) { prt("f2(int)"); }
   void f2(long x) { prt("f2(long)"); }
   void f2(float x) { prt("f2(float)"); }
   void f2(double x) { prt("f2(double)"); }
  
   void f3(short x) { prt("f3(short)"); }
   void f3(int x) { prt("f3(int)"); }
   void f3(long x) { prt("f3(long)"); }
   void f3(float x) { prt("f3(float)"); }
   void f3(double x) { prt("f3(double)"); }
  
   void f4(int x) { prt("f4(int)"); }
   void f4(long x) { prt("f4(long)"); }
   void f4(float x) { prt("f4(float)"); }
   void f4(double x) { prt("f4(double)"); }
  
   void f5(long x) { prt("f5(long)"); }
   void f5(float x) { prt("f5(float)"); }
   void f5(double x) { prt("f5(double)"); }
  
   void f6(float x) { prt("f6(float)"); }
   void f6(double x) { prt("f6(double)"); }
  
   void f7(double x) { prt("f7(double)"); }
  
   void testConstVal() {
    prt("Testing with 5");
    f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5);
   }
   void testChar() {
    char x = 'x';
    prt("char argument:");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
   }
   void testByte() {
    byte x = 0;
    prt("byte argument:");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
   }
   void testShort() {
    short x = 0;
    prt("short argument:");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
   }
   void testInt() {
    int x = 0;
    prt("int argument:");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
   }
   void testLong() {
    long x = 0;
    prt("long argument:");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
   }
   void testFloat() {
    float x = 0;
    prt("float argument:");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
   }
   void testDouble() {
    double x = 0;
    prt("double argument:");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
   }
   public static void main(String[] args) {
    PrimitiveOverloading p =
     new PrimitiveOverloading();
    p.testConstVal();
    p.testChar();
    p.testByte();
    p.testShort();
    p.testInt();
    p.testLong();
    p.testFloat();
    p.testDouble();
   }
  } ///:~
  
  若觀察這個程序的輸出,就會發(fā)現(xiàn)常數(shù)值5被當(dāng)作一個int值處理。所以假若可以使用一個過載的方法,就能獲取它使用的int值。在其他所有情況下,若我們的數(shù)據(jù)類型“小于”方法中使用的自變量,就會對那種數(shù)據(jù)類型進(jìn)行“轉(zhuǎn)型”處理。char獲得的效果稍有些不同,這是由于假期它沒有發(fā)現(xiàn)一個準(zhǔn)確的char匹配,就會轉(zhuǎn)型為int。
  若我們的自變量“大于”過載方法期望的自變量,這時又會出現(xiàn)什么情況呢?對前述程序的一個修改揭示出了答案:
  
  
  //: Demotion.java
  // Demotion of primitives and overloading
  
  public class Demotion {
   static void prt(String s) {
    System.out.println(s);
   }
  
   void f1(char x) { prt("f1(char)"); }
   void f1(byte x) { prt("f1(byte)"); }
   void f1(short x) { prt("f1(short)"); }
   void f1(int x) { prt("f1(int)"); }
   void f1(long x) { prt("f1(long)"); }
   void f1(float x) { prt("f1(float)"); }
   void f1(double x) { prt("f1(double)"); }
  
   void f2(char x) { prt("f2(char)"); }
   void f2(byte x) { prt("f2(byte)"); }
   void f2(short x) { prt("f2(short)"); }
   void f2(int x) { prt("f2(int)"); }
   void f2(long x) { prt("f2(long)"); }
   void f2(float x) { prt("f2(float)"); }
  
   void f3(char x) { prt("f3(char)"); }
   void f3(byte x) { prt("f3(byte)"); }
   void f3(short x) { prt("f3(short)"); }
   void f3(int x) { prt("f3(int)"); }
   void f3(long x) { prt("f3(long)"); }
  
   void f4(char x) { prt("f4(char)"); }
   void f4(byte x) { prt("f4(byte)"); }
   void f4(short x) { prt("f4(short)"); }
   void f4(int x) { prt("f4(int)"); }
  
   void f5(char x) { prt("f5(char)"); }
   void f5(byte x) { prt("f5(byte)"); }
   void f5(short x) { prt("f5(short)"); }
  
   void f6(char x) { prt("f6(char)"); }
   void f6(byte x) { prt("f6(byte)"); }
  
   void f7(char x) { prt("f7(char)"); }
  
   void testDouble() {
    double x = 0;
    prt("double argument:");
    f1(x);f2((float)x);f3((long)x);f4((int)x);
    f5((short)x);f6((byte)x);f7((char)x);
   }
   public static void main(String[] args) {
    Demotion p = new Demotion();
    p.testDouble();
   }
  } ///:~
  
  在這里,方法采用了容量更小、范圍更窄的主類型值。若我們的自變量范圍比它寬,就必須用括號中的類型名將其轉(zhuǎn)為適當(dāng)?shù)念愋汀<偃绮贿@樣做,編譯器會報(bào)告出錯。
  大家可注重到這是一種“縮小轉(zhuǎn)換”。也就是說,在造型或轉(zhuǎn)型過程中可能丟失一些信息。這正是編譯器強(qiáng)迫我們明確定義的原因——我們需明確表達(dá)想要轉(zhuǎn)型的愿望。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 中宁县| 广昌县| 北海市| 阳谷县| 应城市| 文成县| 西华县| 宽甸| 天祝| 临颍县| 普洱| 无极县| 尼木县| 文水县| 慈利县| 乌恰县| 曲阳县| 桓台县| 柳河县| 泰来县| 东乌珠穆沁旗| 大理市| 红安县| 互助| 诸城市| 成安县| 巫溪县| 连江县| 保亭| 保康县| 陇西县| 昌邑市| 五华县| 罗田县| 阜新| 资源县| 奉化市| 青冈县| 柳林县| 盐城市| 岚皋县|