程序员转型指南当java遇见了objectivec

上传人:第*** 文档编号:61711253 上传时间:2018-12-10 格式:PDF 页数:18 大小:341.06KB
返回 下载 相关 举报
程序员转型指南当java遇见了objectivec_第1页
第1页 / 共18页
程序员转型指南当java遇见了objectivec_第2页
第2页 / 共18页
程序员转型指南当java遇见了objectivec_第3页
第3页 / 共18页
程序员转型指南当java遇见了objectivec_第4页
第4页 / 共18页
程序员转型指南当java遇见了objectivec_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《程序员转型指南当java遇见了objectivec》由会员分享,可在线阅读,更多相关《程序员转型指南当java遇见了objectivec(18页珍藏版)》请在金锄头文库上搜索。

1、程序员转型指南程序员转型指南 当当 JavaJava 遇见了遇见了 Objective-CObjective-C 作者: 【51CTO 译文】目前在移动开发领域最重要的两个平台分别为 Android 平台和 iOS,在两个平台开发应用分 别要用 Java 和 Objective-C 语言。虽然 Java 和 Objective-C 就像是处在两个不同的世界,但这两种编程 语言以及它们的平台库等等还是有许多相似的地方。本文为 51CTO 独家译文,讲述了外国开发者 Genadiy Shteyman 从 Java 开发转向 Objective-C 需要掌握技能。 以下为全部译文,以下为全部译文,(

2、 (文章中的文章中的“我我”指代指代“Genadiy“Genadiy Shteyman“)Shteyman“): 最近一段时间,我从编写企业 Java 应用转向使用 Objective-C。经过长时间的困扰之后,我发现两者的相 似之处很多,如果能够早些读到相关的文章,转换工作会容易得多。 所以我写下这篇文章,想要帮助 Java 程序员快速的掌握 Objective-C 开发的主要特点。我使用一个社交网 络应用作为例子,演示怎样用这两种语言建立开发环境。例子中会包括创建基本对象与比较两种语言的 MV C 设计模式,还会演示两种语言中数据的存储和获取。 Objective-CObjective-C

3、 开发:从哪里开始开发:从哪里开始 开发 iPhone 应用,首先最好要使用 Mac 电脑。最新的 Mac OS X 10.6 版本通常包含了一份 Xcode IDE,以 及使用 Objective-C 的配套 iPhone 开发软件工具套装(图表一)。 498)this.wid th=498;“ height=289 图表一:图表一:XcodeXcode IDEIDE 开发环境,项目视图开发环境,项目视图 2010 年 11 月,苹果发布了期待已久的 iOS SDK 4.2,其中包含了丰富的框架和功能,用来搭建互动 iPhon e 应用。Xcode 还包含了一个仿真器,可以让你在电脑中模拟程

4、序运行在手机上的效果。 Objective-C 是 iPhone 应用的主要开发语言。对 Java 开发者来说,幸运的是 Objective-C 是完全面向对 象的,使用和其他 OO 语言相同的理念继承、多态和封装等等。定义一个类(Objective-C 中称为 modu le 或.m 文件),首先要定义一个接口(一个 header 或.h 文件),然后把它引入到类中。 我们来看这个社交网络应用的例子,这个应用需要建立一个联系册,让你和朋友们时常保持联系。朋友的 档案存储在 FriendProfile 对象中,包含四个字段:朋友的名字、城市、国家和电话号码,如 Listing On e 所示:

5、 1.ListingOne 2./FriendProfile.h 3.#import 4.#import 5. 6.interfaceFriendProfile:NSObject 7. 8.property(nonatomic,retain)NSString*name; 9.property(nonatomic,retain)NSString*country; 10.property(nonatomic,retain)NSString*city; 11.property(nonatomic,retain)NSString*phoneNbr; 12.end 13./FriendProfile.m

6、 14.#import“FriendProfile.h“ 15. 16.implementationFriendProfile 17.synthesizename; 18.synthesizecountry; 19.synthesizecity; 20.synthesizephoneNbr; 21.end 在这个例子中,接口 FriendProfile:NSObject 表示我们定义了一个叫做 FriendProfile 的接口,它从 NSO bject 基类中继承各种功能。NSObject 是 Objective-C 的根类,大多数 Objective-C 中用到的类都会从中 继承,这和 J

