asp.net C # classes and objects detailed

Source: Internet
Author: User
Tags modifier modifiers sleep

A class of objects

1 What is a class?

In our real world, we often classify things that have a series of identical behaviors and attributes, such as human beings, which is what we call class.

For example, all people have facial features, internal organs and so on (among other special reasons), these components we call human attributes.

All people have to eat, sleep and so on (among other things such as weight loss), these actions we call behavior.

2 What is an object?

We have just learned what the behavior and attributes of classes and classes are, so objects are an entity of this class!

A class is a description of a set of behaviors and attributes, and an object is an entity that conforms to those descriptions.

As shown in figure

Class: A class is a series of descriptions and abstractions of the same behavior and attributes.

Object: An object is an entity of a class, a real instance of existence, so that the human object can eat and sleep, and have five senses and viscera.

Members of the second class

Having learned the basic concepts of classes and objects, let's look at a piece of code:

using System;
2
3 namespace Myhelloworld
4 {
5 class Student
6 {
7 public string _name;
8 public int _age;
9}
10
Class Program
12 {
static void Main (string[] args)
14 {
Student std = new student (); Create an object for a student class
Std._name = "John"; Assign a name attribute to a student object
Std._age = 20; Assigning a student's age attribute to an object
18
19//Print student's name and age of STD
Console.WriteLine ("My Name is:" +std._name);
Console.WriteLine ("My Age is:" +std._age);
22}
23}
24}

Demonstrates the syntax of C # to create classes and attributes that represent the temporary understanding of the C # field as object-oriented, and I will correct when it comes to C # properties:

Class student
{
public string _name;
public int _age;
}

When you look at the modifier public in front of the attribute, it is a modifier that the class member can be accessed externally: the publicly representative is common, which means that the property can be accessed outside the object in the future, and how to access it? Look at 15 ~ 17 Lines of code:

Student std = new student (); Create an object for a student class
Std._name = "John"; Assign a name attribute to a student object
Std._age = 20; Assigning a student's age attribute to an object
At this time, when the student object is created, we can access the property and assign a value to the property by: the object. property.

The console class described in the previous article is then invoked to print the values of the two properties of the student object to the console. The operation effect is as follows:

The above is the property of the class and access to the property.

Three kinds of methods

Having just encapsulated the attributes of the student class, we will then look at the behavior of students like students who need to learn. Sublimation just the code as follows:

1 class Student
2 {
3 public string _name;
4 public int _age;
5
6 public void Tolearn ()
7 {
8 Console.WriteLine (this._name+ "to learn!") ");
9}
10}

The 6th to 9th line of code is the standard definition and implementation of a method.

Look at the definition first (define the method to describe what the behavior is):

Access modifier return value type method name (behavior name)

Access modifier: Public represents a common, future object can access the secondary method

return value type: Void means that the method does not have a return value

Method Name: Tolearn as the name implies is to learn a method (behavior)

The next step is to see how the implementation of the method (implementing the release is to describe how this behavior is implemented):

Method of the implementation of the {method}

We achieve this method in the console output a sentence content is his name added up to learn! Say what you are going to do.

How do I access methods?

Student std = new student (); Create an object for a student class
Std._name = "John"; Assign a name attribute to a student object
Std._age = 20; Assigning a student's age attribute to an object

Std.tolearn (); Call to learn methods
Ctrl + F5 run the following results:

Four access modifiers

The next step is an introduction to the access modifier, as you can see in the example above, with a public access modifier, and then see what other access modifiers are available in C #:

Public: Mark class members as common, which means that objects can be accessed externally.

Private: Marks a class member as private and cannot be accessed outside of the object.

Protected: Mark class members as protected, you can inherit from the quilt class, and you cannot access them outside.

Internal: Class members are marked as accessible within an assembly and cannot be accessed outside the assembly.

Protected internal: Class members are marked as accessible in the current assembly and cannot be accessed externally.

The above does not understand the matter here is just an introduction, after the article will be explained in detail.

Five to guide your pseudo code

1 class Human//Human
2 {
3 public string name; The name of the human attribute
4 public int age; Age of human attributes
5
6 public void Eating (food)//human outside accessible and controllable methods of eating
7 {
8 visceral digestion (food);
9}
10
One private void visceral digestion (food)//human internal method behavior outside inaccessible, such as eating we can control, but how digestion is visceral control so external inaccessible
12 {
13//The digestive process of things
14}
15}
16
Class Program
18 {
static void Main (string[] args)
20 {
21 Human man = new Humans (); Human objects
22 people. Name = "John"; Give the name of the human attribute to assign a value
23 people. Age = 20; Assigning a student's age attribute to an object
24
25 people. Eat (Hamburg); Call eating method
26
27//There is no way to call people here. Visceral digestion () method because he's private. Internal execution of the class
28}
29}
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.