肚子响是什么原因| 安逸是什么意思| 查乳房挂什么科| 结婚长明灯有什么讲究| 夏天适合喝什么汤| 小马是什么牌子| 提手旁的字与什么有关| 什么去火效果最好| 金蝉脱壳什么意思| 师父的老公叫什么| 迷你什么意思| 尿道尿血是什么原因| 四季豆为什么叫四季豆| 竖心旁的字与什么有关| 肝不好有什么症状表现| 爆菊花什么感觉| 高温丝假发是什么材质| 什么是盆地| 灵魂伴侣什么意思| 熊喜欢吃什么食物| 子宫肌瘤有什么症状| 掉头发什么原因| 正能量是什么意思| 腿肿应该挂什么科| 胎盘位于子宫后壁是什么意思| pco是什么意思| 去取环前需做什么准备| 敏字五行属什么| 生动是什么意思| 啪啪啪什么意思| aep是什么意思| 为什么房间有蟑螂| 阴虚火旺是什么意思| 孕妇梦见掉牙齿是什么意思| 尿频尿急吃什么药最好| 芒果过敏用什么药| 王羲之的儿子叫什么名字| 女生喜欢什么礼物| cm医学上是什么意思| 坚壁清野什么意思| 太平猴魁是什么茶| 黄体是什么| 覆盆子是什么东西| 神秘感是什么意思| 韩墨池是什么电视剧| 最好的红酒是什么牌子| asmr是什么| 三七粉主要治疗什么病| 生活质量是什么意思| 医院三甲是什么意思| 酒石酸是什么| 属鼠的和什么属相最配| 兔子可以吃什么| 眉毛里面有痣代表什么| 杏林春暖的杏林指什么| 卩是什么意思| 防微杜渐是什么意思| 早上醒来嘴苦是什么原因| 香菇配什么菜炒着好吃| 龙眼是什么季节的水果| 馥字五行属什么| 陪产假什么时候开始休| 膻是什么意思| 松果体囊肿是什么病| 闹乌龙是什么意思| 法务是干什么的| 1020是什么星座| 湿热是什么症状| 罗嘉良为什么娶苏岩| 广州为什么叫花城| 无后为大是什么意思| 男生爱出汗是什么原因| 什么样的人容易得心梗| 于无声处是什么意思| 为什么医生很少开阿斯美| 横纹肌溶解什么意思| 手抖挂什么科室| 瞅瞅是什么意思| 不长头发是什么原因| 日语一库一库是什么意思| 肠道紊乱吃什么药| 牙齿变黑是什么原因| pfs是什么意思| 乳房疼吃什么药| 身体机能是什么意思| 脚气真菌最怕什么| 梦见花开是什么预兆| 全自动洗衣机不脱水是什么原因| 胃胀吃什么药效果好| 现在最火的歌是什么| 健脾吃什么食物| 返图是什么意思| 银屑病吃什么药| 维生素d和d3有什么区别| 布鲁斯是什么| 巽什么意思| 哈喇味是什么味道| 肚脐的左边疼是什么原因| 早搏的症状是什么表现| 气短吃什么药| 流是什么意思| 钴蓝色是什么颜色| 什么最解渴| 过午不食是什么意思| 卡介疫苗是预防什么的| 胃痛怎么办吃什么药| 吃坏肚子吃什么药| 月经不调去医院要做什么检查| 梦见放鞭炮是什么意思| 肛门瘙痒用什么药最好| 四是什么生肖| 阴道镜活检是什么意思| 73年属牛的是什么命| 五音指什么| 做肠镜需要准备什么| 严重脱发是什么病先兆| 皮肤瘙痒用什么药膏| 什么食物养胃又治胃病| 2029年属什么生肖| 龙的五行属性是什么| 真正的爱情是什么| 一个三点水一个除念什么| 知了幼虫叫什么| 新农合是什么| 指鼻试验阳性代表什么| lake是什么意思| 月经几个月不来是什么原因| 蓝色加红色等于什么颜色| 白带带血丝是什么原因| 什么是褪黑素| 宁字五行属什么| 30岁以上适合用什么牌子的护肤品| 血管检查是做什么检查| 回头鱼是什么鱼| 梦见蔬菜是什么预兆| 声东击西是什么意思| 6.26什么星座| 风寒感冒流鼻涕吃什么药| 出虚汗吃什么中成药| 千山鸟飞绝的绝是什么意思| 陈醋和蜂蜜一起喝有什么好处| 什么叫免疫力| 意见是什么意思| 梅花在什么季节开放| 摸鱼是什么意思| 血栓吃什么药化得快| v3是什么意思| 肠道肿瘤有什么症状| 毕业证有什么用| 投放是什么意思| 月光石五行属什么| 什么螺不能吃| 荣耀是什么品牌| 张三李四王五赵六后面是什么| 立秋什么意思| 春天是什么样子的| 所谓是什么意思| 灰指甲是什么原因引起的| 谷丙转氨酶高是什么原因| 上海有什么特色美食| 天克地冲是什么意思| 咽炎雾化用什么药最好| 大腿根部疼痛是什么原因| 腿上起水泡是什么原因| 甘油三酯偏高说明什么| 离职是什么意思| 维生素c对身体有什么好处| 贫血吃什么水果| 表水是什么意思| 体检挂什么科| 猫翘尾巴是什么意思| 书犹药也下一句是什么| crp高是什么意思| 贫血吃什么补的快| 科颜氏属于什么档次| 羊奶有什么作用与功效| 猴戏是什么意思| 黄芪主治什么病| 解酒的酶是什么酶| 跑马什么意思| 牙龈发紫是什么原因| 氟化钠是什么| 胎盘成熟度1级是什么意思| 心肌炎吃什么食物最好| 有什么| 心脏反流吃什么药| pg是什么激素| 人心惶惶是什么意思| 五月21号是什么星座| 涤纶是什么面料| 人为什么要吃饭| 球蛋白的功效与作用是什么| 检查肺部应该挂什么科| 1994年是什么命| 棕色眼睛的人什么血统| 胎元是什么意思| 乳头出血是什么原因| 大年初一是什么星座| 福肖指什么生肖| 金火是什么生肖| 你在说什么用英语怎么说| 腿走路没劲发软是什么原因| 肛周湿疹挂什么科| 路痴是什么意思| 声泪俱下是什么意思| 喝什么茶减肥效果最好| 水痘擦什么药膏好得快| 香瓜什么时候成熟| 卵巢囊肿吃什么药好得最快| 女生下体瘙痒用什么药| 婴幼儿积食会有什么症状| 呼吸道感染一般用什么消炎药| 正月初十是什么星座| 身先士卒是什么意思| 朱元璋为什么不杀朱棣| hpv16是什么意思| 伊朗是什么民族| 谷丙转氨酶是检查什么的| 虎是什么命| 牛b克拉斯什么意思| 在什么什么后面| 电解质水是什么水| 龙生九子是什么生肖| 脚背疼挂什么科| 无济于事的意思是什么| 搞破鞋是什么意思| 老虎属于什么科| 戍是什么意思| 甲状腺是什么引起的原因| 男生小便尿道刺痛什么原因| 痛风反复发作什么原因| MP是什么| mL代表什么| 室上性心动过速是什么原因引起的| 女人绝经一般在什么年龄段| 烂脚丫用什么药能治除根| 菊花什么时候开花| 无名指为什么叫无名指| 果糖胺偏高说明什么| 一热就咳嗽是什么原因| 负数是什么| ct平扫能检查出什么| 农历十月是什么星座| 梅开二度是什么意思| 吃葵花籽有什么好处和坏处吗| 全身皮肤瘙痒是什么原因| 七月半是什么节日| 加味逍遥丸和逍遥丸有什么区别| 原来是什么意思| 41年属什么生肖| 什么是逆商| 粘胶纤维是什么面料| 封闭抗体是什么意思| 甲状腺是挂什么科| 前列腺增生吃什么药| 一什么新月| 中午12点半是什么时辰| 办独生子女证需要什么材料| 为什么肛门会出血| 牛黄是什么东西| 鼓目念什么| 日本豆腐是什么做的| 行云流水是什么意思| mcm中文叫什么牌子| 百度

