English 中文(简体)
如何在事先界定的类别中增加方法或分类?
原标题:Better to add method to pre-defined class or make subclass?

你们想在预先界定的档案类别中添加一个长度的OfFirstLine方法。 改变现有类别,或采用新的方法使档案类别扩大的新类别,是否是一种更好的做法?

www.un.org/spanish/ecosoc 具体来说,情况是,某类人尤其缺乏一种方法。 我不想完全改变这一类别,而是用这种方法加以扩充。

最佳回答

这取决于该方法是否适用于所有类别卷宗。 例如,OfFirstLine并不适用于双亲档案,因此可能不属于普通档案。 卷宗类别,但如果您的班子仅代表文本档案,那么就应该去。

问题回答

对于NET语言,还可选择使用extension methods。 这样,你就不必通过增加助手/实用方法来“放弃”一类,也无需继承——你只是通过在你的法典中添加“<>条码/条码”的字句添加“功能”。

Agree with Luis and Lester. If you are using .Net the extension methods are the way to go for this sort of functionality. But you should try not add LengthOfFirstLine to a base class if you can open all sorts of files such as binary files. You would sub class it to a FileClass and add the method to that.

Remember that the extension methods in .Net are syntactic sugar anyway. You can simulate it in your own language using Static classes and methods. This is what .Net does under the covers anyway.

例如,有静态卷宗Helpers,有各种静态帮助方法。 这些静态方法中的第一个参数是文件类别。 因此,你可以使用<代码>来说明这一点。 文件:





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签