7、ava 中的 Object 类相似。接下来,我们分配多个 NSString 类型变量(等同于 Java 中的 Strin g 类型)用来存储朋友的数据。然后是建立 FriendProfile 类,使用synthesize 关键字自动创建各种 get 和 set 方法。建立一个 FriendProfile 对象可以使用如下的语句: 1.FriendProfile*profile=FriendProfileallocini t; 这里的 alloc 和 init 就像 Java 里的 new 关键字,用来在内存中建立 FriendProfile 对象。接下来,就可 以给对象的各种字段赋值了。 1.

8、profilesetName:“Albert“; 2.profilesetCountry:“USA“; 3.profilesetCity:“Houston“; 4.profilesetPhoneNbr:“123-456-789“; 或者可以更简单一点: 1.profile.name=“Albert“; 2.profile.country=“USA“; 3.profile.city=“Houston; 4.profile.phoneNbr=“123-456-789“; 想要充分了解 Objective-C 的语法和功能可以去苹果的开发者站点,那里的语言参考编写的非常好。 JavaJava 的构造

9、的构造 在 Java 中,如果我们想写一个 FriendProfile 类,所做的和 Objective-C 会非常相像,就像 Listing Two 所示: 1.ListingTwo 2.packagepackagecom.vo; 3. 4.publicpublicclassclassFriendProfile 5.privateprivateStringname; 6.privateprivateStringcountry; 7.privateprivateStringcity; 8.privateprivateStringphoneNbr; 9. 10.publicpublicStrin

10、ggetName() 11.returnreturnname; 12. 13. 14.publicpublicvoidvoidsetName(Stringname) 15.thisthis.name=name; 16. 17. 18.publicpublicStringgetCountry() 19.returnreturncountry; 20. 21. 22.publicpublicvoidvoidsetCountry(Stringcountry) 23.thisthis.country=country; 24. 25. 26.publicpublicStringgetCity() 27.

11、returnreturncity; 28. 29. 30.publicpublicvoidvoidsetCity(Stringcity) 31.thisthis.city=city; 32. 33. 34.publicpublicStringgetPhoneNbr() 35.returnreturnphoneNbr; 36. 37. 38.publicpublicvoidvoidsetPhoneNbr(StringphoneNbr) 39.thisthis.phoneNbr=phoneNbr; 40. 41. Listing Two 中提供了相似的字段,但是那些 get 和 set 必须清楚的

12、写出来。现在我们看看怎样在通讯录 里添加一个新朋友,参加 Listing Three: 1.ListingThree 2.publicpublicclassclassFriendlyServletControllerextendsextendsHttpServle t 3. 4.privateprivatestaticstaticfinalfinallonglongserialVersionUID=1L; 5. 6./* 7.*seeHttpServlet#doGet(HttpServletRequestrequ est, 8.*HttpServletResponseresponse) 9.*

13、/ 10.protectedprotectedvoidvoiddoGet(HttpServletRequestreques t, 11.HttpServlet Responseresponse) 12.throwsthrowsServletException,IOExce ption 13.doPost(request,response); 14. 15. 16./* 17.*seeHttpServlet#doPost(HttpServletRequestreq uest, 18.*HttpServletResponseresponse) 19.*/ 20.protectedprotected

14、voidvoiddoPost(HttpServletRequestreques t, 21.HttpServl etResponseresponse) 22.throwsthrowsServletException,IOExce ption 23.response.setContentType(“text/html“); 24.PrintWriterout=response.getWriter (); 25. 26.finalfinalStringaction= 27.request.getParameter(“request edAction“); 28. 29.ifif(action=nu

15、llnull|action.trim().length() =0) 30.out.println(“invalidactionrequest ed“); 31.returnreturn; 32. 33.elseelse 34.ifif(action.equalsIgnoreCase(“addToContacts“) 35. 36.Stringname=request.getP arameter(“name“); 37.Stringcountry=request.getParame ter(“country“); 38.Stringcity=request.getParameter (“city

16、“); 39.StringphoneNbr=request.getParam eter(“phoneNbr“); 40./normallyyouhavetovalidat ebrowser-originatedrequests 41.booleanbooleanvalidParameters= 42.validateParameters(na me,country,city,phoneNbr); 43.ifif(validParameters=falsefalse) 44.out.println( 45.“pleaseverifyands ubmitcorrectinformation“); 46.returnreturn; 47. 48. 49.FriendProfilenewProfile=newnewFr iendProfile(); 50.newProfile.setName(name); 51.newProfile.se

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号