自用。。从C#到java的程序移植经验

本文对比了C#与Java在二维数组定义与操作、列表初始化、接口实现等方面的语法差异,并探讨了两者在过滤条件应用、排序字典及树映射使用上的不同。

二维数组定义 C#:[,] JAVA [][]

二维数组取长度 C# 一维.Getlength(0),二维.Getlength(1)

c#中可以使用“过滤条件”?名称好像不对,例如calcAsyncList.ForEach(obj => obj.Stop())

“obj => obj.Stop()”为过滤条件,ForEach方法遍历该list,对每一个满足条件的list成员(即"obj",

(可随意写为其他名字,如nima=> nima.Stop()也可))调用stop函数。

java中貌似只能自己 动手写遍历。


C#中可以通过以下方法初始化list

List<MyPoint> list =new List<MyPoint>  {new MyPoint(1,0),new MyPoint(2,0),new MyPoint(3,0)};

Java貌似可以使用“双大括号法”

List<MyPoint> list =new List<MyPoint>(){{add(new MyPoint(0,0));add(new MyPoint(1,0));add(new MyPoint(2,0));}}//未经过编译



c#从: IComparable继承 相当于 JAVA implements Comparable<MyPoint> 

(该方法用于在构造完成这样类的List,HashMap等以后,特化比较的方法,这往往是必须的)



c#的“过滤条件”让移植者吃尽苦头,注意多个条件并列,如:

“条件1”&&“条件2”&&“条件3”&&“条件4”

改为内嵌式判断,则会出错!

改之前注意列举所有可能的条件组合,修改为嵌套以后要补上满足的剩余情况!


c#有“传值调用”,传地址调用“,“传引用”调用

JAVA里对于一般的变量为传值调用,对于类是传引用调用


c#应用程序报错:有出现”正在跳过目标GenerateTargetFrameworkMonikerAttribute因为所有输出文件相对于输入文件而言都是最新的...“,的,其实应该先看log上面第一行的错误提示。


c#用SortedDictionary时候

对应java应使用treeMap,但要注意,键如果是自定义类型,而且其比较函数并不与其他键进行大小比较,则必出错,

因为根据treeMap源码中所述,由于使用compare方法,因此当不断往树枝上查找时候比较不出相同的元素。

(但是c#没事。。也许用了其他的比较函数吧比如equal)


以后继续补充


一个很强大的工具, 能将c#代码片段、文件甚至工程直接换成java代码,并能彻底解决外部引用的DLL问题,最强的是支持c#工程的直接换,生成的Java代码质量也很不错。软件已破解,去除了未注册版最多只能换1000行的限制,亲测可用!压缩包内含帮助文档,不过由于软件的使用很简单,帮助文档基本可以忽略。(小提示:如无法运行,请确认是否安装了.NET framework) 下面是一些英文介绍: C# to Java Converter features include: 1. Folder conversion: Entire folders of C# code files are converted to Java code files.(文件夹换) 2. Code snippet and file conversion: Our snippet conversion accuracy is outstanding and does not require you to insert entire methods or classes. Heuristics are used to convert code fragments wit h missing declarations. (代码片段和文件换) 3. Full support for the latest .NET code syntax: Generics and other newer C# features are converted where possible to the Java equivalent.(全面支持最新版的.NET语法) 4. Conversion options: You can specify numerous conversion options via our Options dialog. Code formatting options, custom type & member replacements, custom string replacements, and miscellaneous options. (可以指定换规则) 5. File comparison feature: After converting a project or folder, you can compare original and converted files side-by-side. Comparing converted code to original code.(原代码与换后代码的比较) 6. Running from the command line: C# to Java Converter can be launched directly for a specific project, folder, or file conversion from the command line. Command line.(命令行执行) 其他一些特点: 1. Converts all versions of C# code (2002, 2003, 2005, 2008, and 2010) (可以换所有版本的C#代码) 2. Evaluates all referenced assemblies and .NET projects in order to resolve external references more completely.(能彻底解决外部引用的dll类库) 3. Converts C# ref parameters using Java generics 4. Superb conversions of all types of arrays 5. Handles the numerous coding alternatives and ambiguities of C# code 6. Flawless conversion of all aspects of inheritance and interfaces 7. Allows custom replacement of strings in the final converted code 8. Accurate even with poorly formatted C# code
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值