Compare object-oriented syntax differences in C # and Java

Source: Internet
Author: User
Object-oriented is a kind of development idea, the most should remember is the object of everything. In order to make the program better understood and written, the way and idea of describing things in real life is fused into, it becomes the object-oriented thought. The integration of the things in life in the process sequence so need to describe, the description is divided into characteristics and behavior of two aspects, and different categories of object characteristics and behavior has a huge difference, in order to better develop the way to describe each kind of things, then extract the concept of a class in the programming world, equivalent to the concept of the type of life, Each of these things should be of a type. Then the life of the things according to different aspects of classification, can be divided into different categories, all the programming class is also a public saying that the rational, Po said the good thing, quite abstract, with considerable uncertainty and randomness.

The composition of the class:

A class in Java that contains properties and methods. A property is a variable in a class that can be divided into static variables, instance variables (global variables), local variables (which exist in the method, and the declaration period is limited to the invocation phase of the method)

A class in C # that contains fields, properties, and methods. Fields correspond to properties in Java, and the get and set accessors in the Property object Java in C # are the encapsulation of fields, as in the case of methods, that describe the behavior.

Invocation of a class member:

An instance member is called by an object. Static members are called by the class. But in Java, static members can also be called by the instance, the equivalent of every student in the class can arbitrarily dominate the Banfei, a very bad question. C # is strictly limited in this regard, and static members can only have classes to invoke.

Three main Features-package

Encapsulation: In order to hide the internal implementation details, to achieve the protection of data security and the effect of code reuse a means.

Encapsulation is ubiquitous, seemingly simple but infinitely extensible. There is no explicit keyword to represent the encapsulation. Since it is thought and means, there is no grammatical difference between Java and C #. The access modifiers are not the same in both languages when they use the access modifier to achieve the encapsulated effect.

In Java:

Private: Only the internal can be accessed

Dufault: Default, the internal of the same package can be accessed.

Protected: Protected, accessible in the same package or in a subclass of a different package.

Public: accessible from anywhere.

Features: Clear size subordinate relationship: Private < Default < protected < public

In C # (introduces the concept of an assembly.) A namespace is similar to a package in Java, but it is a logical grouping unlike a package in Java that is a physical grouping, an assembly that resembles a project):

Private: Only the internal can be accessed.

Intenal: Internally, the same assembly can be accessed internally, with default.

Protected: Protected, sub-class accessible, and Java in protected is different, where the scope is smaller, the same assembly is not a subclass of access.

Proteted intenal: Is the set of intenal and protected access scopes.

Public: accessible from anywhere.

Features: No definite size subordinate relationship, intenal and protected Access range size is not clear.



Three major features-inheritance
Inheritance: The purpose is to have a class have properties and methods of another class.

In Java: Using extends to represent the use of inheritance

Overriding requirements: A, method name, return value type, parameter is the same, B, access modifier access scope is greater than or equal to the parent class method access modifier;

Access to the parent class member: Use Super keyword, which can be used to specify a constructor method for calling the parent class in the constructor method.

In C #: using: Representing the use of inheritance

Overriding requirements: A, method name, return value type, parameter, access modifier is the same; B, parent method is modified by virtual, subclass method is overridden by override

Access to the parent class succeeds: Use the base keyword after the method is constructed: base (parameter), specify to call the parent class construction method, base cannot be used in a static environment, the parent class static member cannot be called.

Overwrite: Use the new keyword. The overridden content is introduced in C #, and the non-virtual method of the parent class, which is a method that cannot be overridden, overrides the parent class's method. To cover my view is to make up for the need to be modified by the virtual method to rewrite the limitations may bring problems, but can not use it, coverage is not significant, or I have not really realized the actual role of coverage and use of occasions, there is a man can comment on the doubts.

Determines whether the rewrite succeeds by using a reference to the parent class to point to the object of the subclass, if the method calls the parent class method that the override is unsuccessful, and if the call to the subclass method, the rewrite succeeds.

Three major features-polymorphic

Polymorphism: Multiple forms of existence of the same behavior. Expressions are: overloaded, rewritten.

Overload requirements: A, the same class, B, the method name is the same; C, the parameters are different (number of parameters, type, order).

Called to determine which method to call, based on the parameters passed in.

Abstract classes and Interfaces

Abstract class: Classes that use the abstract adornment are called abstract classes.

Source: In my opinion, the source of the abstract class is worth pondering, which is conducive to deeper understanding. There are many such problems in real life, that is, we know that this kind of thing will do this action (method), but do not know how to do it, for example, we all know that animals are moving, but each animal you do not know how to move. When defining this animal class, you need a move method, only the method header (which describes what will be done), no method body (describing how to do this), then this method is more special, we mark it as an abstract method, and use abstract adornments.

So the animal class has an abstract method, if you instantiate the animal class, what happens when you call the Move method? Unknown, because it does not describe how to do it. So in order to avoid this unknown situation, such as the definition of animal class as an abstract class, the salient feature is that it cannot be instantiated. A class that cannot be instantiated, whose non-static members are not callable, what is the meaning of such a class's existence?

So the summary: the meaning of the existence of abstract class lies in being inherited. Abstract classes exist for abstract methods, but they cannot be instantiated by construction methods. The syntax for Java and C # is the same in this respect, no longer elaborate.

Interface: A set of rules and specifications that enable the implementation class to satisfy this rule and specification, and in practice can greatly increase the flexibility of the program. Interface-oriented programming, my understanding is not particularly deep, nor can I speak one or two words clearly. There are tall man can paste their own experience in the back, learning to learn.

Difference: In C #, if the implementation class does not implement all of the methods in the interface, it is like defining itself as an abstract class and re-copying the non-implemented method to define it as an abstract method.

Summarize

has always been to do Java development, teaching the need to learn C #, there are similarities, learn quickly. This is only about the grammar, for the Daniel are engaged in the agreement, the bottom, is not to see this.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.