需求:把名字变成拼音,然后用空格分割每个字

我学生用面向过程的思想来编实现这个类,我把他的代码稍微整理了下,下面是他的代码:

pom.xml

先要去导入一个三方jar,用于转化拼音

     <dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId><version>2.5.0</version></dependency>

PinyinTool

package org.chb.test;import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;/*** * Created by 33css*/
public class PinyinTool {HanyuPinyinOutputFormat format = null;public PinyinTool() {format = new HanyuPinyinOutputFormat();format.setCaseType(HanyuPinyinCaseType.UPPERCASE);format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);}/*** 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换 如: 明天 转换成 MINGTIAN* * @param content* @return* @throws BadHanyuPinyinOutputFormatCombination*/public String toPinYin(String content, HanyuPinyinOutputFormat format)throws BadHanyuPinyinOutputFormatCombination {if (content == null || content.trim().length() == 0)return "";StringBuffer result = new StringBuffer();for (int i = 0; i < content.length(); i++) {char c = content.charAt(i);result.append(getPinyin(c, format)).append(" ");}return result.toString();}/*** 把传入的汉字变成拼音* @param content 汉字内容* @param format 拼音格式* @return*/private String getPinyin(char content, HanyuPinyinOutputFormat format) {if ((int) content <= 128) {//如果小于128 说明不是中文汉字 则直接返回return String.valueOf(content);} else {try {return PinyinHelper.toHanyuPinyinStringArray(content, format)[0];//toHanyuPinyinStringArray多音字 会获取到数组 使用第一个字} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();}}return null;}
}

test测试该功能

package org.chb.test;import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;public class Test {public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {PinyinTool tool = new PinyinTool();System.out.println(tool.toPinYin("罗若峰", createFormat()));}/*** 创建样式* @return*/public static HanyuPinyinOutputFormat createFormat() {HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();format.setCaseType(HanyuPinyinCaseType.LOWERCASE);format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);return format;}
}
这样写代码好吗?如果现在有了新的需求:
想把姓 放在名的后面,或者是一些其他需求,你就会去pinyinTool这个类中写,更多的方法,需求越多,就会变得越冗长,你会发现面相过程编程,会变成一个坑,需求增加的越多,代码就越乱。
在pinyinTool中增加方法
/*** 把名字放到姓的前面* @param content 内容* @return 返回名字在前的字段,如:若峰罗*/private String sortContent(String content) {return content.substring(1)+content.substring(0,1);}
/*** 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换 如: 明天 转换成 MINGTIAN* * @param content* @return* @throws BadHanyuPinyinOutputFormatCombination*/public String toPinYin(String content, HanyuPinyinOutputFormat format)throws BadHanyuPinyinOutputFormatCombination {if (content == null || content.trim().length() == 0)return "";StringBuffer result = new StringBuffer();content = sortContent(content);//修改名和姓的顺序for (int i = 0; i < content.length(); i++) {char c = content.charAt(i);result.append(getPinyin(c, format)).append(" ");}return result.toString();}
如想看面相对象的实现,请看下回分解

JAVA面向对象编程艺术与思想:中文转拼音1相关推荐

  1. JAVA面向对象编程艺术与思想:中文转拼音2

    我现在用面向对象的思想来实现名字的转换 把名字中每个字都看成一个对象,建立类PinyinChar 该类包括字和拼音对象属性 package org.lrf.pinyin;import net.sour ...

  2. java面向对象编程思想的理解

    1.我们总说java是一门面向对象编程的语言,那什么是面向对象呢? 我是这样理解的,对象是事物存在的实体,如,猪.狗,花早等都是对象,对象由两部分组成.面向对象编程的三大特点:继承,多态,类是封装对象 ...

  3. java面向对象编程的思想_java面向对象编程思想的理解

    1.我们总说java是一门面向对象编程的语言,那什么是面向对象呢? 我是这样理解的,对象是事物存在的实体,如,猪.狗,花早等都是对象,对象由两部分组成.面向对象编程的三大特点:继承,多态,类是封装对象 ...

  4. java面向对象编程思想_Java面向对象编程思想的理解

    1.我们总说java是一门面向对象编程的语言,那什么是面向对象呢? 我是这样理解的,对象是事物存在的实体,如,猪.狗,花早等都是对象,对象由两部分组成.面向对象编程的三大特点:继承,多态,类是封装对象 ...

  5. 孙卫琴:我为什么要写《Java面向对象编程》

    孙卫琴:我为什么要写<Java面向对象编程> 特约作者:孙卫琴 策划 & 设计 & 制作:李大微 当<精通Struts>和<精通Hibernate> ...

  6. java面向对象编程基础

    java面向对象编程基础 前言:什么是java 是咖啡飘香的清晨 - 是斯坦福校园意浓情深 - 是James的思想睿智 是剁手党双十一挥舞的利刃 是大数据云计算驰骋的平台 - 是ATM上吐出的钞票 - ...

  7. 八、Java面向对象编程(类、对象、方法、重载、可变参数、作用域、构造器、this本质)

    文章目录 Java面向对象编程(类.对象.方法.重载.可变参数.作用域.构造器.this本质) 一.类与对象 1. 类与对象的引出 2. 使用现有技术解决 3. 现有技术解决的缺点分析 4. 类与对象 ...

  8. java面向对象编程_包_继承_多态_重载和重写_抽象类_接口_this和super

    目录点击跳转 包 包的命名方法 导入包中的类 系统包的介绍 **注意事项** 继承 基础知识 构造方法 **基础语法** `protected`修饰符 组合 `this`和`super`关键字 `th ...

  9. java实验Java面向对象编程_Java实验项目 面向对象编程.doc

    Java实验项目 面向对象编程 Java实验项目二 面向对象编程 第1部分 类与对象 [实验目的] 熟悉Java面向对象程序设计的基本思想. 掌握类与对象的定义及使用方法. 掌握package语句与i ...

最新文章

  1. CCTouchDispatcher sharedDispatcher 方法过期
  2. OpenCV中的透视变换介绍
  3. Arduino可穿戴开发入门教程Arduino开发环境介绍
  4. 数据结构第二版之(课后题)BF算法病毒感染检测
  5. 【XML DOM】解析XML Dom
  6. activeMQ的三种通讯模式
  7. remote: Incorrect username or password ( access token ) fatal: Authentication failed for gitee
  8. celery AttributeError: 'str' object has no attribute 'items'
  9. 思维导图学Java编程思想
  10. php微信段子,年度挤进前十名的微信段子,笑死了
  11. Android Studio3.2经常用的一些依赖(以后再加)
  12. The Clean Architecture--一篇很不错的关于架构的文章
  13. python collections,函数等笔记
  14. 【无标题】IDM + 油猴 + 百度云
  15. libiconv android编译,NDK编译经常使用开源库-libiconv
  16. 深入了解KNN算法原理
  17. IaaS PaaS SaaS DaaS基础设施即服务、平台即服务、软件即服务、数据即服务详解
  18. C++Primer 第9章 顺序容器
  19. 七日杀服务器无限刷空投,七日杀空投作弊代码 | 手游网游页游攻略大全
  20. jQuery 操作整理

热门文章

  1. ubuntu下面玩街机+北通野牛手柄
  2. qemu下的USB直通功能介绍
  3. jacob电脑重启后word/excel转pdf是0KB问题解决方法
  4. Day12 instanceof 内部类
  5. 可爱的头像【 InsCode Stable Diffusion 美图活动一期】
  6. 微信公众号简单开发(10)链接消息
  7. 硬盘容量计算公式为什么是这个【硬盘容量 = 磁柱数量 * 磁头数量 * 扇区数量 * 512Byte】
  8. 利用微搭实现下拉框动态填充值得问题
  9. (36)RuntimeError: Given groups=4, weight of size [4, 1, 11, 11], expected input xxxxxxxxx
  10. 2022电赛小车跟随行驶系统(C题)复盘