巧吃香蕉:这样吃,一周瘦8斤!小伙伴们都惊呆了~

百度 3月24日,2018南京市中考政策解析会在南京市雨花台中学举办,南京市招办主任钱汉平权威解读中招最新变化。

The uniform access principle of computer programming was put forth by Bertrand Meyer (originally in his book Object-Oriented Software Construction). It states "All services offered by a module should be available through a uniform notation, which does not betray whether they are implemented through storage or through computation."[1][2] This principle applies generally to the syntax of object-oriented programming languages. In simpler form, it states that there should be no syntactical difference between working with an attribute, pre-computed property, or method/query of an object.

While most examples focus on the "read" aspect of the principle (i.e., retrieving a value), Meyer shows that the "write" implications (i.e., modifying a value) of the principle are harder to deal with in his monthly column on the Eiffel programming language official website.[3]

Explanation

edit

The problem being addressed by Meyer involves the maintenance of large software projects or software libraries. Sometimes when developing or maintaining software it is necessary, after much code is in place, to change a class or object in a way that transforms what was simply an attribute access into a method call. Programming languages often use different syntax for attribute access and invoking a method, (e.g., object.something versus object.something()). The syntax change would require, in popular programming languages of the day, changing the source code in all the places where the attribute was used. This might require changing source code in many different locations throughout a very large volume of source code. Or worse, if the change is in an object library used by hundreds of customers, each of those customers would have to find and change all the places the attribute was used in their own code and recompile their programs.

