Java Learning (11), object-oriented programming (3) Encapsulation-package, java Object-Oriented Programming

Source: Internet
Author: User

Java Learning (11), object-oriented programming (3) Encapsulation-package, java Object-Oriented Programming

Significance of packaging:

① The standard Java library is composed of a series of packages, including java. lang, java. util, and java.net. The standard Java package is a hierarchical package structure. Just like the subdirectories nested on the hard disk, we can use nested hierarchies to form a package;

② Java packages are designed to better plan code and prevent name conflicts and confusion. Therefore, there is a packaging mechanism in Java;

③ When the class is organized into a package, the package members are given the access permission to each other and the program code in the package is "owned;

④ Package access permissions provide meaning and reason for clustering classes in a package;

Package ---- declare package

 

Package com. iotek. ch09;

Public class lotekTeacher {

Private String name; // instructor name

Private int age; // age of the instructor

/**

* Package access permission

* @ Return self-introduced content

*/

String introduction (){

Return "Hello everyone, I am" + name + ", this year" + age + "years old ";

}

}

① The package name consists of lower-case words;

② Java programmers can write their own Java packages. To ensure the uniqueness of each Java package name, the programming specification requires programmers to add a unique prefix before the name of their own package;

③ Because domain names on the Internet are not repeated, we generally recommend that you use the domain name inversion on the Internet as the unique prefix of the package;

 

A class can use all classes in the same package and all public classes in other packages.

You can access the public classes in other packages in two ways.

① Simply add the complete package name before each class, for example, java. util. Date today = new java. util. Date ();

② The more concise and more general method is to use the import Statement to export the package.

Import java. util. Date;

...

Date today = new Date ();

 

You can import a specific class or the entire package. The import Statement is used at the top of the source code file (after the package Statement.

Steps:

Create a package folder in the javastudy folder on disk D, and write the program named Dog. java in it

Source program

1 package com. iotek. entity; 2 public class Dog {3 String name; 4 public int age; 5 public void show () {6 System. out. println (name + "------" + age); 7} 8}View Code

The preceding packaging path is in entity under iotek under com.

 

Compilation Method: after entering the program path, javac-d. Dog. java

After compilation is successful, the compiled file is generated in the package path.

 

In the same package

Compile a DogTest source program

1 package com. iotek. entity; 2 public class DogTest {3 public static void main (String [] args) {4 Dog dog = new Dog (); 5 dog. name = "wangcai"; 6 dog. age = 5; 7 dog. show (); 8} 9}View Code

After compilation

Running Method: java com. iotek. entity. DogTest

 

The name attribute in Dog. java is accessible to other classes in the same package.

 

In different packages

Delete the com folder first

The package path in DogTest. java is changed to package com. iotek. test;

As follows:

1 package com. iotek. test; 2 public class DogTest {3 public static void main (String [] args) {4 Dog dog = new Dog (); 5 dog. name = "wangcai"; 6 dog. age = 5; 7 dog. show (); 8} 9}View Code

Compile the Dog. java program first, and compile the DogTest. java program.

Then change the DogTest. java program again.

1 package com. iotek. test; 2 public class DogTest {3 public static void main (String [] args) {4 com. iotek. entity. dog dog = new com. iotek. entity. dog (); 5 dog. name = "wangcai"; 6 dog. age = 5; 7 dog. show (); 8} 9}View Code

The compilation still fails.

The error is caused by the name attribute in Dog. java. It should be changed to public String name;

The source program is as follows:

1 package com. iotek. entity; 2 public class Dog {3 // String name; // other classes in the same package can be accessed, 4 // other classes in different packages cannot access the attributes and methods of the default access modifier 5 public String name; 6 public int age; 7 public void show () {8 System. out. println (name + "------" + age); 9} 10}View Code

After the modification, re-compile Dog. java and re-compile DogTest. java. No error is reported this time and the code is generated successfully.

Then run java com. iotek. test. DogTest

However, the global package name in DogTest. java is too troublesome.

Change to the import method. The package must be the first line.

1 package com. iotek. test; 2 import com. iotek. entity. dog; 3 public class DogTest {4 public static void main (String [] args) {5 Dog dog = new Dog (); 6 dog. name = "wangcai"; 7 dog. age = 5; 8 dog. show (); 9} 10}View Code

Re-compilation can also be executed.

 

To sum up:

The final code of Dog. java and DogTest. java is as follows:

1 package com. iotek. entity; 2 public class Dog {3 // String name; // other classes in the same package can be accessed, 4 // other classes in different packages cannot access the attributes and methods of the default access modifier 5 public String name; 6 public int age; 7 public void show () {8 System. out. println (name + "------" + age); 9} 10}Dog. java 1 package com. iotek. test; 2 import com. iotek. entity. dog; 3 public class DogTest {4 public static void main (String [] args) {5 Dog dog = new Dog (); 6 dog. name = "wangcai"; 7 dog. age = 5; 8 dog. show (); 9} 10}DogTest. java

Then

Javac-d. Dog. java

Javac-d. DogTest. java

Java com. iotek. test. DogTest

You can create and call a package.

Related Article

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.