全局功能与面向对象 [英] Global functions vs. object orientation

查看:105
本文介绍了全局功能与面向对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总而言之,



在我们的C ++ / MFC项目中,我们有以下结构:



  class  A {
public
bool FileExists(...);
};

class B {
public
< span class =code-keyword> bool FileExists(...);
};

class C {
public
< span class =code-keyword> bool FileExists(...);
};

// ...





在某些类中,有相同的函数可以检查是否存在XML文件。



但是这些类太不同了,无法创建一个基类来存储函数。



所以我们决定将FileExists写成一个全局函数并将其放入

stdafx.cpp现在是我们的全球功能存储。



我的问题:



像C ++这样的旧语言的优点允许使用

全局函数和变量,导致它的缺点是面向对象

被打破。



还有哪些其他的状态可以解决上层问题?



如何用C#这样的语言来解决它?或禁止全局内容的Java?



祝你好运

解决方案

引用:

如何在C#或Java等语言中解决全局内容被禁止的问题?

你创建一个静态类和把静态方法放在那里。

所以它可以从任何你需要的地方访问。


Quote:

但是这些类太不同了,无法创建存储函数的基类。

在我看来,这不是一个有效的异议。





Quote:

所以我们决定将FileExists写成一个全局函数并放入它现在是我们的全局函数存储器./ / blockquote>你可能不需要它(尽管它没有错)你可以为此创建一个静态类。 / blockquote>

1)听起来你的对象模型可能是错误的,这就是 OO被打破的原因。我会先检查一下。



关于如何在C#中解决问题:



2)正如 lukeer 建议的那样,你可以在一个类中创建一个静态方法,可能是一个静态类,与方法处理的对象类型相关。

- 在 FileExists 的情况下,已经有一种以这种方式精确实现的.NET方法; 文件的静态方法[ ^ ] class:System.IO.File.Exists [ ^ ]



3)你可以创建一个扩展方法函数处理的对象类型。



4)您可以实现一个接口类,并将每个类定义为具有该接口。如果您需要以特定于类的方式表现不同的相同方法,这将特别有用。 (例如 Dispose [ ^ ])。



问候,

Ian。


Hallo all together,

In our C++/MFC project we have the following structure:

class A {
public:
	bool FileExists(...);
};

class B {
public:
	bool FileExists(...);
};

class C {
public:
	bool FileExists(...);
};

//...



In some classes there is the same function which checks the existence of a XML file.

But the classes are too different to create a base class to store the function.

So we decided to write "FileExists" as a global function and put it into
stdafx.cpp which is now our global function storage.

My question:

The advantage of an "old" language like C++ allows the usage of
global functions and variables, with leads to its disadvantage that object orientation
is broken.

What other stategies are available to solve the upper "problem" ?

And how can it be solved in languages like C# or Java in which global stuff is forbidden ?

Best regards

解决方案

Quote:

And how can it be solved in languages like C# or Java in which global stuff is forbidden ?

You create a static class and put the static method there.
So it''s accessible from wherever you need it.


Quote:

But the classes are too different to create a base class to store the function.

This is not a valid objection, in my opinion.


Quote:

So we decided to write "FileExists" as a global function and put it into
stdafx.cpp which is now our global function storage.

You don''t need that (albeit it is not wrong) you might create a static class for the purpose.


1) It sounds like your object model may be wrong, which would be why "OO is broken". I would check that in the first instance.

With respect to how the "problem" would be solved in C#:

2) As lukeer has suggested, you could create a static method in a class, possibly a static class, related to the type of object the method deals with.
-- In the case of "FileExists" there is already a .NET method implemented precisely in this way; a static method of the File[^] class: System.IO.File.Exists[^]

3) You could create an extension method to the type of object that the function deals with.

4) You could implement an interface class and define each class as having that interface. This is particularly useful if you need the same method(s) which can behave differently, in a class-specific way. (E.g. Dispose[^]).

Regards,
Ian.


这篇关于全局功能与面向对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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