Objects defined
So what is an object? An object is a component of a program that knows how to perform certain actions and to interact with other pieces of the program. Functions have previously been described as "black boxes" that take an input and spit out an output. Objects can be thought of as "smart" black boxes. That is, objects can know how to do more than one specific task, and they can store their own set of data. Designing a program with objects allows a programmer to model the program after the real world. A program can be broken down into specific parts, and each of these parts can perform fairly simple tasks. When all of these simple pieces are meshed together into a program, it can produce a very complicated and useful application.
Let's say that we are writing a text-based medieval video game. Our video game will have two types of characters: the players and the monsters. A player has to know the values of certain attributes: health, strength, and agility. A player must also know what type of weapon and what type of armor they possess. A player must be able to move through a maze, attack a monster, and pick up treasure. So, to design this "player object", we must first separate data that the player object must know from actions that the player must know how to execute. The definition for a player object could be:
Player Object:
data:
health
strength
agility
type of weapon
type of armor
actions:
move
attack monster
get treasure
END;
Member Data and Member Functions
Data that an object keeps track of is called member data and actions that an object knows how to do are called member functions. Member data is very similar to variables in a regular function in the sense that no other object can get access to that data (unless given permission by the object). Member data keeps its values over the life of an object.
Objects and Instances
There is a very important distinction between an object and an instance of an object. An object is actually a definition, or a template for instances of that object. An instance of an object is an actual thing that can be manipulated. For instance, we could define a Person object, which may include such member data as hair color, eye color, height, weight, etc. An instance of this object could be "Dave" and Dave has values for hair color, eye color, etc. This allows for multiple instances of an object to be created. Let's go back to the medieval video game example and define the monster object.
Monster Object: data: health skin thickness claws tail spikes actions: move attack player with claws attack player with tail END;
Now, our game could have one instance of a player:
Player Instance #1:
data:
health = 16
strength = 12
agility = 14
type of weapon = "mace"
type of armor = "leather"
END;
and our game could have two instances of monsters:
| a tough one: | and a weak one: |
Monster Instance #1: data: health = 21 skin thickness = 20 claws = "sharp" tail spikes = "razor sharp" END; | Monster Instance#2: data: health = 9 skin thickness = 5 claws = "dull" tail spikes = "quite dull" END; |
Let's say that we had a "Battle" function in our game. The pseudocode for it may go something like the following:
Function Battle(parameters: _player = the Player Object instance
_monster = the Monster Object instance)
turn = PLAYER;
while ((_player's health > 0) AND (_monster's health > 0)) {
if (turn == PLAYER){
player attack's monster;
turn = MONSTER;
}
else {
monster attack's player
turn = PLAYER;
}
}
} // END FUNCTION Battle
In the "attack" phase, the attacking person would somehow deduct points from the defending person. Let's say that the player was fighting with the weaker monster. The weaker monster's health value is 9. If the player attacked the monster and did 5 points of damage to the monster, the monster's new health value would be 4. The monster keeps this value as its health value until it is undated again. So if the monster ran away at this point, and later in the game, the player discovered the weaker monster again, it's health value would still be 4.
博客介绍了面向对象编程中对象的概念,对象是能执行特定动作、与程序其他部分交互的组件,可存储自身数据。还阐述了成员数据和成员函数,以及对象和实例的区别,通过中世纪视频游戏示例进行说明,如玩家和怪物对象及其实例。
1268

被折叠的 条评论
为什么被折叠?



