Is it Attribute or Property?

Source: Internet
Author: User

Is it Attribute or Property? Sometimes it is troublesome.

 

Attribute and Property can be translated into "Attribute". In some cases, Attribute is used to represent "Attribute", and in some cases Property is used. Beginners often "lose" between these two words ", I even think there is no difference between the two, but they are the same. However, Attribute is not equal to Property. What is the difference between the two?

 

Let's start with OOA/OOD (Object Oriented Analysis/Object Oriented Design, Object-Oriented Analysis and Design. In OOA/OOD, Attribute is used to indicate the Feature of an Object ). In some programming languages (such as C # And Delhpi), why is the word "Property" rather than "Attribute?

 

To understand this, we need to drag C ++ in. Attribute in OOA/OOD is called Member Variable in C ++. Member Variable in C ++ can be extended in C #, but it has a new name: Field (Field ). Let's look at a simple C # example:

Public class Student {// Student class

Private string name; // Field (Field) in C #, Member Variable in C ++, and Attribute in OOA/OOD)

}

 

We sorted out the following table and we can see the same concept in OOA/OOD, C ++, and C:

Description

OOA/OOD

C ++

. NET (C #)/ Delphi

Feature

Attribute

Member Variable

Field

 

The Method in OOA/OOD refers to the object Operation ). In C ++, it is called the Member Function (Member Function), while in C #, it is also called the Method. Add the name of the Method to the table:

Description

OOA/OOD

C ++

. NET (C #)/ Delphi

Feature

Attribute

Member Variable

Field

Operation

Method

Member Function

Method

 

We can also see that in C ++, Attribute is called Member Variable, and Method is called Member Function, which is actually quite appropriate. Essentially, it is indeed declaring variables and defining functions. Later users (such as C #) may think that such a title is not OO (Object Oriented, object-oriented), so some changes were made. Method does not need to be called by OOA/OOD, but Attribute is called Field. Because DB (Database, Database) has the Field concept and is also translated as "Field", it is easy to confuse.

 

Now we can distinguish the Attribute mentioned in OOA/OOD from the Property mentioned in C. Continue with the above example:

Public class Student {// Student class

Private string name; // Field (Field) in C #, Member Variable in C ++, and Attribute in OOA/OOD)

Public string Name {// C # is called Property)

Get {

Return name;

}

Set {

Name = value;

}

}

}

 

By the way, let's take a look at private and public: Property is essentially a pair of get/set methods that can be used for access control and thus can be set to public. In accordance with OOA/OOD principles, Attribute cannot be set to public, set it to private.

 

C # does have an Attribute. it seems better to translate it into "Features. For example:

[WebMethod]

In many. NET/C # The book shows that the Attribute is translated into "Attribute". In this way, the old bird can judge through context that it is not talking about Property. I am afraid that the beginner will suddenly fall into the fog. Although it won't be "A blood case caused by a steamed bread", "The difference caused by two attributes" is not a good thing after all.

 

In addition, Attribute and Property are also common in Web programming. For example, you can use VS 2003 or 2005 to create an ASP. NET application. If you add a control in the Code view:

<Asp: TextBox id = "txtName" text = "Hello" runat = "server"/>

In this case, the id, text, and runat in the TextBox Control are called Attribute, which is extended in HTML.

 

If you drag and drop controls in the design view, and then set the ID or Text in the Properties window, the ID or Text will be called the Property. Because the Control is implemented as a Control Class on the server, the Control can be accessed programmatically. The attribute must be the Property in the Class.

 

When we are learning new knowledge, we need to know what it is, so that we can have a deep understanding of the concept. At the same time, we must be accurate when using terms on weekdays. Otherwise, we will encounter unnecessary obstacles during communication and increase communication costs in vain.

The difference between attribute and property is truly confusing to me. google's views on others are still confused;
From the meaning of attribute and property, attribute should be a "feature", while "feature" in Chinese is a property inherent in nature, so some people think of it as "meta property ", the meaning of "tag" (different from property) is also appropriate. property can be understood as "property"-additional properties;
I don't know what this means, right?
Attribute: modifies attributes)
Property: it is a different property (Nature). When you increase the property, you should think of it, which can be dynamically specified in the program.

You can understand what it means. Foreigners don't understand Chinese. You just need to know what they are.
Property is a language feature introduced in c #. It raises some programming skills in c ++ to the position of syntax.
This feature declares class data members as private and provides public methods to access them.
For example
Public class
{
Private int data1;
Public int Data1
{
Get {return data1 ;}
Set {data1 = value ;}
}
}
Data1 is a property.

//////////////////////////////////////// ////////////////////////
Attribute is an updated language feature that does not appear in c ++.
It can be used to describe features of classes, fields, and methods, such as whether a class can be serialized or not, what name should a field be called when serialized into XML, and whether a method is made public by webservice network methods.
For example
[Serilizeable]
Public class
{
}

"[]" Is an attribute.

[] Attribute Signature
Property is in the class. The two are not comparable.

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.