java 类和包的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7021295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between class and package
提问by surya narayana raju g
What is the difference between a class and a package in Java ?
Java 中的类和包有什么区别?
回答by maerics
A classis a declaration (and often implementation) which encapsulatesthe members properties and methods which instances of that type will posses. You can create instances of a class as distinct, individual objects that behave as specified.
甲类是一个声明(通常实现),其封装所述构件的属性和方法,其该类型的实例它们所具有。您可以将类的实例创建为按指定行为的不同的单独对象。
A packageis simply a namespaceunder which you can categorize classes, analogous to a "folder" or "path", so you can group classes with related purpose or functionality.
甲包是一个简单的命名空间在其下可以分类类,类似于一个“文件夹”或“路径”,这样就可以与相关的目的或功能组的类。
Don't be confused by the fact that Java has classes which represent both the concept of a Classand a Packagewhich are useful for inspecting the structure of Java programs at runtime. They are merely models of the general concepts described above.
不要对 Java 具有表示类和包的概念的类这一事实感到困惑,这些类对于在运行时检查 Java 程序的结构很有用。它们只是上述一般概念的模型。
回答by Buhake Sindi
Quite simply a java packageis a namespace where classes are organized based on the same category or functionality
很简单,java 包是一个命名空间,其中的类基于相同的类别或功能进行组织
Java classes is an object that consists of behaviours (methods), properties (variables), constructors (if any, for object creation), etc. that exists inside a folder (package).
Java 类是一个对象,由存在于文件夹(包)中的行为(方法)、属性(变量)、构造函数(如果有,用于创建对象)等组成。
回答by ngesh
A class is like a Template, where an you put set Behaviors (Methods)
and properties (Instance variables)
which every Object of its kind will Posses.
一个类就像一个模板,你在其中放置了一个集合Behaviors (Methods)
,properties (Instance variables)
并且每个同类对象都将拥有它。
but package is totally different and has nothing to do with technical part. It is just for the purpose of Readability and Organizing your code. you have to put classes with different behaviour in diff package. thats it.
但包装完全不同,与技术部分无关。它只是为了可读性和组织您的代码。您必须将具有不同行为的类放在 diff 包中。而已。
回答by Ratna Dinakar
For understanding it more clearly,just think a package as a box of toolset. You have various tools in a toolset box i.e a screw driver,cutter,cutting pliers etc. Each tool set box is a package which is collection of various tools ( classes). Now here each tool ie a screw driver is a class , cutter another class etc, each of tool having their own properties and purpose ( operation).In the similar way you have many toolsets designed for domestic,industrial purposes which can be compared to different packages.
为了更清楚地理解它,只需将包视为一盒工具集。工具箱中有各种工具,例如螺丝刀、刀具、切割钳等。每个工具箱都是一个包,其中包含各种工具(类)。现在这里的每个工具,即螺丝刀是一个类,刀具是另一个类等,每个工具都有自己的属性和用途(操作)。以类似的方式,您有许多为家庭、工业目的设计的工具集,可以比较不同的包。
Hope this helps :)
希望这可以帮助 :)
回答by Waqas
Package is a mechanism for organizing Java classes into namespaces. Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time.
包是一种将 Java 类组织到命名空间中的机制。Java 包可以存储在称为 JAR 文件的压缩文件中,允许类作为一个组下载而不是一次一个下载。
on the other hand, a class is a programming language construct that is used to group related instance variables and methods. Each class expresses structural and behavioral design decisions made by the programmer to indicate what types of objects can exist when the program is executed.
另一方面,类是一种编程语言结构,用于对相关的实例变量和方法进行分组。每个类都表达了程序员做出的结构和行为设计决策,以指示在执行程序时可以存在哪些类型的对象。
Reference: Coders2020
参考:Coders2020