Java learned fourteen from scratch (package and Access Control), java started from scratch

Source: Internet
Author: User

Java learned fourteen from scratch (package and Access Control), java started from scratch
1. java package

Java file organizationFile functions in Windows are similar.

When developing a large project, it is impossible to involve only one java file. It may involve creating dozens or even hundreds of java files. At this time, we can use the package, put related java files together,Use packages to conveniently, quickly, and effectively manage these files

Package introduction is also possibleAvoid name conflictsThe class names under different packages can have the same name.

Ii. Define packages

Package name

This statement must be placed in the first line of the java source program. No executable code exists before it, except for comments.

Packages can be created at multiple levels. Different layers are separated by DOTS (.), which are the same as the multi-layer files in windows.

Definition package: Multi-Level

Package name 1. package name 2. package name 3 ......

The package names are separated ".".

Create a package in eclipse:

 

3. Import packages

Import package name. Class Name

For example, create a class

PackageCom. pb. person;// Package name public class Person {private String name; // name private String sex; // gender private int age; // age // set getter and setter Methods public String getName () {return name;} public void setName (String name) {this. name = name;} public String getSex () {return sex;} public void setSex (String sex) {if (sex. equals ("male") | sex. equals ("female") {// sets the Gender limit. If the value is invalid, an error is returned. sex = sex;} else {System. out. println ("invalid gender, Gender: male or female") ;}} public int getAge () {return age;} public void setAge (int age) {if (age> 0 & age <= 150) {// set the age limit this. age = age;} else {System. out. println ("the age must be between 1 and") ;}} public Person () {this. name = "anonymous"; this. sex = "male"; this. age = 22;} public Person (String name, String sex, int age) {this. name = name; this. sex = sex; this. age = age;} public void say () {System. out. println ("self-introduction:"); System. out. println ("name:" + this. name); System. out. println ("Gender:" + this. sex); System. out. println ("Age:" + this. age );}}
Package com. pb. person; // package name
Create a test class in another package
PackageCom. pb. persontest;// Import the Person class with the import package name. class Name import com. pb. person. person; public class PersonTest {public static void main (String [] args) {Person person Person = new person (); Person. setName ("HAN Bing"); // enter an invalid person value. setSex ("neutral"); // enter an invalid person value. setAge (200); // call a common method to output person. say ();}}

Shortcut Key Ctrl + Shift + O import package

Iv. Common packages

Java. lang Package: contains the basic class of the Java program language. This package can be used without being imported, such as the String class.

Java. until package: contains various tool classes

Java. io package: class that contains various input and value output functions

Java. SQL package: contains database operation-related classes

V. Access Control 5.1. Class Access Control

Class access modifier:

  • Public modifier: public access level. classes in other packages can access this class.
  • Default modifier: Private access level of the package. This class can only be accessed by classes in the same package. classes in other packages cannot access this class.
5.2 access control for Class Members

 

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.