


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
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!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools
