This article has not been written yet and has time to complete it gradually.
Differences and conversions between Java and C # access modifiers:
In C #, we usually use the following access modifiers:
Public access is unrestricted.
Protected access is limited to include classes or types derived from include classes.
Internal access is limited to the current Assembly.
Protected internal access is limited to the current Assembly or a type derived from the include class.
Private access is limited to the inclusion type.
In Java, only the following types of configurations are available:
Public is consistent with C #
Protected is consistent with C #
Private is the same as C #
Internal does not have equivalence in Java (in Java, if no modifier is added for a function or class, it means only access to all classes in the current package, which is similar to internal, but the range is not large. In C #, when no range modifier is needed, the default value is protected and cannot be accessed outside the class)
In addition, the type of C # is basically equivalent to the Java class. The younger brother also provides some conversion tools in the C # version of lgame (these tools are in the namespace Org. loon. framework. xNa. java ).
Differences and conversions of the this keyword in Java and C:
Java serves the same purpose as C #'s this keyword. It references the current instance of the current class, but there are some differences in details.
For example, in Java, we want to use this to call an existing constructor. The style is as follows:
Public test {
This (0, 0 );
}
Public test (int x, int y ){
}
In C #, the following usage is required:
Public test: This (0, 0 ){
}
Public test (int x, int y ){
}
Access the base class members from the derived class, that is, the method for calling the parent class, are as follows.
Java
Public test (){
Super (0, 0 );
}
C # Medium
Public test (): Base (0, 0 ){
}
Differences and conversions between Java and C # Arrays:
The differences between Java and C # arrays are mainly reflected in the definition of multi-dimensional arrays (the one-dimensional array has no difference ).
For example, if we define a 3x3 two-dimensional array in Java, we need to set it as follows.
Int [] [] test = new int [3] [3];
The read method is as follows:
Int v = test [0] [0]
C # must be written in the same settings.
Int [,] test = new int [3, 3];
The reading method must conform to the format and become like this (with a note, the younger brother provides the method of copying the Java array in the C # version of lgame ):
Int v = test [0, 0];
In addition, the C # array settings are more rigorous, not as casual as Java.
For example, constructing the following style array in Java will not cause any problems:
String test [] = new string [3];
In C #, the style must be as follows:
String [] test = new string [3];
If [] is written in the variable name rather than the variable type, C # does not recognize it and cannot even compile it.
Java and C # function differences and conversions:
If you use a function in Java, you can directly [rewrite] without declaring [not override].
In the Java environment, if we need a public method that cannot be rewritten, We need to declare this function as final (basically equivalent to the sealed modifier of C #, but sealed is only valid for classes ).
Suppose Class A has a public function:
Public void test (){}
Class B does not require any modification. If you re-write the function once, the rewriting will be completed.
Public void test (){}
If we do not want the test function to be reset in Class A, we need to modify the test function, for example, in the following example:
Public final void test (){}
In C # functions, features are the opposite of Java. If you do not declare a function, [rewrite] means that the corresponding function cannot be rewritten].
In C #, if there is also a class A, to complete operations like Java, you must first make the following settings:
Public Virtual void test (){}
Then the Class B needs to be rewritten and modified as follows to complete the rewrite process (in C #, if we need a public method that can be rewritten, it must be declared as override. Although Java has metadata comments for override, it is optional ......).
Public override void test (){}
In C #, if you do not want the test to be reset, you do not need to set it as follows:
Public void test (){}
In addition, the virtual modifier cannot matchStatic, abstract, privateOrOverrideThe modifier is used at the same time (otherwise, the vs compilation fails and cannot be run at all ).
Differences and conversions between Java and C # global constants:
Sometimes we need a value always fixed to a single volume, which is a global constant.
In C #, This continues the practice of the Standard C language family. You can directly use the const keyword (without static description, this keyword is used globally), for example:
Public const int type = 0;
In this way, we can directly use the class name. type to access this constant value in any situation.
It is troublesome to implement the same effect in Java, because although Java has the const keyword, it retains the value (only placeholder, no actual function ), the final and static keywords must be used together to achieve the approximate effect. For example:
Public final static int type = 0;
In this way, we can access this constant value in any situation by using the Java class name. type.
In addition, the following methods can be used in C # To achieve the goal:
Public readonly static int type = 0;
In essence, the above C # statement is closer to Java's constant usage (the const field is the compile-time, and the readonly field can be used for the run-time, it means there is a chance of Dynamic assignment during initialization ).
Differences and transformations of Java and C # inheritance:
In terms of name, C # is the same as Java, and all classes are class (but when the current class is obtained, it cannot be getclass like Java, but GetType is required), the interface is interface, abstract: abstract.
However, the Java inheritance class requires the modifier [extends] and the implementation interface requires the modifier [implements ].
For example:
Public class test extends a implements B {
}
C # only needs one.
Public class test: A, B {
}
During compilation, the system will distinguish that Is Inheritance, that is interface or other type (struct cannot be inherited ).
In addition, if you want to prevent a class from being derived, you can use the final keyword in Java and the sealed keyword in C # To modify the target class.
However, C # also has a feature that is not as good as Java, making the younger brother very awkward, that is, there cannot be constants in the interface (cannot contain fields ), and there is no public keyword before the function), so some java code can only be fine-tuned to C.
Differences and conversions between Java and C # attributes:
Defining and accessing attributes in Java should use the get and set methods in a standard way (of course, no one stops following the Rules), which may not appear in pairs.
For example
Private string name;
Public void setname (string N ){
This. Name = N;
}
Public String getname (){
Return this. Name;
}
In C #, you can directly access the name using the following method.
Public string name
{
Set;
Get;
}
In addition, we can set the name to public directly in Java, so that the functional aspects seem to be no different from the above C # syntax. However, in C #, We can freely limit the set and get attributes of this method to regulate the operation permissions exposed to users, it is easier to generate a massive set and get in batches than in Java.
Note that if we call the getmethods method traversal function in type of C #, we can see that the name will change to the following style.
Set_name
Get_name
In fact, C # automatically adds the set and get attributes for us, which is essentially different from Java in terms of runtime mechanism.
Java and C # differences and conversions in multithreading management:
Use methodimploptions. synchronized in C # For Thread Synchronization
[Methodimpl (methodimploptions. Synchronized)]
Public void test (){
}
Generally equal to Java
Public synchronized void test (){
}
Use the lock keyword in C # As the mutex lock
Object o = new object ();
Public void test (){
Lock (o ){
}
}
Generally equal to Java
Object o = new object ();
Public void test (){
Synchronized (o ){
}
}
Use monitor. Enter in C # as the exclusive lock
List <Object> List = new list <Object> ();
Public void test ()
{
Monitor. Enter (list );
// List the specific operations
//......
Monitor. Exit (list );
}
There is no equivalence in Java, but some Functional Approximation classes are available for allocation under the java. util. Concurrent. Locks package, for example:
Reentrantreadwritelock ilock = new reentrantreadwritelock ();
Lock readlock = ilock. readlock ();
Lock writelock = ilock. writelock ();
List <Object> List = new arraylist <Object> ();
Public void test (){
Try {
// Read lock
Readlock. Lock ();
// Write to unlock
Writelock. Unlock ();
// List the specific operations
//......
} Finally {
// Read and unlock
Readlock. Unlock ();
// Write lock
Writelock. Lock ();
}
}
For the relationship between other common C # multithreading functions and Java functions, see the following implementation (you can also refer to the C # javaruntime class of lgame ):
Public static void wait (Object O)
{
Monitor. Wait (O );
}
Public static void wait (Object o, long milis)
{
Monitor. Wait (O, (INT) milis );
}
Public static void wait (Object o, timespan T)
{
Monitor. Wait (O, t );
}
Public static void policyall (Object O)
{
Monitor. pulseall (O );
}
Public static void notify (Object O)
{
Monitor. Pulse (O );
}