创建对象的提示 [英] Tips on Creating Objects

查看:66
本文介绍了创建对象的提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读过书籍,并对如何创建对象有所了解。我经常

创建我自己的项目和程序。他们最终变得很漂亮

复杂多久。我经常在我的程序中使用对象,它们是我发现的最强大的编程工具之一。通常我的

程序会增长,所以我的对象也会增长。很多时候我回头看看

我的对象可以分解为几个更小的更可重复的

模块代码片段。从一个大物体回来制作几个较小的物体是个好主意。


这是一个例子:

班级单位{

受保护:


HWND hwnd;

coord loc;

coord currentLoc;

图形* gr;

tbox * combatBox;

std :: vector< color> colors;

std :: map< char,coordkeys; //移动引擎

coord n; //指示

coord s;

coord e;

coord w;

char kind;


浮动攻击;

浮动数据包;

浮动防御;

int move;

int move;

int dmove;

int range;

bool canmove;

bool selected;

bool mark;

bool支出;

int col;


void create();

void displayBox(HDC,coord);

int convert(const int n){return n * 16;}


public:

unit();

单位(HWND,graphics *,int,int,int);

单位(HWND,图形* ,int,int,int,int,int);

单位(HWND,graphics *,DWORD,float,float,int,int,int);

unit( HWND,graphics *,DWORD,float,float,int,int,int,bool);

unit(unit *);

~unit();


DWORD sideColor(){return colors [0] .getCode();}

int intAttack(){return attack;}

int intDefence(){return defense;}

float Attack(){return attack;}

float Defense(){return defense;}

int Moved(){return moved;}


void display(HDC); //显示在crrent loc

void display(HDC,int,int); //在特定位置显示

void display(HDC,int); //显示带偏移量的堆叠

void displayBig(HDC,int,int);

无效更改(HWND);

无效更新( HWND,coord);


coord nextCoord(char);

void mover(coord&,int);

void reset ();

void tomark(); //标记死亡单位;

bool标记(){返回标记;}

coord getCoord(){return loc;}

coord getCurrent(){return currentLoc;}

bool canMove(){return canmove;}

bool isSelect(){return selected;}

bool inRange(coord);

void selectOn(){selected = true;}

void selectOff(){selected = false;}

void isArt(){range = 12;}

void cBoxOn(){combatBox = new tbox;};

void disbersed();

void unDisberse(){disburse = false;}

void write(std :: ofstream&);

void load(std :: ifstream&);

void addk(char k){kind = k;}

};

这是我游戏的一个单位。基本上它是一个单一的游戏。


它完成了我想要的单位所做的一切。它有一个图形颜色它

可以战斗和移动。


它可以是图形的。

它可以是可移动的。

它可以战斗。


我有一个:


我应该创建一个图形来做图形

a移动,可以运动和可战斗

,因为它打架。


后来我可以使用那些更简单的类型的东西在我的游戏中

图形但不动或不打架的单位。我从经验中来到这个

。我不想回去开始编辑一个有效的物品

。对于我的下一个项目,我可以创建这些更简单的对象

并构建更复杂的对象。

解决方案

On 2007年2月5日18:55:15 -0800,JoeC < en ***** @ yahoo.comwrote:


>这是我游戏的一个单位。基本上它是一个单一的游戏。

它做我想让单位做的一切。它具有图形颜色,可以对抗和移动。

它可以是图形化的。
它可以移动。
它可以战斗。

我有一个:

我是否应该创建一个图形来制作图形
一个可移动的动作和可战斗因为它打架。 />
后来我可以在我的游戏中使用那些更简单的类型,这些类型是图形但不移动或不打架的单位。我从经验中来到这个
。我不想回去开始编辑一个有效的对象。对于我的下一个项目,我可以创建这些更简单的对象,并构建更复杂的对象。



你走在正确的轨道上。基本上你会想要定义你的类可以支持的接口




这里有一个提示:接口基本上只有公共纯虚拟函数。

所以可显示界面可能有这样的界面:


类可显示

{

public:

虚拟void display(HDC)= 0; //显示在crrent loc

虚拟空白显示(HDC,int,int)= 0; //在特定位置显示

虚拟空白显示(HDC,int)= 0; //显示带偏移的堆叠

//等

};


然后你的类通过继承它们来实现接口并且

提供纯虚函数的定义。

创建接口可以让你考虑特定的问题域(在

中)这种情况下,渲染图形)。为了获得全部好处,

与您的界面一起使用的算法也必须严格地使用指针或引用

到该接口,无论对象的实际类如何在运行时工作




-dr


JoeC写道:
< blockquote class =post_quotes>
>

我应该创建一个图形来执行图形

a可移动的运动和可战斗的

因为它打架。



要上好课程并不容易:

1.高级别课程必须反映你的工作领域。 - 工作

区域的专家必须能够使用您的程序类 - 创建对象

,其中有一个工作区。然后给他们发一条消息,那里也有b
。例如


Tlandscape& meadow = *新Tlandscape;


meadow.has(新Tgrass +新Tpine(3)+新Tlake );

