C # basicProgramIt's actually easy to get started with learning java. Most of the syntaxes are basically the same and their concepts are similar.
1. Attributes
Java does not have the property concept like C #. Only the set and get methods are available.
Package jmyang. Oop;
Public class animal {
Private string _ name;
Public animal (){
System. Out. println ("the animal constructor is called! ");
}
Public void eat (){
System. Out. println ("the animal eat method is called! ");
}
Public StringGetname(){
Return this. _ name;
}
Public voidSetname(String value ){
This. _ name = value;
}
}
2. inherit extends
Package jmyang. Oop;
Public class dogExtendsAnimal {
Public dog (){
System. Out. println ("The Dog constructor is called! ");
}
Public void eat (){
System. Out. println ("Dog's eat method is called! ");
}
Public void eat (string food ){
The system. Out. println ("Dog's eat (string food) method is called! ");
}
}
3. Interfaces
Package jmyang. Oop;
PublicInterfaceIrun {
// Note: The final keyword can be omitted.
FinalInt speed = 1;
// Note: Abstract keyword, which can be omitted
AbstractVoid run ();
}
The implements keyword must be used when the class is defined for the Implementation interface.
Package jmyang. Oop;
Public class dog extends animalImplementsIrun {
Public dog (){
System. Out. println ("The Dog constructor is called! ");
}
Public void eat (){
System. Out. println ("Dog's eat method is called! ");
}
Public void eat (string food ){
The system. Out. println ("Dog's eat (string food) method is called! ");
}
Public voidRun(){
System. Out. println ("Dog implements the run method of the Irun interface, speed =" + speed );
}
}
4. Java: Final and C #: sealed
If a class does not want to be inherited by other classes, you can useFinalKeyword modifier.SealedThe keywords work the same.
Note: The static keyword in C # can be used to modify the class, indicating that the class cannot be inherited, and the methods/fields in the class must be of the static type, java does not have a corresponding concept (static in Java cannot be used for class definition)
6. Constant notation (Java: Final and C #: const)
To represent constants in Java, addFinalKeyword, indicating that the variable cannot be modified again. This is similar to the const keyword in C #. (Java also has the const keyword, but it is not enabled yet. It is only used as a reserved keyword)
7. Major Differences
In Oop, "parent class" is often used to create "subclass", that is:Fatherclass c = new childclass ();
However, if both fatherclass and childclass have the same method signature (for example, there are methodsample methods), the results of calling this method in Java and C # are completely different. In Java, C. methodsample () callsSubclassIn C #,Parent class.
You can write your own instance for testing.
In addition,Static MethodIn JavaCalled by "instantiated object"But C # does not.
For example, classa has a static method methoda, which can be called in Java as follows:
Classa A = new classa ();
A. methoda ();
Classa. methoda ();
C # can only be usedClassa. methoda ();
8. Java: Package and C #: namespace
In Java, the file organization is carried out by "package", which is close to "namespace" in C. The difference is:The package in Java corresponds to the folder nameAnd the namespace in C # does not have to be forcibly matched.
Example:
If you create a package named AAA in Java, there will inevitably be a folder named AAA, and all the files in the AAA package will be placed under the folder AAA; you can create a sub-package under the AAA package, for example, AAA. in the AAA folder, a bbb sub-folder is created.