"Java Fundamentals" "Eighth Lesson" package

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.