enum {grass = 0,pines,lake};

meadow [lake] .has(new Tfish(100));

Tperson& worker = * new Tworker;


worker.making(meadow.east,new Tditch);

const uint ditch =湖+ 1;


注意,这个例子并不打算只是创建风景的方式,但

显示,任何与景观合作的人可以说明程序将要执行的部分是什么。


2.基类级别必须可以重复使用。试试设计模式建立

您的课程可重复使用。

-

Maksim A. Polyanin


在童话故事世界中,人们喜欢olso

/ Gnume /


2月6日上午12:25,戴夫Rahardja< a ... @ me.comwrote:


2007年2月5日18:55:15 -0800,JoeC < enki ... @ yahoo.comwrote:


这是我游戏的一个单元。基本上它是一个单一的游戏。


它完成了我希望单位做的一切。它有图形颜色,它可以战斗和移动。


它可能是图形的。

它可能是可移动的。

它可以战斗。


我有一个:


我应该创建一个图形可以做图形

a可移动,可以运动和可战斗

,因为它打架。


后来我可以在我的游戏中使用那些更简单的类型来获得图片但是不要移动或者不打架的单位。我从经验中来到这个

。我不想回去开始编辑一个有效的物品

。对于我的下一个项目,我可以创建这些更简单的对象

并构建更复杂的对象。



你走在正确的轨道上。基本上你会想要定义你的类可以支持的接口




这里有一个提示:接口基本上只有公共纯虚拟函数。

所以可显示界面可能有这样的界面:


类可显示

{

public:

虚拟void display(HDC)= 0; //显示在crrent loc

虚拟空白显示(HDC,int,int)= 0; //在特定位置显示

虚拟空白显示(HDC,int)= 0; //显示带偏移的堆叠

//等等

};


然后你的类实现接口通过继承它们和

提供纯虚函数的定义。


创建接口可以让你考虑特定的问题域(在
中)
这种情况​​下,渲染图形)。为了获得全部好处,

与您的界面一起使用的算法也必须严格地使用指针或引用

到该接口,无论对象的实际类如何在运行时工作




-dr



我知道虚函数以及如何使用它们。在大多数情况下,我会因为重复劳动而选择不使用它们。我的单位

有空中和海上单位的潜在虚拟功能。他们有相同的属性

但可能以不同的方式实现。对于

示例地面单位不能在水中移动,但水单位不能移动

在地面上。


我想要获得更多的思考过程。我通常可以想出语法,但真正的挑战是设计我的程序。我知道你需要经验来设计好的物品,但是你需要写一些物品来获得这种体验。我已经阅读了一些书中给出的建议

,但我只是想知道如何更好地创建对象。我读到你应该设计一个

的对象,目的是不使用你的程序。我担心这是一个非常好的对象并搞砸了它。该对象被设计为

作为一个单元工作,如果它们是分开的,则不同的功能可能不起作用。我的对象很好而且不太大。


I have read books and have ideas on how to create objects. I often
create my own projects and programs. They end up getting pretty
complex and long. I often use objects in my programs they are some of
the most powerful programming tools I have found. Often times as my
program grows so do my objects. Often times I look back and see that
my objects could be broken down int several smaller more re-usable
module pieces of code. Is it a good idea to go back and make several
smaller objects out of a big object.

Here is an example:
class unit{
protected:

HWND hwnd;
coord loc;
coord currentLoc;
graphics * gr;
tbox * combatBox;
std::vector<color>colors;
std::map<char, coordkeys; //movement engine
coord n; //directions
coord s;
coord e;
coord w;
char kind;

float attack;
float dattack;
float defence;
int move;
int moved;
int dmove;
int range;
bool canmove;
bool selected;
bool mark;
bool disburse;
int col;

void create();
void displayBox(HDC, coord);
int convert(const int n){return n * 16;}

public:
unit();
unit(HWND,graphics*,int,int,int);
unit(HWND,graphics*,int,int,int,int,int);
unit(HWND,graphics*,DWORD,float, float, int, int, int);
unit(HWND,graphics*,DWORD,float, float, int, int, int, bool);
unit(unit*);
~unit();

DWORD sideColor(){return colors[0].getCode();}
int intAttack(){return attack;}
int intDefence(){return defence;}
float Attack(){return attack;}
float Defence(){return defence;}
int Moved(){return moved;}

void display(HDC); //display at crrent loc
void display(HDC, int, int); //display at specific loc
void display(HDC, int); //display with offset for stacking
void displayBig(HDC, int, int);
void change(HWND);
void update(HWND, coord);

coord nextCoord(char);
void mover(coord&, int);
void reset();
void tomark(); //marks unit for death;
bool marked(){return mark;}
coord getCoord(){return loc;}
coord getCurrent(){return currentLoc;}
bool canMove(){return canmove;}
bool isSelect(){return selected;}
bool inRange(coord);
void selectOn(){selected = true;}
void selectOff(){selected = false;}
void isArt(){range = 12;}
void cBoxOn(){combatBox = new tbox;};
void disbersed();
void unDisberse(){disburse = false;}
void write(std::ofstream&);
void load(std::ifstream&);
void addk(char k){kind = k;}
};
This is a unit for my game. Basically it is a single game piece.