Going the reverse way (from method to simple attribute) really was not a problem, as one can always just keep the function and have it simply return the attribute value.

Meyer recognized the need for software developers to write code in such a way as to minimize or eliminate cascading changes in code that result from changes which convert an object attribute to a method call or vice versa. For this he developed the Uniform Access Principle.

Many programming languages do not strictly support the UAP but do support forms of it. Properties, which are provided in a number of programming languages, address the problem Meyer was addressing with his UAP in a different way. Instead of providing a single uniform notation, properties provide a way to invoke a method of an object while using the same notation as is used for attribute access. The separate method invocation syntax is still available.

UAP example

edit

If the language uses the method invocation syntax it may look something like this.

// Assume print displays the variable passed to it, with or without parens
// Set Foo's attribute 'bar' to  value 5.
Foo.bar(5)
print Foo.bar()

When executed, should display :

5

Whether or not Foo.bar(5) invokes a function or simply sets an attribute is hidden from the caller. Likewise whether Foo.bar() simply retrieves the value of the attribute, or invokes a function to compute the value returned, is an implementation detail hidden from the caller.

If the language uses the attribute syntax the syntax may look like this.

Foo.bar = 5
print Foo.bar

Again, whether or not a method is invoked, or the value is simply assigned to an attribute is hidden from the calling method.

Problems

edit

However, UAP itself can lead to problems, if used in places where the differences between access methods are not negligible, such as when the returned value is expensive to compute or will trigger cache operations.[2]

Language examples

edit

Python

edit

Python properties may be used to allow a method to be invoked with the same syntax as accessing an attribute. Whereas Meyer's UAP would have a single notation for both attribute access and method invocation (method invocation syntax), a language with support for properties still supports separate notations for attribute and method access. Properties allow the attribute notation to be used, but to hide the fact that a method is being invoked instead of simply retrieving or setting a value.

As such, Python leaves the option of adherence to UAP up to the individual programmer. The built-in @property function provides a simple way to decorate any given method in attribute access syntax, thus abstracting away the syntactical difference between method invocations and attribute accesses.[4]

In Python, we may have code that access an Egg object that could be defined such that weight and color are simple attributes as in the following

"""
>>> egg = Egg(4.0, "white")
>>> egg.color = "green"
>>> print(egg)
Egg(4.0, green)
"""

class Egg:
    def __init__(self, weight, color) -> None:
        self.weight = weight
        self.color = color

    def __str__(self) -> str:
        return f"{__class__.__name__}({self.weight}, {self.color})"

Or the Egg object could use properties, and invoke getter and setter methods instead

# ...(snip)...
class Egg:
    def __init__(self, weight_oz: float, color_name: float) -> None:
        self.weight = weight_oz
        self.color = color_name
        
    @property
    def color(self) -> str:
        """Color of the egg."""
        return to_color_str(self._color_rgb)

    @color.setter
    def color(self, color_name: str) -> None:
        self._color_rgb = to_rgb(color_name)   

    @property
    def weight(self) -> float:
        """Weight in ounces."""
        return self._weight_gram / 29.3

    @weight.setter
    def weight(self, weight_oz: float) -> None:
        self._weight_gram = 29.3 * weight_oz

    # ...(snip)...
Snipped codes are as following:
import webcolors

# class Egg:

def to_color_str(rgb: webcolors.IntegerRGB) -> str:
    try:
        return webcolors.rgb_to_name(rgb)
    except ValueError:
        return webcolors.rgb_to_hex(rgb)
    
def to_rgb(color_name: str) -> webcolors.IntegerRGB:
    try:
        return webcolors.name_to_rgb(color_name)
    except ValueError:
        return webcolors.hex_to_rgb(color_name)


if __name__ == "__main__":
    import doctest
    doctest.testmod()

Regardless of which way Egg is defined, the calling code can remain the same. The implementation of Egg can switch from one form to the other without affecting code that uses the Egg class. Languages which implement the UAP have this property as well.

Ruby

edit

Consider the following

y = Egg.new("Green")
y.color = "White" 
puts y.color

Now the Egg class could be defined as follows

class Egg
  attr_accessor :color
  def initialize(color)
    @color = color
  end
end

The above initial code segment would work fine with the Egg being defined as such. The Egg class could also be defined as below, where color is instead a method. The calling code would still work, unchanged if Egg were to be defined as follows.

class Egg
  
  def initialize(color)
    @rgb_color = to_rgb(color)
  end

  def color
     to_color_name(@rgb_color)
  end
 
  def color=(color) 
     @rgb_color = to_rgb(color)
  end

  private
  def to_rgb(color_name)
     .....
  end

  def to_color_name(color)
     ....
  end
end

Note how even though color looks like an attribute in one case and a pair of methods in the next, the interface to the class remains the same. The person maintaining the Egg class can switch from one form to the other without fear of breaking any caller's code. Ruby follows the revised UAP, the attr_accessor :color only acts as syntactic sugar for generating accessor/setter methods for color. There is no way in Ruby to retrieve an instance variable from an object without calling a method on it.

Strictly speaking, Ruby does not follow Meyer's original UAP in that the syntax for accessing an attribute is different from the syntax for invoking a method. But here, the access for an attribute will always actually be through a function which is often automatically generated. So in essence, either type of access invokes a function and the language does follow Meyer's revised Uniform Access Principle.

The C# language supports class properties, which provide a means to define get and set operations (getters and setters) for a member variable. The syntax to access or modify the property is the same as accessing any other class member variable, but the actual implementation for doing so can be defined as either a simple read/write access or as functional code.

public class Foo
{
    private string _name;

    // Property
    public int Size
    {
        get;    // Getter
        set;    // Setter
    }

    // Property
    public string Name
    {
        get { return _name; }     // Getter
        set { _name = value; }    // Setter
    }
}

In the example above, class Foo contains two properties, Size and Name. The Size property is an integer that can be read (get) and written (set). Similarly, the Name property is a string that can also be read and modified, but its value is stored in a separate (private) class variable _name.

Omitting the set operation in a property definition makes the property read-only, while omitting the get operation makes it write-only.

Use of the properties employs the UAP, as shown in the code below.

    public Foo CreateFoo(int size, string name)
    {
        var foo = new Foo();
        foo.Size = size; // Property setter
        foo.Name = name; // Property setter
        return foo;
    }

C++ has neither the UAP nor properties, when an object is changed such that an attribute (color) becomes a pair of functions (getA, setA). Any place in that uses an instance of the object and either sets or gets the attribute value (x = obj.color or obj.color = x) must be changed to invoke one of the functions. (x = obj.getColor() or obj.setColor(x)). Using templates and operator overloading, it is possible to fake properties, but this is more complex than in languages which directly support properties. This complicates maintenance of C++ programs. Distributed libraries of C++ objects must be careful about how they provide access to member data.

JavaScript

edit

JavaScript has had support for computed properties via getters and setters since ECMAScript 5 in 2009.[5] However, there are special cases, such as the length property on Array.prototype and String.prototype which pre-date ECMAScript 5.[6]

In ECMAScript 5 JavaScript, Object.defineProperty can be used to define getters and setters.[7] For example:

var obj = {};

Object.defineProperty(obj, "two", {
    "get": function() { return 1 + 1; }
});

Then obj.two can be used to execute the getter function. Since the property two does not define a setter, any writes to the property will result in no operation. For a setter, the set property is used instead:

var obj = {};

Object.defineProperty(obj, "two", {
    "get": function() { return this.x; },
    "set": function() { this.x = 1 + 1; }
});

References

edit
  1. ^ Meyer, Bertrand (1997). Object-Oriented Software Construction (second ed.). Prentice Hall. p. 57. ISBN 978-0-13-629155-8.
  2. ^ a b "The UniformAccessPrinciple". c2 wiki. Retrieved 6 August 2013.
  3. ^ Meyer, Bertrand. "EiffelWorld Column: Business plus pleasure". Retrieved 6 August 2013.
  4. ^ Official Python Docs, built-in functions
  5. ^ "ECMA-262, 5th edition, December 2009" (PDF). Ecma International. December 2009. Retrieved July 15, 2025.
  6. ^ "ECMAScript Language Specification" (PDF). Ecma International. March 24, 2000. Retrieved July 15, 2025.
  7. ^ "Object.defineProperty() - JavaScript". Mozilla. Retrieved July 15, 2025.
尿结石有什么症状 妙手回春是什么意思 蒙蒙的什么 右肾盂分离是什么意思 茯苓生长在什么地方
农历正月初一是什么节日 bae什么意思 双眼屈光不正是什么意思 手掌中间那条线是什么线 谷丙转氨酶偏高说明什么原因
25年是什么婚 小限是什么意思 心率变异性是什么意思 苦瓜为什么是苦的 路由器坏了有什么症状
央企董事长什么级别 大运什么意思 宝刀未老的意思是什么 过指什么生肖 莆田系是什么意思啊
咖啡对心脏有什么影响1949doufunao.com 安琪儿是什么意思hcv8jop9ns1r.cn 左氧氟沙星有什么副作用hcv8jop0ns1r.cn 嘚是什么意思yanzhenzixun.com 新白娘子传奇许仙为什么用女的演hcv9jop6ns2r.cn
什么是海啸hcv9jop5ns0r.cn 指甲长得快说明什么hcv9jop7ns2r.cn 毛的部首是什么gangsutong.com 摩托车代表什么生肖hcv8jop1ns5r.cn 道观是什么意思hcv9jop7ns4r.cn
世界上最难的字是什么hcv7jop5ns6r.cn pcv是什么意思hcv8jop3ns0r.cn 什么是公元前和公元后hcv8jop2ns8r.cn 吃什么降血压效果最好hcv7jop6ns7r.cn 宝刀未老的意思是什么hcv8jop4ns5r.cn
美容行业五行属什么wmyky.com 超声介入是什么意思hcv9jop2ns9r.cn dha是什么意思hcv8jop6ns0r.cn 6个月宝宝可以吃什么水果hcv9jop5ns3r.cn 乙酉日五行属什么hcv8jop5ns0r.cn
百度