Java 无法理解对象状态、行为和身份?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18219339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:25:29  来源:igfitidea点击:

Trouble understanding Object State, Behavior, and Identity?

javaobjectstateidentitybehavior

提问by TheNewGuy

I have been instructed by my professor to introduce myself on a page as if I were an object, and that I must address three things:
1) Object State, 2) Behavior, and 3) Identity.

我的教授指示我在页面上介绍自己,就好像我是一个对象一样,我必须解决三件事:
1) 对象状态,2) 行为,以及 3) 身份。

However, I am still really confused as to how I would go about doing something like this. (I have read about the three attributes I must address, but I don't know how I would apply it to a person).
For example, I was told a dog would have States, such as name, color, and breed; as well as Behaviors, such as walking, barking, or wagging their tail.

但是,我仍然很困惑我将如何做这样的事情。(我已经阅读了我必须解决的三个属性,但我不知道如何将它应用于一个人)。
例如,有人告诉我一只狗会有状态,例如名称、颜色和品种;以及行为,例如走路、吠叫或摇尾巴。

So would I do something similar to:

所以我会做类似的事情:

Student me = new Student();
System.out.println(me.getName()); //a state?
System.out.println(me.getCurrentActivity()); //A behavior? (if it were to return watching TV or something) 
System.out.println(me.get....()); //???

Or am I getting the completely wrong idea here?

或者我在这里得到了完全错误的想法?

采纳答案by MrSimpleMind

Characteristics of objects are:

对象的特征是:

State: what the objects have, Student have a first name, last name, age, etc

Behavior: what the objects do, Student attend a course "Java for beginners"

Identity: what makes them unique, Student have Student-ID-number, or an email which is unique. (this is important when implementing the equals method, to determine if the objects are different or not)

状态对象有什么,学生有名字、姓氏、年龄等

行为对象做什么,学生参加“Java 初学者”课程

身份是什么让他们独一无二,学生有学生 ID 号,或者是独一无二的电子邮件。(这在实现 equals 方法时很重要,以确定对象是否不同)

Student john = new Student("John");
john.setCurrentActivity("Learning Java");
john.setAge(21);
john.setWeight(173);
john.setAddress(...);
john.setHobbies(...);

and you can figure out the getters.

你可以找出吸气剂。

public class Student {
    private String name;
    private int    age;
    //etc

    // construct a new student 
    public Student(String name) {
        this.name   = name;
    }

    public setAge(int age) {
        this.age   = age;
    }

    public int getAge() {
        return age;
    }
}

An illustration of a Car object, which I found that might help you some...

Car 对象的插图,我发现它可能对您有所帮助......

Car state:

汽车状态

  • Speed
  • RPM
  • Gear
  • Direction
  • Fuel level
  • Engine temperature
  • 速度
  • 转速
  • 齿轮
  • 方向
  • 油位
  • 发动机温度

Behaviors:

行为

  • Change Gear
  • Go faster/slower
  • Go in reverse
  • Stop
  • Shut-off
  • 换档
  • 走得更快/更慢
  • 倒着走
  • 停止
  • 关机

Identity:

身份

  • VIN
  • License Plate
  • 车牌号
  • 牌照

回答by JNL

All objects have three essential features:

所有对象都具有三个基本特征:

  • state
  • behavior
  • identity
  • 状态
  • 行为
  • 身份

An object's state is defined by the attributes of the objectand by the values these have. In your case, name, age, gender

一个对象的状态是由对象的属性定义和由这些值。有 在你的情况下,name, age, gender

The term "behavior"refers to how objects interact with each other, and it is defined by the operations an object can perform. In your case, student object, can do some activity?

该术语"behavior"指的是如何objects interact with each other,它由对象可以执行的操作定义。在你的情况下,学生对象,可以做一些活动吗?

student.getResults();
  • Identity:
  • 身份:

Even objects with the same properties and behavior have their own individual identity.

Even objects with the same properties and behavior have their own individual identity.

For Example,

例如,

2 blue station wagons that were built in the same year by the same manufacturer are still separate and unique cars.

The identity of an object is independent of its attributes or operations. So an object will retain its identity no matter what values its properties have.

2辆由同一厂家同年生产的蓝色旅行车仍然是独立且独特的汽车。

对象的身份与其属性或操作无关。因此,无论其属性具有什么值,对象都将保留其身份。

回答by Millard

As a correction to @MrSimpleMind's answer marked as best answer, and elaborating on @JNL's answer:

Identityis not what makes the object unique in terms of it's state (e.g. name = "Tim" or whatever). Identity however is that an object is unique in terms of it's location in memory.
If you what to read more about this, you can start by looking at this Wiki page: Identity in OOP

作为对标记为最佳答案的@MrSimpleMind 答案的更正,并详细说明@JNL 的答案:

身份并不是使对象在其状态方面具有唯一性的原因(例如 name = "Tim" 或其他)。然而,身份是一个对象在内存中的位置是唯一的。
如果您想了解更多关于此的内容,您可以从查看此Wiki 页面开始:OOP 中的身份

UPDATE
It's worth mentioning that it's not always in terms of location in memory. When saving an object to a database, it'll essentially be saved as a row, here an ID column is used.

更新
值得一提的是,它并不总是在内存中的位置方面。将对象保存到数据库时,它本质上将保存为一行,此处使用 ID 列。

回答by Prateek Mahajan

State:- state is values of objects variables

状态:- 状态是对象变量的值

Behavior:- are methods of object

行为:- 是对象的方法

Identity:- It is name of object.

身份:- 它是对象的名称。

e.g:-

例如:-

class car{

类车{

int speed = 100;

整数速度 = 100;

void accelerate(){ }

无效加速(){}

}

}

class_car obj_car;

class_car obj_car;

here,

这里,

State:- speed

状态:-速度

Behavior:- accelerate

行为:-加速

Identity:- obj_car

身份:- obj_car

回答by Madhava Rao

In the programming implementation of an object,

在对象的编程实现中,

its state is defined by its instance variables. Instance variables are private to the object. Unless explicitly made public or made available to other “friendly” classes, an object's instance variables are inaccessible from outside the object.

它的状态由它的实例变量定义。实例变量是对象私有的。除非明确公开或提供给其他“友好”类,否则无法从对象外部访问对象的实例变量。

An object's behavior is defined by its methods. Methods manipulate the instance variables to create new state; an object's methods can also create new objects. Java white paper page:34

对象的行为由其方法定义。方法操作实例变量以创建新状态;对象的方法也可以创建新对象。 Java 白皮书页数:34