Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Attribute (programming)

From Wikipedia, the free encyclopedia
(Redirected fromAttribute (computing))
Metadata which defines a property
This article is about a software engineering paradigm. For attributes of computer files, seeFile attribute. For numeric values representing colors in computer graphics, seeColor attribute.
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Attribute" programming – news ·newspapers ·books ·scholar ·JSTOR
(January 2022) (Learn how and when to remove this message)
Inobject-oriented programming,classes can contain attributes andmethods.
An attribute in arelational database can be represented as acolumn or field.

Inobject-oriented programming, anattribute is a specification that defines aproperty of anobject, element, or file. It may also refer to or set the specificvalue for a given instance of such. For clarity, attributes should more correctly be consideredmetadata. An attribute is frequently and generally a property of a property. However, in actual usage, the term attribute can and is often treated as equivalent to aproperty depending on the technology being discussed. An attribute of an object usually consists of a name and a value. For an element these can be a type and class name, while for a file these can be a name and an extension, respectively.

Rules and typing

[edit]
  • Rules: Each named attribute has an associated set of rules calledoperations: For example, one doesn't sum characters or manipulate and process anintegerarray the same way as an image object. Neither does one process text as if it was type of floating point (decimal numbers).
  • Data types: It follows that an object definition can be extended by imposingdata typing which can consist of a representation format, a default value, and legal operations (rules) and restrictions (e.g. "division by zero is not to be tolerated") are all potentially involved in defining an attribute, or conversely one may view them as attributes of that object's type.

Picture file formats (for exampleJPEG,PNG andBMP) are not decoded using the same operations (however similar the images look — these are all graphics data formats). Similarly, a programming language does not use the same operations to evaluate a floating point typed number and typed long integers.

For example, in computer graphics, line objects can have attributes such as thickness (with real values), color (with descriptive values such as brown or green or values defined in a certain color model, such as RGB), dashing attributes, etc. A circle object can be defined in similar attributes plus an origin and radius.In reference to computer systems, attributes are defined particularly for read or write attributes for specific read or write.

Attribute usage

[edit]

If the element in question could be considered a property (CUSTOMER_NAME) of another entity (let's sayCUSTOMER), the element can have zero or more attributes (properties) of its own (CUSTOMER_NAME is ofTYPE = "KINDOFTEXT").

C++

[edit]

C++ has support for both attributes and annotations.

C++11 added attributes, which are indicators to the compiler of some information. However, they are either standard-defined or implementation-defined, and custom attributes cannot be created.

classMyObject{private:[[no_unique_address]]intx;public:[[nodiscard]]boolsatisfiesProperty()constnoexcept{if([[likely]]x>0){returntrue;}returnfalse;}};

C++26 added annotations which can be created, and can be accessed usingreflection, allowing arbitrary metadata to be attached.

importstd;usingstd::string;usingcustom::Debug;usingcustom::EnumFlag;usingcustom::Rename;enumclass[[=EnumFlag]]Toggle:uint8_t{Off,On};struct[[=Debug]]Person{[[=Rename("full name")]]stringfullName;intage;};

C#

[edit]

In theC# programming language, attributes aremetadata attached to a field or a block of code likeassemblies,members andtypes, and are equivalent toannotations in Java. Attributes are accessible to both the compiler and programmatically throughreflection. In contrast, properties, in C# terminology, are members of a class which syntactically are used like instance (or class) variables, but are implemented as a pair of getter/setter functions. (In the absence of a setter, properties are read-only.)

Users of the language see many examples where attributes are used to address cross-cutting concerns and other mechanistic or platform uses. This creates the false impression that this is their sole intended purpose.

Their specific use as metadata is left to the developer and can cover a wide range of types of information about any given application, classes and members that is not instance-specific. The decision to expose any given attribute as a property is also left to the developer as is the decision to use them as part of a larger application framework.

Attributes are implemented as classes that are derived fromSystem.Attribute. They are often used by theCLR services, likeCOM interoperability,remoting,serialisation and can be queried at runtime.

The example shows how attributes are defined in C#:

[Obsolete("Use class C1 instead", IsError = true)]// causes compiler message sayingpublicclassC// that C is obsolete{...}// class name ends with "Attribute"// but can be used as "Obsolete"publicclassObsoleteAttribute:Attribute{publicstringMessage{get;}publicboolIsError{get;set;}publicObsoleteAttribute(){...}publicObsoleteAttribute(stringmsg){...}publicObsoleteAttribute(stringmsg,boolerror){...}}[Obsolete][Obsolete("This is obsolete")][Obsolete("This is obsolete", false)][Obsolete("This is obsolete", IsError = false)]

Positional parameters like first parameter of type string above are parameters of the attribute's constructor. Name parameters like the Boolean parameter in the example are a property of the attribute and should be a constant value.[1]

Attributes should be contrasted against XML documentation that also defines metadata, but is not included in the compiled assembly and therefore cannot be accessed programmatically.

HTML & JavaScript

[edit]

Display the checked attribute and property of a checkbox as it changes.

<!doctype html><html><body><inputid="check1"type="checkbox"checked="checked"><labelfor="check1">Check me</label><p></p><script>document.getElementById('check1').addEventListener('change',function(e){varinput=this;varp=document.querySelector('p');p.innerHTML="input.checked: <b>"+(input.checked?'true':'false')+"</b>"});</script></body></html>

Java

[edit]
Main article:Java annotation

TheJava language uses annotations to carry metadata on symbols or perform code generation, and can be accessed using reflection.

abstractclassAnimal{publicabstractvoidspeak();publicStringgetType(){return"Generic animal";}}classCatextendsAnimal{@Overridepublicvoidspeak(){System.out.println("Meow!");}@OverridepublicStringgetType(){return"Cat";}}

Multi-valued databases

[edit]

On many post-relational ormulti-valued databases systems, relative to SQL, tables are files, rows are items, and columns are attributes. Both in the database and code, attribute is synonymous with property and variable although attributes can be further defined to contain values and subvalues.

The first of these databases was thePick operating system. Two current platforms include Rocket U2's Universe andInterSystems' Caché.

XML

[edit]

InXML, anattribute is a markup construct consisting of a name/value pair that exists within astart-tag orempty-element tag. Markup languages, such asHTML andXML, use attributes to describe data and the formatting of data.

Further information:HTML attribute

A good example is the process of XML assigning values to properties (elements). Note that the element's value is found before the (separate) end tag, not in the element itself. The element itself may have a number of attributes set (NAME = "IAMAPROPERTY").

If the element in question could be considered a property (CUSTOMER_NAME) of another entity (let's sayCUSTOMER), the element can have zero or more attributes (properties) of its own (CUSTOMER_NAME is ofTYPE = "KINDOFTEXT").

See also

[edit]

References

[edit]
  1. ^Mössenböck, Hanspeter (2002-03-25)."Advanced C#: Variable Number of Parameters"(PDF). Institut für Systemsoftware, Johannes Kepler Universität Linz, Fachbereich Informatik. p. 44. Retrieved2011-08-08.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Attribute_(programming)&oldid=1318257831"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp