zoukankan      html  css  js  c++  java
  • Can we say objects have attributes, states and behaviors?

    I was reading through Oracle's introduction to OOP concepts and I came across this description:

    Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Software objects are conceptually similar to real-world objects: they too consist of state and related behavior.

    My problem with that passage is that when describing state its mixes attributes there too. For instance, the name and color of a dog are its attributes, while it being hungry or thursty are its states.

    So in my opinion it's more accurate to break the characteristics of objects into three parts: attributes, states and behaviors.

    Sure, when translating this into a programming language I can see that the three-fold partition becomes a two-fold one, because both attributes and states will be stored into fields/variables, while behaviors will be store into methods/functions.

    But conceptually speaking it makes more sense to have the 3 things separate.

    Here's another example: consider a lamp. Saying that both the lamp size and whether or not it's turned on are states is a stretch in my opinion. The lamp size is an attribute, not a state, while it being turned on or off is a state.

    Or did I miss something?

     
    ------------------------------------------------------------------

    You are right in that objects consist of attributes, states, and behavior, if you define attributes to mean non-changing characteristics of an instance. As a matter of fact, it is important to make this distinction, because there exist objects which contain only attributes, (in your sense,) and no state; they are called immutable and they are very useful in programming.

    This three-part definition is indeed represented in programming languages, for example using the final keyword in Java or the readonly keyword in C# to denote instance data which may not change throughout the lifetime of the instance.

    I have to add, though, that non-changing instance data are usually not called attributes. We tend to speak of them as 'final' or 'readonly' or 'constant data' depending on which language we are using. The proper term for them would be 'invariants', but then this word is not frequently used in this sense; it is more often used for other things.

     
  • 相关阅读:
    Js获取当前日期时间及其它操作
    Java JDBC 基础知识
    java自动创建多级目录
    [Java]读取文件方法大全
    table固定首行(二)
    table固定首行(一)
    DIV滚动条
    查看本机开放的端口号,查看某个端口号是否被占用,查看被占用的端口号被哪个进程所占用,如何结束该进程
    DWZ SSH2 菜单树--使用Struts2 标签(iterator/set/if 组合使用)
    synchronized与lock,哪个效率更高
  • 原文地址:https://www.cnblogs.com/oxspirt/p/9256861.html
Copyright © 2011-2022 走看看