Java packages vs. C++ libraries

后端 未结 6 1808
死守一世寂寞
死守一世寂寞 2021-01-18 08:52

In Java, there is what is called package. Does library in C++ represent the same meaning, especially in terms for example

6条回答
  •  鱼传尺愫
    2021-01-18 09:25

    A package in Java is a namespace for classes, interfaces and enums. 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!

    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.

    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.

提交回复
热议问题