Object-oriented is a development idea. The most important thing to remember is that everything is an object. In order to make the program better understood and written, the ways and ideas of describing things in real life are integrated into it, which becomes object-oriented thinking. To integrate things in life into a program, description is needed. Description is divided into two aspects: characteristics and behavior. The characteristics and behaviors of different categories of objects have huge differences. In order to better formulate a way to describe each type of things, then The concept of class extracted from the programming world is equivalent to the concept of type in life. Every thing should have a type. Then, if things in life are classified according to different aspects, they can be classified into different categories. All classes in programming are also a thing that everyone says is reasonable. It is quite abstract and has considerable uncertainty and Randomness.
The composition of a class:
A class in Java contains attributes and methods. Properties are variables in a class, which can be divided into static variables, instance variables (global variables), and local variables (existing in methods, and the declaration cycle is limited to the calling phase of the method)
Classes in C# include fields , properties and methods. Fields correspond to attributes in Java. The attribute objects in C# and the get and set accessors in Java are encapsulations of fields. The methods are the same and describe behaviors.
Invocation of class members:
Instance members are called by objects. Static members are called by classes. However, in Java, static members can also be called by instances, which means that every student in the class can control the class fee at will, which is a very bad problem. C# is strictly restricted in this regard. Static members can only be called by classes.
Three major features - Encapsulation
Encapsulation: A means to hide internal implementation details and achieve the effects of data security protection and code reuse.
Encapsulation is everywhere. It seems simple but can be infinitely extended. There is no explicit keyword for encapsulation. Since it is an idea and a method, there is no grammatical difference between Java and C#. It's just that when they use access modifiers to achieve the effect of encapsulation, the access modifiers of the two languages are different.
In Java:
Private: Private, only accessible internally
Dufault: Default, accessible internally in the same package.
Protected: protected, accessible in the same package or subclasses of different packages.
Public: Public, accessible from anywhere.
Features: There is a clear size ownership relationship: private < default < protected < public
In C# (the concept of assembly is introduced. The namespace is similar to the package in Java, but It is a logical grouping, which is different from the physical grouping of packages in Java. An assembly is similar to a project):
private: private, only accessible internally.
Intenal: Internal, accessible within the same assembly, same as default.
Protected: protected, can be accessed in subclasses, which is different from protected in java. The scope here is smaller, and non-subclasses in the same assembly cannot access.
Protected internal: It is the union of internal and protected access scopes.
Public: Public, accessible from anywhere.
Characteristics: There is no clear size ownership relationship, and the access range sizes of internal and protected are unclear.
Three Major Characteristics - Inheritance
Inheritance: The purpose is to let one class have the properties and methods of another class.
In Java: Use extends to indicate the use of inheritance
Requirements for rewriting: a. The method name, return value type, and parameters are the same; b. The access scope of the access modifier must be greater than or equal to the parent class Method access modifier;
Access parent class members: use the super keyword, you can use super (parameter); specify a constructor method of the parent class to be called in the constructor method.
In C#: Use: indicates the use of inheritance
Requirements for rewriting: a. The method name, return value type, parameters, and access modifiers are the same; b. The parent class method is modified by virtual. The subclass method is modified by override
Access to the parent class is successful: use the base keyword, use: base (parameter) after the constructor method; specify to call the parent class constructor method, base cannot be used in a static environment and cannot be called Static member of parent class.
Override: Use the new keyword. Introduce the content of override in C#. For non-virtual methods of the parent class, that is, methods that cannot be overridden, using override can override the methods of the parent class. My view on overwriting is to make up for the problems that may arise from the limitation that methods must be modified by virtual in order to be overridden. But if you can’t use it, don’t use it. Overwriting is of little significance, or I haven’t really realized the actual role and effect of overwriting. In case of use, an expert can comment and clear up any doubts.
The basis for judging whether the rewriting is successful: use the reference of the parent class to point to the object of the subclass. If the method calls the parent class method, the rewriting is unsuccessful. If the subclass method is called, the rewriting is unsuccessful. success.
Three major characteristics-polymorphism
Polymorphism: Multiple existence forms of the same behavior. The manifestations include: overloading and rewriting.
Overloading requirements: a. In the same class; b. The method name is the same; c. The parameters are different (number, type, order of parameters).
When calling, determine which method to call based on the parameters passed in.
Abstract classes and interfaces
Abstract classes: Classes modified with abstract are called abstract classes.
Source: In my opinion, the source of abstract classes is worth pondering carefully to help deepen understanding. There are many such problems in real life, that is, we know that this type of things will do this action (method), but we don’t know how to do it. For example, we all know that animals move, but you don’t know how each animal moves. How to move. When defining this Animal class at this time, you need a move method. There is only a method header (describing what it will do) and no method body (describing how to do it). Then this method is quite special, so we mark it as an abstract method. , use abstract modification.
So there are abstract methods in the Animal class. If you instantiate the Animal class, what kind of problems will occur when you call the move method? Unknown because it doesn't describe how to do it. Therefore, in order to avoid this unknown situation, for example, the Animal class is defined as an abstract class. The notable feature is that it cannot be instantiated. For a class that cannot be instantiated, its non-static members cannot be called. So what is the significance of the existence of such a class?
So summary: The meaning of abstract class existence is to be inherited. Abstract classes exist for abstract methods. They have constructors but cannot be instantiated. Grammatically, Java and C# are the same in this regard, so I won’t go into details.
Interface: A set of rules and specifications formulated so that all implementation classes meet these rules and specifications. In practical applications, it can greatly increase the flexibility of the program. My understanding of interface-oriented programming is not particularly deep, nor can I explain it clearly in just one or two sentences. There are experts who can post their own experience in the back and learn from it.
Difference: In C#, if the implementation class does not implement all the methods in the interface, it can, for example, define itself as an abstract class and re-copy the unimplemented methods and define them as abstract methods.
Summary
I have always been doing Java development, and I only learned C# when it was needed for teaching. There are similarities and I can learn it quickly. We only focus on syntax here. We look down upon the big guys who are working on protocols and working on the bottom layer.
The above is the detailed content of Compare the differences between object-oriented syntax in C# and JAVA. For more information, please follow other related articles on the PHP Chinese website!