如何使用面向对象的编程。 [英] How to use objectiented programming.

查看:79
本文介绍了如何使用面向对象的编程。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在写一些大型程序和游戏,而且我已经有了b $ b b人们告诉我,我需要编写更多面向对象的程序。我也理解了这个概念,并写了一些小程序来理解理论。

类单位{

protected:

int locx;

int locy;


int atk;

int dfce;

void create();

public:

unit();

unit(std :: istream);

virtual~unit(){};

虚拟单位* copy()const;

virtual void move();

虚拟空洞攻击();

虚拟空白显示();

};


类hunit {


unit * p;

int * cnt;

public:

hunit( ):cnt(new int(1)),p(new unit){}

hunit(char);

hunit(const hunit& u):cnt(u .cnt),p(up){++ * cnt;}

hunit& operator =(const hunit&);

hunit(std :: istream);

~hunit();

void move();

虚空攻击();

无效显示();

};


这一切作品。我试图表明我理解如何动态

绑定但不太好如何使用它以及它如何使我的程序更好地使用
。我知道形状的例子,但有时候,我找不到

这种编程对我的工作有用。


我更多感兴趣的是我如何从面向对象的角度来看待编程。我还尝试在我的

程序中使用继承来保存重写不同对象的公共部分。什么是

很好的方式来说明小而有用的程序或解决

我可以学习的问题,以帮助我看到更多的编程来自

面向对象的视角。


关于这个程序的最后一个问题是关于朋友的。这是我很少使用的另一个

功能。我经常发现,当我创建一个对象然后

把它放在一个像我上面的句柄中我经常要复制写作

访问器功能:


虚拟空虚move();

虚拟空洞攻击();

虚拟空白显示();


void move();

void attack();

void display();


如果我添加一些东西对于开发中的对象,我必须返回并且

将函数添加到句柄中。我不是在寻找具体的答案

代码,但建议如何成为更好的程序员

并利用C ++提供的强大功能。

I have been writing some large programs and games lately and I have had
people advise me that I need to write more object oriented programs. I
also I understand the concept and have written some small programs to
understand the theory.
class unit{
protected:
int locx;
int locy;

int atk;
int dfce;
void create();
public:
unit();
unit(std::istream);
virtual ~unit(){};
virtual unit* copy() const;
virtual void move();
virtual void attack();
virtual void display();
};

class hunit{

unit * p;
int * cnt;
public:
hunit() : cnt(new int(1)), p(new unit) {}
hunit(char);
hunit(const hunit& u) : cnt(u.cnt), p(u.p) {++*cnt;}
hunit& operator = (const hunit&);
hunit(std::istream);
~hunit();
void move();
void attack();
void display();
};

This all works. I am trying to show that I understand how to do dynamic
binding but not so well how to use it and how it can make my programs
better. I know about the shape example but often times, I don''t find
this kind of programming useful for what I do.

What I am more interested in is how I can look a programming from an
object oriented perspective. I also try to use inheritance in my
programs to save rewriting common parts of different objects. What is a
good way to go about stating with small but useful programs or solving
problems that I can learn from to help me see programming more from the
perspective of object orientation.

One last question about this program is about friends. That is another
feature I seldom use. I often find that when I create an object then
put it in a handle like I have above I often have to duplicate writing
the accessor functions:

virtual void move();
virtual void attack();
virtual void display();

void move();
void attack();
void display();

If I add something to the object in development, I have to go back and
add the functions to the handle. I am not looking for specific answer
to some piece of code but advice on how I can become a better programmer
and tap into the power that C++ offers.

推荐答案

Vitor写道:
Vitor wrote:

我最近一直在写一些大型程序和游戏而且我有

让人们告诉我,我需要编写更多面向对象的

程序。我也理解这个概念,并写了一些

小程序来理解这个理论。

[..]
I have been writing some large programs and games lately and I have
had people advise me that I need to write more object oriented
programs. I also I understand the concept and have written some
small programs to understand the theory.
[..]



查找高级C ++:编程风格和习语的副本作者:James

Coplien,以及他的C-D的多范式设计。我敢打赌,如果

你去''www.accu.org''并在书评部分环顾四周,

你会发现很多有趣的OOP作品。另外,请在论坛中询问

''comp.object''以获取更多书籍建议(提及C ++以避免

不得不涉及Smalltalk或Eiffel)。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Find a copy of "Advanced C++: Programming Styles and Idioms" by James
Coplien, and also his "Multi-Paradigm Design for C++". I bet that if
you go to ''www.accu.org'' and look around in the book review section,
you''ll find plenty of interesting OOP works. Also, ask in the forum
''comp.object'' for more book recommendations (mention C++ to avoid
having to wade through Smalltalk or Eiffel).

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Vitor< no ** @ yahoo.netwrote:
Vitor <no**@yahoo.netwrote:

这一切都有效。我试图表明我理解如何动态

绑定但不太好如何使用它以及它如何使我的程序更好地使用
。我知道形状的例子,但有时候,我不会发现这种编程对我的工作很有用。
This all works. I am trying to show that I understand how to do dynamic
binding but not so well how to use it and how it can make my programs
better. I know about the shape example but often times, I don''t find
this kind of programming useful for what I do.



如果你觉得它没用,那就不要用了。在许多情况下,OO概念可以很好地消除重复工作。例如,如果你有
五''if / switch''语句都是从同一个变量中分离出来的话,那就是使用虚拟调度机制来代替他们

全部。


这可以减少整个程序的周期性复杂性

这使得它更容易理解和维护。

If you don''t find it useful, then don''t use it. OO concepts do a good
job of removing duplication in many contexts. For example, if you have
five ''if/switch'' statements all branching off of the same variable it is
a simple matter to use the virtual dispatch mechanism to replace them
all.

This can reduce the cyclometric complexity of your program as a whole
which makes it easer to understand and maintain.


一切正常。我试图表明我理解如何动态
This all works. I am trying to show that I understand how to do dynamic

绑定但不太好如何使用它以及它如何使我的程序

更好。我知道形状的例子,但有时候,我不会发现这种编程对我的工作很有用。
binding but not so well how to use it and how it can make my programs
better. I know about the shape example but often times, I don''t find
this kind of programming useful for what I do.



好​​吧,在OOP(面向对象编程)中,多态性是

最重要的事情之一。

多态性意味着你调用像移动这样的方法。或攻击在

一个对象和不同的事情发生。


类演员{

私人:

m_life_points ;

公开:

虚拟空洞攻击(演员& foo)= 0;

虚拟空虚move()= 0;

virtual void reduce_life_points(){m_life_points--;};

}


class Hero:public Actor {

虚拟空洞攻击(演员&foo){

swing_sword();

foo.reduce_life_points();

}

虚拟空移动(){

run_forward();

}

}


class Monster:公共演员{

虚拟空洞攻击(演员&foo){

use_claws();

foo.reduce_life_points ();

}

虚拟空移动(){

run_forward();

}

}

在这种情况下,主循环可能会做这样的事情(伪代码):


为演员中的每个演员做

actor.move()


没有关心演员是英雄还是怪物或任何其他类型的玩家可能会在未来添加到游戏中的
。与使用

一个开关语句不同,没有必要更改循环以添加更多

种类的演员。


总和它了。如果你想在_RUN-TIME_

(与编译时相反)执行不同的行为,那么OOP很有可能对你有益。$ br。 >

对我而言,了解设计模式是完全有用的。

了解OO。然而,由于设计模式不易理解

而不了解OOP,你应该尝试一个简单的讲座,比如Head

First Design Pattern。阅读并快速帮助喘息OO很有趣。


问候,

Thomas Kowalski

Well, in OOP (object-oriented-programming) polymorphism is one of the
most important things.
Polymorphism means that you call a method like "move" or "attack" on
an object and different things happen.

class Actor {
private :
m_life_points;
public:
virtual void attack( Actor &foo ) = 0;
virtual void move() = 0;
virtual void reduce_life_points() {m_life_points--;};
}

class Hero : public Actor {
virtual void attack( Actor &foo ) {
swing_sword();
foo.reduce_life_points();
}
virtual void move() {
run_forward();
}
}

class Monster : public Actor {
virtual void attack( Actor &foo) {
use_claws();
foo.reduce_life_points();
}
virtual void move() {
run_forward();
}
}
In this case the main loop might do something like this (pseudo-code):

for each actor in actors do
actor.move()

without caring whether actor is a hero or a monster or any other kind
of actor that might be added to the game in the future. Unlike using
an switch statement there is no need to change the loop to add more
kinds of actors.

To sum it up. If you want to execute different behavior at _RUN-TIME_
(in opposite to Compile-time) there is a good chance that OOP is
beneficial for you.

For me it was helpful to learn about design pattern to fully
understand OO. However since design pattern are not easy to understand
without knowledge of OOP you should try an easy lecture like "Head
First Design Pattern". It''s fun to read and quick helpful to gasp OO.

Regards,
Thomas Kowalski


这篇关于如何使用面向对象的编程。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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