國內大多數大學的軟件相關專業大多強制學生學習c和Java,但.net 的幾個語言可以說是選學的。
由於visual studio在windows平台上使用相對方便,一些同學會在上大學的Java課之前自學.net開發技術。
這一類同學就需要一些代碼轉換技巧來幫助學習Java。
在這之前我吐槽一下TinyMCE編輯器,它不能正確染色一些vb.net的關鍵字,比如Async, NameOf, Aggregate 。c#的某些關鍵字也不能正確染色,如__arglist。
由於我這學期才開始上Java課,代碼中出現的錯誤歡迎大家指正。
1. 枚舉類型
VB
Public Enum Direction Right = 1 DownRight Down DownLeft Left UpLeft Up UpRight End Enum
C#
public enum Direction { Down = 1, DownLeft, Left, UpLeft, Up, UpRight, Right, DownRight }
轉換成Java是這樣的。感覺跟c++/cli和c++/cx的enum class差不多。
如果原先的枚舉比較長,最好寫個小程序轉換一下。
public enum Direction { Down(1), DownLeft(2), Left(3), UpLeft(4), Up(5), UpRight(6), Right(7), DownRight(8) private int __value; private Direction(int value) { this.__value = value; } @Override public String toString() { return String.valueOf(this.nCode); } }
那個__value是我抄.net參考源碼的,如果看不順眼可以改名。
2.異常
Java的異常如果不是RuntimeException這一類的,就得用Throws 聲明,否則像AccessViolationException這一類錯誤一樣只能在UnhandledException(ErrorListener)那裡處理。
轉換代碼的時候寫一堆Throws比較麻煩,寫了一層就忘了下面那一層有什麼異常了。
這個比較好辦,只要不是致命的異常,那自定義異常就都繼承RuntimeException好了。自帶的幾個需要用Throws的異常用ErrorListener攔截。
class PointOutOfScreenException extends RuntimeException{ public PointOutOfScreenException(){ super("點出屏幕了..."); } }
3.標識符類型的推斷
VB
Dim a = New StringBuilder
C#
var a = new StringBuilder();
轉換成Java就用復制粘貼大法,把類名復制一下然後粘貼到開頭
StringBuilder a = new StringBuilder();
4. c#不安全模式, vb和f#各種調運行庫的獨有運算符, 動態, async, await, linq, 無符號類型 .......
VB
Async Function LoadImages(device As CanvasDevice) As Task forestTiles = Await SpriteSheet.LoadAsync(device, $"SpriteSheets/ForestTiles{NameOf(ImageID)}.png", New Vector2(64, 64), Vector2.Zero) wizardWalk = Await SpriteSheet.LoadAsync(device, "SpriteSheets/WizardWalkRight.png", New Vector2(128, 192), New Vector2(64, 150)) wizardIdle = Await SpriteSheet.LoadAsync(device, "SpriteSheets/WizardIdleRight.png", New Vector2(128, 192), New Vector2(64, 150)) End Function
C#
async Task LoadImages(CanvasDevice device) { forestTiles = await SpriteSheet.LoadAsync(device, $"SpriteSheets/ForestTiles{nameof(ImageID)}.png", new Vector2(64, 64), Vector2.Zero); wizardWalk = await SpriteSheet.LoadAsync(device, "SpriteSheets/WizardWalkRight.png", new Vector2(128, 192), new Vector2(64, 150)); wizardIdle = await SpriteSheet.LoadAsync(device, "SpriteSheets/WizardIdleRight.png", new Vector2(128, 192), new Vector2(64, 150)); }
VB
Public Function CalculateClipGeometry(resource As ICanvasResourceCreator, SourcePoint As Vector2, Geometies As CanvasGeometry(), ScreenSize As Size) As CanvasGeometry Dim geos = Aggregate geo In Geometies Let Lines = Aggregate tes In geo.Tessellate From ln In {New LineSegment(tes.Vertex1, tes.Vertex2), New LineSegment(tes.Vertex1, tes.Vertex3), New LineSegment(tes.Vertex3, tes.Vertex2)} Select ln Distinct Into ToArray Select Rays = Aggregate tes In geo.Tessellate From light In {New LineSegment(SourcePoint, tes.Vertex1), New LineSegment(SourcePoint, tes.Vertex2), New LineSegment(SourcePoint, tes.Vertex3)} Where Not (Aggregate l In Lines Where light.RayToBoundary(ScreenSize).HasIntersection(l) Into Any) Select light Into ToArray Where Rays.Length >= 2 AndAlso Rays(0).Name Like "Ln*" Let Fir = Rays.First Select Arr = Aggregate ln In Rays Order By ln.Angle(Fir) Into ToArray Select CanvasGeometry.CreatePolygon(resource, {Arr.First.Point2, Arr.First.RayToBoundary(ScreenSize).Point2, Arr.Last.RayToBoundary(ScreenSize).Point2, Arr.Last.Point2}) Into ToArray Return geos.Union End Function
C#
public unsafe void AddThree(__arglist) { var args = new ArgIterator(__arglist); var a = (byte*)TypedReference.ToObject(args.GetNextArg()); *a+=3; }
Java
// TODO: 自己重新寫吧。直接轉換這種代碼反而浪費時間!
我還要上課呢,這次就寫到這裡吧。喜歡Java的可以找出描述不合理的地方,但是請勿噴。