简体   繁体   English

Java包与C ++库

[英]Java packages vs. C++ libraries

In Java , there is what is called package . Java ,有所谓的package Does library in C++ represent the same meaning, especially in terms for example of containg relative classes and the use of protected members? C++ library是否代表相同的含义,特别是在包含相对类和受保护成员的使用方面?

Thanks. 谢谢。

There are different dimensions of what a package means in Java. package在Java中的含义有不同的维度。 As a container that differentiates the names of the classes inside from the names of classes in other packages, its equivalent would be c++ namespaces . 作为一个容器,它将内部类的名称与其他包中的类的名称区分开来,它的等价物将是c ++ namespaces

As a unit that guarantees access to non-private members to classes in the same block, there is no equivalent in C++. 作为一个保证访问同一块中的类的非私有成员的单元,C ++中没有相应的东西。 The access level granted to a class is independent of the namespace where the class is defined. 授予类的访问级别独立于定义类的命名空间。

As a way of ordering your sources in the disk, there is no equivalent, the C++ language has no requirements on how the code is stored in files. 作为一种在磁盘中对源进行排序的方法,没有相应的,C ++语言对代码如何存储在文件中没有要求。

Regarding c++ libraries, that is closer to jar files in Java. 关于c ++库,它更接近Java中的jar文件。 They bundle different classes that share some relation. 它们捆绑了不同的类,这些类共享某种关 A jar can contain more than one package, and more than one jar can contain classes from the same package. jar可以包含多个包,并且多个jar可以包含来自同一包的类。 Similarly with libraries, they can contain classes from different namespaces and/or different libraries can contain classes from the same namespace. 与库类似,它们可以包含来自不同命名空间的类和/或不同的库可以包含来自相同命名空间的类。

The closest to Java packages are namespaces in C++. 最接近Java包的是C ++中的命名空间

They can be nested into one another, and you need to specifically declare that you are using them or a part of their contents. 它们可以相互嵌套,您需要明确声明您正在using它们或它们的一部分内容。 However, they do not enforce any physical file hierarchy like Java packages do. 但是,它们不像Java包那样强制执行任何物理文件层次结构。

严格来说,我认为C ++中的命名空间提供了相同的语义。

I guess it is more related to namespaces in C++. 我想它与C ++中的命名空间更相关。 Java and C++ both use libraries. Java和C ++都使用库。 Library can be any independent set of classes[probably a framework] which can be accessed in our code. 库可以是任何独立的类[可能是框架],可以在我们的代码中访问。

External Libraries are there in both Java and C++. Java和C ++都有外部库。 Just the formats vary, .jar in Java and .dll/.so in C++. 只是格式不同,Java中的.jar和C ++中的.dll / .so。

Purpose of Packages and Namespaces are different from Libraries. 包和命名空间的用途与库不同。 They avoid running out of names by allowing user to logically group the source. 它们允许用户对源进行逻辑分组,从而避免耗尽名称。

A package in Java is a namespace for classes, interfaces and enums. Java中的package是类,接口和枚举的命名空间。 Package name, a dot and the classname form the fully qualified classname of a class: 包名,点和类名构成类的完全限定类名

com.example.hello.HelloWorldApplication
^--packagename--^ ^-----classname-----^

The dots in the package name have a different meaning then the dot between the names: the first two dots of this example are part of the package name , the last one is a separator. 包名称中的点与名称之间的点具有不同的含义:此示例的前两个点是包名称的一部分 ,最后一个是分隔符。

This should be kept in mind, because there's a common misunderstanding regarding package names: just because the names can be mapped to a hierarchical folder structure, some people think, package names have a hierarchy too - which is not the case: hello is not a "subpackage" of example ! 这应该牢记在心,因为对包名有一个常见的误解:只是因为名称可以映射到分层文件夹结构,有些人认为,包名也有层次结构 - 事实并非如此: hello不是“subpackage”的example

But, to create a simple mapping to folders and files, a classloader can simply take the fully qualified class name, replace all dots with a slash and append .class to get a relative path to a class file. 但是,要创建到文件夹和文件的简单映射,类加载器可以简单地获取完全限定的类名,用斜杠替换所有点并附加.class以获取类文件的相对路径。

But note again, that a folder/file mapping is not required t load classes - we can invent a class loader that gets classes from a database or a remote service - a folder/file mapping wouldn't make any sense in that case. 但请再次注意,加载类不需要文件夹/文件映射 - 我们可以发明一个从数据库或远程服务获取类的类加载器 - 在这种情况下文件夹/文件映射没有任何意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM