C # Object-oriented programming

Source: Internet
Author: User
Tags constructor

Before the advent of object-oriented programming, almost all programs are process-centric, and the program runs from one place to the next to achieve a certain purpose. And the program's Code modification and reuse rate is low. Object-oriented programming regards the whole world as a set of entities, all individuals are objects. An object is a collection of entities with certain properties and behaviors. The table below roughly illustrates the difference between process-oriented programming (OPP) and object-oriented programming (OOP).

Opp

Oop

A Classes and objects

People who have studied C + + or Java before have a better understanding of classes and objects; objects are an integral part of a OPP application that encapsulates part of the application, which can be a process, data, and some more abstract entities. A class consists of members and methods. These include: Properties, fields, and methods in C # 's classes. The following is described in turn.

Properties and fields can access the data contained in the object, which can be used to distinguish between different objects because the properties and fields of an object are unique. Let's look at how classes are defined in C #.

Field:

Field:

Class School//defines a category

{

public string Schoolname;

}

//

Class classname{

<code>

}

Property:

Class School

{

private string Schoolname;

public string Schoolname;

{

get {return this. schoolname;}

set {this. schoolname=value;}

}


The difference between a property and a field: A property is a logical field; A property is an extension of a field from a field; a property does not occupy the actual memory, and the field occupies the memory location and space. Most directly, the attribute is "externally used", and the field is "used internally." Let's learn the next person class below. This article is published in the introductory web of programming: www.bianceng.cn

class person//definition of a type

{

Public String name;//Field

public String sex; Field

public int age; Field

public double weight; Field

Public person ()//constructor, initializing object

{

Name= "LFC";/

Sex= "man";/

age=1;//

weight=10;//initialization of four fields;

}

Public person (String name,string sex,int age,double weight)

{//constructor, initialization of specified object

This.name=name;

This.age=age;

This.weight=weight;

This.sex=sex;

}//about this and then elaborate on it later. It's understood here as "this."

Methods in the public void Eatfood (double quanity)//Class

{

Double temp=this.weight;//details about this later. It's understood here as "this."

this.weight=temp+quanity;

}

Methods in the public void Pushsth (Double liang)//class

{

Double temp=this.weight;

This.weight=temp+liang;

}}

Person Mywife=new to create an object for the person class.

Console.WriteLine (Mywife.weight);

Mywife.eatfood (22);//Call function

Console.WriteLine (Mywife.weight);

Mywife.pushsth (3.2);//Call function

Console.WriteLine (Mywife.weight);

To be continued < Please teach yourselves what you do not know in the person class >

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.