计算机科学中,特性(英語:attribute)是一种规格,用于定义对象、元素或文件的属性(property),它也常被译作“属性”。也用作指向这样的实例或给实例设置值(即特性可以读写访问)。特性可以更准确地理解为元数据。 特性通常是属性的属性(property of a property)。但在实际使用中,依所讨论的技术领域不同,特性与属性可等价地混为一谈。对象的特性通常是名字与值组成;元素的特性通常是类型或类名;文件的特性通常是文件名与扩展名。

各领域的用法

编辑

如果一个元素被视作另一实体(例如CUSTOMER)的一个属性(property,例如CUSTOMER_NAME),则这个元素自身可以有零或多个特性attributes(或属性properties) ,如CUSTOMER_NAME可有TYPE = "KINDOFTEXT"

C#

编辑

C#语言中,特性是元数据,附加于字段或代码块,如程序集英语Assembly (programming)(assemblies)、成员变量数据类型,等价于Java注解。编译器与反射式编程可访问特性。

开发者可以决定把特性作为元数据,专门用于表示与给定应用程序,类和成员有关的,与实例无关的各类信息。开发者也可以决定把一些特性暴露为属性(properties),用作更大的应用程序框架的一部分。

特性可以实现为类(派生自System.Attribute)。可用作CLR服务,比如COM互操作、remoting序列化,可在运行时查询。

下例在C#中定义特性:

[Obsolete("Use class C1 instead", IsError = true)]  // causes compiler message saying
public class C {...}                                // that C is obsolete

public class ObsoleteAttribute: Attribute {         // class name ends with "Attribute"
  public string Message{ get; }                     // but can be used as "Obsolete"
  public bool IsError{ get; set; }
  public ObsoleteAttribute() {...}
  public ObsoleteAttribute(string msg) {...}
  public ObsoleteAttribute(string msg, bool error) {...}}

[Obsolete]
[Obsolete("This is obsolete")]
[Obsolete("This is obsolete", false)]
[Obsolete("This is obsolete", IsError = false)]

位置参数如上例中的string类型首参,是特性类的构造函数的实参。具名参数,如上例中的Boolean实参,是特性类的属性(property)。[1]

HTML & JavaScript

编辑

当一个checkbox变化时,显示被checked的特性与属性:

<!doctype html>
<html lang="en">
<head>
<body>
<input name="food" type="meal" id="meal">
  <meta charset="utf-8">
  <title>attr demo</title>
  <style>
  p { border;1px solid black;
font-family; arial, sans-serif;
text-align center;
  }
  b {
    color: blue;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<input id="check1" type="checkbox" checked="checked">
<label for="check1">Check me</label>
<p></p>
 
<script>
$( "input" )
  .change(function() {
    var $input = $( this );
    $( "p" ).html( ".attr( 'checked' ): <b>" + $input.attr( "checked" ) + "</b><br>" +
      ".prop( 'checked' ): <b>" + $input.prop( "checked" ) + "</b><br>" +
      ".is( ':checked' ): <b>" + $input.is( ":checked" ) + "</b>" );
  })
  .change();
</script>
</body>
</html>

点击前

编辑
.attr( 'checked' ): checked
.prop( 'checked' ): false
.is( ':checked' ):  false

点击后

编辑
.attr( 'checked' ):   checked
.prop( 'checked' ):    true
.is( ':checked' ):     true

多值数据库

编辑

许多非关系型数据库或多值英语MultiValue数据库系统,表对应于文件,行对应于item,列对应于特性。

XML

编辑

XML中,一个特性是一种标记结构,由名字/值对组成,存在于一个start-tag或empty-element tag中。

参见

编辑

参考文献

编辑
  1. ^ Mössenböck, Hanspeter. Advanced C#: Variable Number of Parameters (PDF). http://ssw.jku.at/Teaching/Lectures/CSharp/Tutorial/: Institut für Systemsoftware, Johannes Kepler Universität Linz, Fachbereich Informatik: 44. 2002-03-25 [2011-08-08]. (原始内容存档 (PDF)于2011-09-20). 

📚 Artikel Terkait di Wikipedia

面向特性编程

apt Spoon XDoclet Attribute-Oriented Programming. An Introduction to Attribute-Oriented Programming. [July 22, 2005]. (原始内容存档于May 26,

Python

允许多态性,不限定于在类层级(英语:Class hierarchy)之内子类型方式,而是采用了鸭子类型方式,就是说针对变量的方法调用和特性(attribute)访问,不事先限制这个的变量的类型,它可被绑定到任何对象。Python的类继承支持多重继承,这可以用来实现混入。Python支持元类,还支持抽象基类,自从Python

面向对象程序设计

物件導向程式設計(英語:Object-oriented programming,缩写:OOP)是種具有物件概念的程式設計典範,同时也是一种程式开发的抽象方针。它可能包含資料、特性、程式碼與方法。对象則指的是類別(class)的实例。它将对象作为程序的基本单元,将程序和数据封装其中,以提高软件的重用

反射式编程

在计算机学中,反射式编程(英語:reflective programming)或反射(英語:reflection),是指计算机程序在运行时(runtime)可以访问、检测和修改它本身状态或行为的一种能力。用比喻来说,反射就是程序在运行的时候能够“观察”并且修改自己的行为。 要注意术语“反射”和“类型内省”(type

HTML

因此,一个HTML元素的一般形式为:<tag attribute1="value1" attribute2="value2">''content''</tag>。一些HTML元素被定义为空元素,其形式为<tag attribute1="value1" attribute

Simula

(PDF)存档于2021-08-10). In general attribute identifiers may be redeclared in subclasses, as is the case of inner blocks. The identity of an attribute is determined by

C♯

包括强类型的代码构建器,以及增量编译的支持等 C# 11 开始支持属性(attribute)为泛型类,即允许声明基类为System.Attribute的泛型类: public class GenericAttribute<T> : Attribute { } C# 11

数据结构对齐

(原始内容 (PDF)存档于2015-03-15).  Attributes - D Programming Language: Align Attribute. [2012-04-13]. (原始内容存档于2012-04-09).  The Rustonomicon - Alternative Representations