课程: Java Essential Training: Syntax and Structure
Packages
- [Instructor] In Java, a package is a folder or container that holds Java files. Let's first create a project so that we can create a package within our project. Within IntelliJ choose to create a new project, select Java as the type and click next. Make sure that you don't have a template selected and click next. Then we need to give it a project name. We'll just call this one Java essentials and click finish. Now on the left, you'll see the project view. This is where all of our files live. So far we don't have much because this is a new project but we can add things to this source folder. Source code is also a term that's used for programs. So under the source folder, we can right click, hover over new, and then choose to add a new package. Again, a package is a special folder in Java that holds one or more Java files. You can have multiple packages within a project. In fact, you should, depending on the relationship between the code. So we want to make packages for the logical grouping of related Java files. Okay. We're ready to create our first package. As you can see we're prompted to give the package a name. For Java packages, the convention is that the packages should be all lowercase. This is not a rule. Meaning your programs won't break if you don't follow this, however, this is a convention. And when I say convention throughout the course, this means this is what's been adopted and accepted by Java programmers worldwide. Following these conventions makes it easy for you to know what's what within your program as well as other programmers who have read or edit your code. Let's go ahead and give it a package name. We're going to call this gross_calculator as that's the logical name for a folder that will hold all of our files that we create in this section of the course. We can hit Enter or press OK if you have that button. And then if we expand this source folder, notice, now we do have a new package which is called gross_calculator. Notice there's a little circle icon on here to denote a package. Great job creating your first package.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。