C # advanced (1) Object-oriented

Source: Internet
Author: User

 

For object-oriented, there areNMany masters wroteNMulti-text. I dare not speak falsely. Let's simply talk about your own understanding.

Let's take a look at "object". What is an object? Everything we can see in real life is called an object. For example, enterprises, hospitals, pets, plants, and people... And so on. In object-oriented programming, objects are often treated as a class and classes have attributes and behaviors. Take a hospital as an example. There are doctors and nurses in the hospital. CT , B Super. In object-oriented programming, we define Hospital Class, which has attributes (doctors, nurses) and behaviors (do CT, Do B ). This is a simple object-oriented programming.

In fact, we can not only regard hospitals as a class, but also doctors as a class. Doctors have attributes (age, name, gender, etc.) and behavior (check patients, prescribed, operated, etc ). Even we can define a pediatrician as a class. Likewise, orthopedic, surgical, internal medicine, and other doctors can all be defined as a class, but these classes all have the same attributes (age, name, gender, etc.) and even behavior. Do we need to define these attributes in sequence for each class? The answer is no.

We can define a doctor class and define these common attributes and actions. Pediatrician, orthopedic, etc. all inherit from this class of doctors, so that they have all attributes and actions of the class of doctors. This is called inheritance in object-oriented programming.

Let's look at an example.

people, white people, black people, yellow Race. People of age and gender can eat. Dogs have white-haired dogs, black dogs, and so on. Dogs have ages and gender. They eat and bite people.

in this case, we can define a person as a base class (with attributes age, gender, behavior, etc ), black, white, and yellow people all inherit from the class "man. We then define "dog" as a class (with attributes age, gender, behavior, eating, biting, etc.), white dogs, black dogs all inherit from this" dog "class.

here, we can see that, some attributes of a dog already exist in the "person" class. We do not need to define them again in the "dog" class, in the "dog" class, you only need to define its unique behavior or attributes, such as biting. Black and white dogs inherit from "human" and "dog. In this way, there will be no repeated definitions.

but in C # and JAVA multi-inheritance is not supported. What should we do? At this time, an interface concept was created. A class can implement multiple interfaces. In the preceding example, you can define an "animal" interface (a person is also an animal), and define attributes and behaviors that people and dogs share, such as age, gender, and meals. Define a base class like "person" and implement this interface. Then, the white and black classes can inherit the base class "person. The same applies to dogs.

Haha. Let's just give a brief introduction. You may not be clear about this.GoogleThere are a lot of object-oriented text.

 

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.