仅包含私有成员的Java类 [英] Java class containing only private members

查看:104
本文介绍了仅包含私有成员的Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我遇到了需要创建自定义

Lately I met a situation where I needed to create a custom VideoView to my android application. I needed an access to the MediaPlayer object and to add some listeners.

不幸的是(对我而言),VideoView类的所有成员都是私有的,因此即使扩展该类也无法帮助我访问其MediaPlayer对象(或其他任何对象),我必须对修改后的课程.

Unfortunately (for me), all members of the VideoView class are private, so even extending the class wouldn't help me to gain access to its MediaPlayer object (or anything else), I had to make a complete duplicate of the class with my modifications.

好吧,虽然听起来像我在抱怨辛勤工作",但在这种情况下,它比扩展类要容易得多(因为所有可用的源代码都可以使用...),但是这让我真的很怀疑信息隐藏方法. 这是比让主要组件可用于修改/访问(受保护的,而不是公共的)更好的做法吗?我的意思是,我知道,如果我扩展VideoView类,也许有一天它们会在VideoView类,我可能会遇到麻烦,但是如果他们改变类,我自己的(重复的)版本将与VideoView类有更大的不同,我的目标不是创建自己的视频视图,而是扩展可用的VideoView.

Well, although it is sound like I'm complaining for the "hard work", it is easier than extending the class in this case (since all the source is available...), but it made me really doubt this method of information hiding. Is this a better practice than leaving main components available to modification / access (protected, not public)? I mean, I understand that if I extend the VideoView class, someday maybe they'll change something in the VideoView class and I might have troubles, but if they'll change the class, my own (duplicate) version will have a bigger difference from the VideoView class, and my goal is not to create my own video view, but to extend the available VideoView.

推荐答案

当程序员将某项内容设为私有时,他们打赌其他人将永远不需要使用或覆盖它,因此将从中获得收益.信息隐藏.有时候,这种赌注不会消失.他们是休息.

When a programmer makes something private, they're making a bet that nobody else will ever need to use or override it, and so there will be a payoff from the information hiding. Sometimes that bet doesn't come off. Them's the breaks.

这篇关于仅包含私有成员的Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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