It does everything I want the unit to do. It has a graphic color it
can fight and move.

It could be graphical.
It could be movable.
It can fight.

I have my has a:

Should I create a graphical that does the graphics
a movable that does the movement and fight-able
because it fights.

Later I could use those simpler types for things in my game that are
graphics but don''t move or units that don''t fight. I come to this
from experience. I don''t want to go back and start editing an object
that works. For my next project I can create these simpler objects
and build more complex ones.

解决方案

On 5 Feb 2007 18:55:15 -0800, "JoeC" <en*****@yahoo.comwrote:

>This is a unit for my game. Basically it is a single game piece.

It does everything I want the unit to do. It has a graphic color it
can fight and move.

It could be graphical.
It could be movable.
It can fight.

I have my has a:

Should I create a graphical that does the graphics
a movable that does the movement and fight-able
because it fights.

Later I could use those simpler types for things in my game that are
graphics but don''t move or units that don''t fight. I come to this
from experience. I don''t want to go back and start editing an object
that works. For my next project I can create these simpler objects
and build more complex ones.

You''re on the right track. Basically you''d want to work on defining interfaces
that your classes can support.

Here''s a hint: interfaces basically have only public pure virtual functions.
So a "displayable" interface may have an interface like this:

class displayable
{
public:
virtual void display(HDC) = 0; //display at crrent loc
virtual void display(HDC, int, int) = 0; //display at specific loc
virtual void display(HDC, int) = 0; //display with offset for stacking
// etc
};

Then your classes implement the interface by inheriting from them and
providing the definitions for the pure virtual functions.

Creating interfaces allows you to think about specific problem domains (in
this case, rendering graphics). To get the full benefit, the algorithms that
work with your interface must also work strictly with pointers or references
to that interface, regardless of the actual classes of the objects it works
with at runtime.

-dr


JoeC wrote:

>
Should I create a graphical that does the graphics
a movable that does the movement and fight-able
because it fights.

To make good classes is not a easy work:
1. The high-level casses must reflect your "job area" - expert of the "job
area" must be able to work with classes of your programs - create objects
that have a sence in "job area" and send them a messages, that have a sence
there too. For example

Tlandscape &meadow= *new Tlandscape;

meadow.has(new Tgrass + new Tpine(3)+ new Tlake );
enum{ grass=0, pines, lake};
meadow[lake].has( new Tfish(100) );

Tperson &worker=*new Tworker;

worker.making(meadow.east, new Tditch);
const uint ditch=lake+1;

Note, the example does not intend to be only way to create landscapes, but
shows, that any man, who works with landscapes can undersand what the part
of program is going to do.

2. The base-level classes must be re-usable. Try "design paterns" to build
your classes as re-usable.
--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/


On Feb 6, 12:25 am, Dave Rahardja <a...@me.comwrote:

On 5 Feb 2007 18:55:15 -0800, "JoeC" <enki...@yahoo.comwrote:

This is a unit for my game. Basically it is a single game piece.

It does everything I want the unit to do. It has a graphic color it
can fight and move.

It could be graphical.
It could be movable.
It can fight.

I have my has a:

Should I create a graphical that does the graphics
a movable that does the movement and fight-able
because it fights.

Later I could use those simpler types for things in my game that are
graphics but don''t move or units that don''t fight. I come to this
from experience. I don''t want to go back and start editing an object
that works. For my next project I can create these simpler objects
and build more complex ones.


You''re on the right track. Basically you''d want to work on defining interfaces
that your classes can support.

Here''s a hint: interfaces basically have only public pure virtual functions.
So a "displayable" interface may have an interface like this:

class displayable
{
public:
virtual void display(HDC) = 0; //display at crrent loc
virtual void display(HDC, int, int) = 0; //display at specific loc
virtual void display(HDC, int) = 0; //display with offset for stacking
// etc

};

Then your classes implement the interface by inheriting from them and
providing the definitions for the pure virtual functions.

Creating interfaces allows you to think about specific problem domains (in
this case, rendering graphics). To get the full benefit, the algorithms that
work with your interface must also work strictly with pointers or references
to that interface, regardless of the actual classes of the objects it works
with at runtime.

-dr

I know about virtual functions and how to use them. In most cases I
choose not to use them because of the duplication of effort. My units
have potential virtual functions for air and sea units. They have the
same properties but may be implemented in a different way. For
example grounds units can''t move in water but water units can''t move
on ground.

I am trying to get more of a thought process. I usually can figure
out syntax but the real challenge is designing my programs. I
understand that you need experience to design good objects but you
have to write objects to get that experience. I have read the advice
given in some books but I am just trying to find out how to go about
creating objects better. I read that you should design an object for
a purpose not to work with your program. My worry is taking a
perfectly good object and messing it up. The object was designed to
work as a unit and the different function may not function if they are
seperated. I my object fine and not too big.


这篇关于创建对象的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