第203章 Object Class|对象类
203.1 从 Element 到 Object
第202章已经建立:
Real-Time Data
实时数据
↓
Element
元素
↓
Element Object
元素对象
但是,现实世界中的一个认知对象,并不是一个单独的数据元素。
例如:
Egg
鸡蛋
系统可能同时获得:
Position
位置
Orientation
姿态
Mass
质量
Fragility
脆弱性
Pressure
压力
Force
力度
这些都是不同的 Element。
因此需要一个更高层的软件对象,把多个 Element 组织起来:
Element
元素
↓
Object
对象
↓
Object Instance
对象实例
本章正式建立 Object。
203.2 Object Class|对象类
在面向对象程序设计中,Class(类)描述一类对象的共同结构。
因此 ICAI 首先建立一个通用的 Object Class:
class Object
{
protected $id;
protected $type;
protected $name;
protected $elements = array();
}
这里并没有定义:
Egg
Glass
Cup
Fruit
等具体对象。
而是定义:
ICAI 中一个认知对象实例应该具备什么基本结构。
因此:
Object Class
↓
定义对象结构
而不是:
Object Class
↓
定义某一个具体场景
203.3 Object Identity|对象身份
每一个 Object Instance 都必须能够被系统区分。
因此需要:
protected $id;
例如:
Object ID = 1001
这个 ID 表示的是:
当前认知系统中的一个具体对象实例。
例如:
1001 → Egg
1002 → Robot Hand
1003 → Table
因此:
Object
↓
ID
↓
Unique Instance
对象身份与对象名称并不是同一个概念。
name = Egg
表示对象名称。
而:
id = 1001
表示系统中的具体对象实例。
203.4 Object Type|对象类型
除了 ID,还需要 Object Type(对象类型)。
protected $type;
例如:
physical_object
device
environment
一个对象可以:
$type = 'physical_object';
因此:
ID
+
Type
+
Name
构成对象最基础的身份信息。
203.5 Object Name|对象名称
对象还可以具有名称:
protected $name;
例如:
Egg
Robot Hand
Table
名称主要用于识别和显示。
因此:
Object Identity
对象身份
│
├── ID
├── Type
└── Name
203.6 Object Class Constructor|对象构造方法
为了能够真正创建 Object Instance,需要 Constructor(构造方法)。
class Object
{
protected $id;
protected $type;
protected $name;
protected $elements = array();
public function __construct(
$id,
$type,
$name
) {
$this->id = $id;
$this->type = $type;
$this->name = $name;
}
}
这样,Class 就具备了创建对象实例的能力。
203.7 Object Instance|对象实例
Class 只是对象的定义。
真正进入 Runtime(运行时)的,是 Object Instance(对象实例)。
例如:
$egg = new Object(
1001,
'physical_object',
'Egg'
);
此时:
Object Class
对象类
↓
new Object()
↓
$egg
↓
Object Instance
对象实例
$egg 就是一个具体的 Object Instance。
203.8 创建多个对象实例
同一个 Object Class 可以创建多个不同对象。
例如:
$egg = new Object(
1001,
'physical_object',
'Egg'
);
$glass = new Object(
1002,
'physical_object',
'Glass'
);
$fruit = new Object(
1003,
'physical_object',
'Fruit'
);
程序结构仍然只有:
Object Class
但是运行时可以产生:
Object Instance
│
├── $egg
├── $glass
└── $fruit
这正是面向对象工程的重要意义。
不是为每一个现实对象重新编写一套程序。
而是:
一个 Class
↓
多个 Object Instance
203.9 Object Instance 的独立性
虽然:
$egg
$glass
$fruit
都是由同一个 Class 创建,但是它们是三个独立的对象实例。
例如:
$egg = new Object(
1001,
'physical_object',
'Egg'
);
$glass = new Object(
1002,
'physical_object',
'Glass'
);
修改 $egg 的数据,不应该自动修改 $glass。
因此:
Object Class
│
├── Object Instance A
│
└── Object Instance B
Class 提供共同结构。
Instance 保存自己的具体数据。
203.10 Object 与 Element
现在把第202章建立的 Element 接入 Object。
Object 中已经准备:
protected $elements = array();
增加:
public function addElement(Element $element)
{
$this->elements[] = $element;
}
于是:
$egg->addElement($position);
$egg->addElement($pressure);
$egg->addElement($force);
形成:
Egg Object Instance
│
├── Position Element
├── Pressure Element
└── Force Element
这里的关系非常重要:
Element
↓
被 Object Instance 持有
Object 不再只是:
ID + Type + Name
而开始拥有属于自己的 Element 集合。
203.11 Object Instance 的基本结构
因此,一个运行中的 ICAI Object Instance 可以表示为:
Object Instance
对象实例
│
├── ID
├── Type
├── Name
└── Elements
│
├── Element
├── Element
└── Element
例如:
Egg Object
鸡蛋对象
│
├── ID = 1001
├── Type = physical_object
├── Name = Egg
│
└── Elements
├── Position
├── Pressure
├── Force
└── Fragility
这样,一个现实对象已经开始具有软件中的完整对象身份。
203.12 Object Class 的访问方法
为了让系统读取对象基本信息,可以增加:
public function getId()
{
return $this->id;
}
public function getType()
{
return $this->type;
}
public function getName()
{
return $this->name;
}
public function getElements()
{
return $this->elements;
}
于是:
$egg->getId();
$egg->getType();
$egg->getName();
$egg->getElements();
可以访问对象。
203.13 完整的基础 Object Class
到本章结束,可以形成第一版:
<?php
class Object
{
protected $id;
protected $type;
protected $name;
protected $elements = array();
public function __construct(
$id,
$type,
$name
) {
$this->id = $id;
$this->type = $type;
$this->name = $name;
}
public function getId()
{
return $this->id;
}
public function getType()
{
return $this->type;
}
public function getName()
{
return $this->name;
}
public function addElement(Element $element)
{
$this->elements[] = $element;
}
public function getElements()
{
return $this->elements;
}
}
这里暂时不加入复杂的认知计算。
因为本章的任务只有一个:
建立能够承载 ICAI Element 的 Object Class,并能够通过 Class 创建独立的 Object Instance。
203.14 从 Class 到 Runtime
到这里,ICAI 已经完成:
Class
类
↓
Constructor
构造
↓
new
实例化
↓
Object Instance
对象实例
↓
Element
元素
即:
Object Class
↓
Object Instance
↓
Element Collection
例如:
Object Class
│
┌─────────┼─────────┐
↓ ↓ ↓
Egg Glass Fruit
Instance Instance Instance
│ │ │
Elements Elements Elements
同一个 Object Class 可以处理不同现实对象。
这正是 ICAI 后续实现跨对象认知和动态行为的基础。
203.15 本章工程原则
第203章正式确立一条软件工程原则:
ICAI 不为每一个现实对象建立一套独立程序,而是建立通用 Object Class,再根据实时数据创建不同的 Object Instance。
因此:
不是:
Egg.php
Glass.php
Fruit.php
Ceramic.php
而是:
Object.php
↓
Object Instance
↓
不同 Element
↓
不同现实对象
这与前面确定的模拟人工行为逻辑完全一致。
人并不是为鸡蛋、玻璃、水果分别建立一套完全独立的认知程序。
ICAI 的软件工程也不应该这样做。
203.16 本章完成
第203章完成:
Element
元素
↓
Object Class
对象类
↓
Object Instance
对象实例
↓
Element Collection
元素集合
现在已经可以在 PHP Runtime 中真正创建:
Egg Object
Glass Object
Fruit Object
Hand Object
Table Object
但它们目前只是独立存在的对象实例。
下一章再解决:
Object
↓
Attribute
↓
Dynamic Attribute
也就是 第204章 Attribute Class|属性类。
届时才进一步解决你前面提出的关键问题:
位置、力度、压力、距离、姿态等,到底怎样成为对象内部可以实时变化、参与计算的认知属性。