Initial: http://www.eoeandroid.com/thread-233275-1-1.html
EOECommunityLarge set of indexes:
Http://www.eoeandroid.com/portal.php? MoD = TOPIC & topicid = 5
Android page flip (source code included)
Http://www.eoeandroid.com/thread-232572-1-1.html
Navigation menu implementation on both sides of Android
Http://www.eoeandroid.com/thread-232567-1-1.html
----------------- Post body --------------------
For an object-oriented language, class is its core. Let's talk about Java classes.
Forget struct, Java only has class
In C #, we often declare a small data structure as a struct (StructIn this way, the generated object will be allocated to the stack (Note), which belongs to the value type and will be copied during transfer.
In Java, all custom data structures are (Class). They are allocated to the stack and belong to the reference type. Only references are passed when being passed.
We can draw a table:
Java enumeration is different from C # enumeration. C # enumeration is a simple encapsulation of integer types (except byte, short, Int, long, and char), while Java enumeration is a complete function class.
Note: If the struct is a member variable of a class, it will still be allocated to the managed stack.
Access Modifier
Haha, I still find that tables are the most intuitive
It can be seen that the access modifier in C # is more complicated to use, but the functions of the two are basically the same.
Note that for members inside the class,When the default access modifier is used, C # considers it private, and Java considers it as accessible in the package.. In this regard, I personally feel that it is more natural to process the default value as private as C.
Different keywords and symbols
Since there are two languages, there are always some symbols and keywords in different usage:
Hidden virtual
JavaCodeSeems to have never seen virtual in?
This is true, becauseJava determines that all methods areVirtualThe method with the same signature as the base class is by defaultOverride.
What should I do if I don't want to create a virtual method? AddFinalKeyword.
Sometimes, when you want to override, you do not have to write the entire signature for the method. For example, if the return type is int, it is clearly written as long. At this time, the compiler will not report an error, but will silently generate a new method.
To prevent this situation, Java
5 added@ OverrideAnnotation, used to indicate the method of rewriting. If this method does not correctly override the methods in the base class, the compiler reports an error.
Public ClassSomecollection {@ overridePublicString tostring (){Return Super. Tostring ();}}
Lost operator overload
Java does not have all the features of C ++... I can't talk about it... It is also advantageous to avoid adding operators to reload this feature:
Advantage: Java syntax is simpler and easier to read.
Disadvantage: the Java language is more expressive and more flexible. For example, C # reloads the = operator of the string to ensure semantic consistency. The list and dictionary in C # can both use [], making the code more intuitive, in Java, only Arrays can be written in this way.
It is a pity that Java has cut off this feature. Such as string determination and Random Access to the list are a little troublesome in Java. But if you try to encapsulate a matrix class in Java and use it to implementAlgorithmI'm sure you will be crazy about the nesting of parentheses.
Since the overload operator is not allowed, why can strings be connected with the + operator?
This is a good question!
The truth is, as long as you connect unaddable objects with +, the compiler will automatically give them to tostring and put them in stringbuilder for connection!
About object
In C #, an object is a base class of all types except pointers. in Java, an object is a base class of all types except basic types.
The two are similar in design. There are several public methods that can be matched one by one. Except for getting the first one, several other methods can be rewritten:
Class initialization Block
Those who have just transferred from C # may be confused by the braces "inexplicable" in the class. "What about this code"
Public ClassSomecollection {Private Int[] Marray; {marray=New Int[10];For(IntI = 0; I <10; I ++) {Marray [I]=I ;}}PublicSomecollection {}}
That is the Java initialization block, which will be executed before the constructor is executed.
If the initialization of a field cannot be done in one sentence on the right side of = and all the constructor methods need to be used, you can consider extracting it into an initialization block.
In addition, there are also corresponding static initialization blocks for static fields. Just add "static" before the braces.