The designers of Java hate the complexity of C++, so Java is very concise, and GC also makes memory management very convenient. C# is interested in Java's GC and virtual machine technology, and hopes to integrate several major Microsoft languages into .NET. . Therefore, C# is not simple or even complex in terms of language.
The design ideas of the two languages are also different. Java is a compiled and interpreted language, while C# is a compiled and then compiled and run language. Java doesn't have delegates, C# has delegates. Java tends to use Interface to implement delegate functions, while in C#, Abstract Class plays a greater role than Interface.
Java follows camel naming rules, and C# follows Pascal naming rules. But now more and more Java people are starting to use C#, and at the same time they are bringing the camel naming rules to C#, which may make C# code more and more difficult to read. Why didn't C# follow camel in the first place? I don't see anything bad about the camel naming convention.
1. Class name.this and inner class
In Java, we often see usage similar to class name.this. This is the current object instance. Why does the class name appear in front of it? C# programmers will be confused by this.
In Java, inner classes are used in many places, and members of the outer class can even be accessed in the inner class. At this time, when using this in the inner class, the question will arise of who this is, and what it means. Is it the current object instance of the inner class or the current object instance of the outer class?
In Java, by adding the class name of the outer class in front of this, it means that the current object instance of the outer class is used in the inner class.
Let's look at an example below.
//External class definition
public class OuterClass {
//Inner class definition
private class InnerClass
{
// There is no id member defined in the inner class. Here we access the members in the outer class.
public int getId(){ return OuterClass.this.id; }
public void setId(int id) { OuterClass.this.id = id;}
// The name member is defined in the inner class, and members in the inner class are directly accessed. By default, this accesses members in the current class.
private String name;
public String getName() { return this.name;}
// You can add the name of an inner class in front of this
public void setName(String name) { InnerClass.this.name = name;}
// The inner class can also access members with the same name in the outer class, and the name of the outer class needs to be added.
public String getOuterName() { return OuterClass.this.name;}
public void setOuterName(String name) { OuterClass.this.name = name;}
@Override
public String toString()
{
return "Id: " + this.getId() + ", Inner Name: " + this.getName() + ", Outer Name: " + this.getOuterName();
}
}
//Member id and name defined in the external class
private int id;
private String name;
private InnerClass innerInstance;
public OuterClass()
{
this.innerInstance = new InnerClass();
this.innerInstance.setId(20);
this.innerInstance.setName("Tom");
this.innerInstance.setOuterName("Alice");
}
public String toString()
{
return this.innerInstance.toString();
}
}
In C#, classes are divided into nested classes and non-nested classes. The former is a class declared inside other data types. The latter is a class defined directly in a certain namespace. Nested classes are rarely defined in C#.
Non-embedded classes only allow the use of public and internal access controls, while built-in classes allow the use of all five access control characters, private, protected, internal protected, public and internal. Inner classes can also access all methods of the outer class, including instance methods and private methods, but they need to explicitly pass an instance of the outer class.
Inner classes in C# can use types and static methods defined by external classes, but cannot directly use instance methods of external classes. Therefore, the above problem does not exist.
In C#, the outer class acts more like a namespace for the inner class. As long as access control allows, you can use the following method to create an instance of the inner class object.
OuterClass.InnerClass obj = new OuterClass.InnerClass(); This instance has no direct relationship with any instance of the outer class. Similar to static inner classes in Java.
2. Class name.class and type
In Java, we often see the usage of class name.class. This usage is equivalent to typeof (class name) in C#, which is used to obtain the type object instance reference of the type.
In Java, each class has a corresponding Class object. When a class is written and compiled, a Class object is generated in the generated .class file to represent the type information of the class. Three ways to obtain Class instances:
Obtain the Class instance of the object by calling the getClass() method of the object instance.
Use the static method forName() of Class to obtain a Class instance using the name of the class. Class.forName(xxx.xx.xx) returns a class. Its function is to ask the JVM to find and load the specified class, which means that the JVM will execute the static code segment of the class.
Get the Class instance using the class name .calss. For encapsulation classes of basic data types, you can also use .TYPE to get the Class instance of the corresponding basic data type.
The way to get an instance of a type object in C# is simpler and clearer.
Obtained by calling the GetType() method of the data instance. This method is inherited from Object, so any object in C# has the GetType() method, x.GetType(), where x is the variable name.
x in typeof(x) must be a specific class name, type name, etc., and cannot be a variable name.
Through the static method System.Type.GetType() of System.Type.
3. Anonymous class
In Java, anonymous classes are also used more often. For example, in Android, when implementing button monitoring, you often see code like this.
@Override
public void onClick(View arg0) {
Intent intent = new Intent( MainActivity.this, ActivityFrameLayout.class);
setTitle("FrameLayout");
startActivity( intent );
}
};
Here, OnClickListenter is actually an interface. Can the interface be used to create object instances? Of course not.
Therefore, java automatically creates an anonymous class that implements the interface here. What we create is actually an object instance of this anonymous class.
The advantage of this is that we do not have to define a class that is only used once, and then create object instances through this class, which simplifies program development.
For example, we have the following interface.
instance.onClick(); In C#, we will not use this form at all. Through delegation, the same function can be achieved very simply.
Note that there are no delegates in Java.
If we print the type of this instance, you will see the actual type of this anonymous class.
The concept of attributes should be familiar to everyone. Class member functions can freely access any attribute member in this class. However, it is more troublesome to access the properties of another class from one class, so many times we use the Getxxx and Setxxx methods, which looks extremely unnatural. For example, in Java or C++, the code is like this :
However, in C#, such methods are "attributed". The same code, in C#, becomes:
foo.size++;
label.font.bold = true;
As you can see, C# is obviously easier to read and understand. We can also see a similar situation from the subroutine code of this "property method":
Java/C++:
C#:
In order to distinguish this kind of attributed method from the attribute members of the class, attribute members are called "fields" in C#, and "attribute" becomes the special noun for this "attributed method". By the way, in fact, this kind of attribute method is often encountered in VB and DELPHI. In VB, it is also called attribute. In addition, Get and Set must appear in pairs in C#. A property cannot have only Get without Set (in Java and C++, it can only have Get or only Set). The advantage of doing this in C# is that it is easy to maintain. If you want to When modifying a certain attribute, you will pay attention to the Get and Set methods at the same time and modify them at the same time. You will not change this and forget about that.
5. Object indexing mechanism (Indexer)
The object indexing mechanism was introduced in C#. To put it more clearly, an object index is actually an array of objects. Let’s talk about it in connection with the properties in the previous section. Properties need to hide the Get and Set methods, but in the index mechanism, the Get or Set methods of each object are exposed. For example, the following example illustrates this point more clearly. The above introduces the differences between C# and Java
stories [index] = value;
}
}
}
...
}
The above introduces the differences between C# and JAVA. I hope it will be helpful for you to understand C# and JAVA.