zoukankan      html  css  js  c++  java
  • java中的包注意事项

    1:需要导入包的三个地方

      a:需要导入第三方的jar包中的类或接口

     b:需要导入除了java.lang包的其他包中的类(jdk中的类)

     c:需要导入自己写的不同包的类

    2:引入包的三种方式

    a:import 包名.类名(最常用的方式)

    b:import 包名.*,这种方式将向类中导入该包中的所有公共类

    c:直接  包名.类名 修饰变量   例如: java.util.Date date = new java.util.Date();

    下面的代码会帮助你

     1 package com.baidu.test;
     2 
     3 import java.text.SimpleDateFormat;
     4 import java.util.Date;//需要导入包的第三个地方:需要引用除了java.lang包的其他包中jdk带的类
     5 import com.baidu.tool.PinYinTool;//需要导包的第二个地方:需要使用在不同包的类
     6 
     7 public class Test {
     8     
     9     public static void main(String[] args) {
    10         String cnStr = "李银霞";
    11         System.out.println(PinYinTool.getPinYinAllChar(cnStr, true));    //输出“LIUFEIFEI”
    12         System.out.println(PinYinTool.getPinYinAllChar(cnStr, false));   //输出“liufeifei”
    13         java.util.Date date= new java.util.Date();
    14         SimpleDateFormat sdf = new SimpleDateFormat("");
    15     }
    16 }
     1 package com.baidu.tool;
     2 //需要导入包的第一个地方:需要使用第三方的jar包中的类或接口
     3 import net.sourceforge.pinyin4j.PinyinHelper;
     4 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
     5 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
     6 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
     7 import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
     8 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
     9 
    10 public class PinYinTool {
    11     
    12     /**
    13      * 将汉字转换为全拼的拼音。
    14      * 
    15      * @param isCapital true表示返回大写形式,false表示返回小写形式。
    16      */
    17     public static String getPinYinAllChar(String zn_str, boolean isCapital) {
    18         char[] strChar = zn_str.toCharArray();
    19         HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();
    20         // 输出设置,大小写,音标方式等
    21         if(isCapital) {
    22             hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    23         } else {
    24             hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    25         }
    26         hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    27         hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
    28        
    29         StringBuffer pyStringBuffer = new StringBuffer();
    30         String[] strString = new String[strChar.length];
    31         try {
    32             for (int i = 0; i < strChar.length; i++) {
    33                 if (Character.toString(strChar[i]).matches("[\u4E00-\u9FA5]+")) {//如果是汉字字符
    34                     strString = PinyinHelper.toHanyuPinyinStringArray(strChar[i], hanYuPinOutputFormat);//将汉字的几种全拼都存到strString数组中
    35                     pyStringBuffer.append(strString[0]);//取出该汉字全拼的第一种读音并连接到字符串pyStringBuffer后
    36                 } else {//如果不是汉字字符,直接取出字符并连接到字符串pyStringBuffer后
    37                     pyStringBuffer.append(Character.toString(strChar[i]));
    38                 }
    39             }
    40         } catch (BadHanyuPinyinOutputFormatCombination e) {
    41             e.printStackTrace();
    42         }
    43         return pyStringBuffer.toString();
    44     }
    45 }

    上面的两个类在不同的包中, 所以使用时需要import导入包! ! 

    同时也用到了第三方中的jar包中的类, 需要导入包

    Date也用到了sun公司写的类, 需要导入包

    注意! 在包中的类不允许导入默认包中的类! !

  • 相关阅读:
    用jmeter进行多用户并发压力测试 [转]
    jmeter 压力测试 参数
    web测试 结果存储类型为“Database”,但尚未指定结果储存库连接字符串
    apache配置多站点
    seajs + easyui [转]
    js,this,constrct ,prototype
    js call apply
    js this [转]
    [leetcode] Longest Common Prefix @ Python
    [leetcode] path sum @ Python [recursion]
  • 原文地址:https://www.cnblogs.com/lyxcode/p/9037925.html
Copyright © 2011-2022 走看看