[Continuous update] Notes for converting. net code to java code. netjava
Most software-related majors in most Chinese universities force students to learn c and Java, but several. net languages can be said to be optional.
Because visual studio is relatively easy to use on the windows platform, some students will learn the. net development technology by themselves before attending university Java courses.
This type of student needs some code conversion skills to help learning Java.
Before that, I spoke about the TinyMCE editor. It cannot properly dye some vb.net keywords, such as Async, NameOf, and Aggregate. Some keywords of c # cannot be correctly colored, such as _ arglist.
Since I started my Java class this semester, you are welcome to correct the errors in the code.
1. Enumeration type
VB
Public Enum Direction Right = 1 DownRight Down DownLeft Left UpLeft Up UpRightEnd Enum
C #
public enum Direction { Down = 1, DownLeft, Left, UpLeft, Up, UpRight, Right, DownRight }
The conversion to Java is like this. It seems like c ++/cli and c ++/cx enum class.
If the original enumeration is long, you 'd better write a small program to convert it.
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); }}
The _ value is the reference source code of. net. You can change the name if it is not pleasing to the eye.
2. Exceptions
If Java exceptions are not in the RuntimeException type, they must be declared using Throws. Otherwise, errors such as AccessViolationException can only be handled in UnhandledException (ErrorListener.
Writing a bunch of Throws during code conversion is troublesome. After writing a layer, you forget the exceptions at the layer below.
This is easy to handle. As long as it is not a fatal exception, all custom exceptions inherit RuntimeException. Several exceptions that require Throws are intercepted by ErrorListener.
Class PointOutOfScreenException extends RuntimeException {public PointOutOfScreenException () {super ("Hitting the screen ...");}}
3. identifier type inference
VB
Dim a = New StringBuilder
C #
var a = new StringBuilder();
Convert to Java and copy and paste the class name to the beginning.
StringBuilder a = new StringBuilder();
4. c # insecure mode. The unique operators, dynamic, async, await, linq, and unsigned types of the vb and f # Call runtime libraries .......
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: write it again. Directly converting this code is a waste of time!
I want to attend classes. I will write it here this time. If you like Java, you can find out the unreasonable descriptions, but do not spray.