Since learning Java, the first time feeling a little bit difficult to understand, let's talk about it.
Existence Meaning:
The existence of a package is for a better organizational structure
The creation of the package:
As long as the top of the program to join the package ******, you can, generally like to write com.carter.animal and so on, and so on, just beginning to think that this thing is a very complex thing, is actually representative of a directory structure used,
If you don't understand it at first, you don't have to write so many ".", I'm confused by this point.
After writing this sentence, eclipse will help you to change the name of the package to use it:
With this stuff, it's like the classes and functions have to be declared public.
The call to the package:
I'm going to use it for two different ways:
1. Use the Import keyword, for example: import com.carter.test.*;
2. Use it directly when calling the class: Com.carter.animal.animal, what a good way to be violent.
Well, first look at the relationship between the code structure, and then look at the source:
Well, look at the source structure:
Package Com.carter.animal; Public class Animal { publicvoid display () { System.out.println ("Wang ... Wang ... "); }
Animal
Package publicclass human{ // constructor 1 Public Human (int h) { System.out.println ("construct 1" + h); } // Constructor 2 Human (int h, String str) { System.out.println ("construct 2" + H + "+ str"); }}
Human
PackageCom.carter.dream;Importcom.carter.test.*; Public classmain{ Public Static voidMain (string[] args) {//import the corresponding package, and then use the classTest atest =Newtest (); Atest.nothing (); //without import, you can invoke the class directly using this method.Com.carter.animal.animal Adog =NewCom.carter.animal.animal (); Adog.display (); }}
Main
Package com.carter.test; import com.carter.dream.*; Public class test{ public Test () { System.out.println ("test class"); } Public void Nothing () { int i; = 9; } New Human (1);}
Test
Output Result:
Construct 1 1
Test class
Wang.....wang .....
"Java Fundamentals" "Eighth Lesson" package