提供Java库,但隐藏了一些类 [英] Providing Java library, but hiding some classes

查看:59
本文介绍了提供Java库,但隐藏了一些类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Java ME中的应用程序,我希望将其作为库提供。有没有办法隐藏我不希望每个人都使用的课程,但是为了让图书馆工作还是必不可少的?

I am developing an application in Java ME that I want to provide as a library. Is there no way to hide classes that I don't want everyone to use, but is essential still in order for the library to work?

更新:
我知道我可以省略公共说明符,但是如何在开发的同时构建库本身而不创建不同的包?我喜欢将不同的包视为不同的文件夹,这使我能够以良好的方式构建代码。但是,在某些情况下,我可能需要访问其他包中的类,因此这非常棘手。包真正代表什么?一个想法可能是创建接口,但这些必须被声明为公共,这意味着外国人也可能只实现仅用于库内某些进程的接口,对吗?

UPDATE: I get that I can omit the public specifier, but how can I structure the library itself while developing without creating different packages? I like to view different packages as different folders simply which allows me to structure the code in a good way. However, in some cases I might need to access classes in other packages so this is rather tricky. What does packages really represents? One idea might be to create "interfaces", but these has to be declared public so that means that foreigners might also implement the interfaces intended only for some processes inside the library, correct?

推荐答案

要设置库API,您需要保护
不想暴露的任何内容。这样做只是省略了访问修饰符:

For setting up your library API you'll want to protect anything you don't want exposed. Do do this just omit the access modifier:

class fooBar {
    // do stuff here    
}

这会将类访问设置为'default',允许在
内访问包以及子类
fooBar 的任何类。

This will set the class access as 'default' which allows access from within the same package as well as from any classes which subclass fooBar.

在您的课程中,您还需要通过将它们标记为 private protected 或省略修饰符以锁定对方法和成员的任何访问权限根据需要是'默认'。

Within your classes you will also want to lock down any access on your methods and members by marking them either private, protected or omitting the modifier so that they are 'default' as required.


  • private 将允许从包含类访问only;

  • 'default'(无修饰符)允许在包含的类中包含包;和

  • protected 将允许从同一个类,包和任何子类中进行访问。

  • private will allow access from the containing class only;
  • 'default' (no modifier) allows from within the containing class and containing package; and
  • protected will allow access from within the same class, package and any subclasses.

对于你暴露的任何事物( public ),将它标记为也是一种好习惯。最终如果它不是为了被覆盖而设计的。

For anything that you have exposed (public) it is also good practice to mark it as final if it's not designed to be overridden.

基本上,尽可能多地锁定所有内容。较小的API更易于使用且更难破解。如果您发现将来需要展示某些内容,请在将来进行。扩展API要比弃用部分API容易得多。

Basically, lock down everything as much as you can. Smaller API's are easier to use and harder to break. If you find something needs to be exposed in the future, do it in the future. It's much easier to expand an API rather than deprecate parts of it.

这篇关于提供Java库,但隐藏了一些类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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