Object oriented basic features: a package

Benefits package

1, the caller: convenience / ease of use

2, designer: safe, controllable

How the package, different levels of control packages

Usage rights modifier

Modifiers This class This package Other subclasses package Other packages of non-child class
private × × ×
default × ×
protected ×
public

What rights modifier can be modified?

All rights modifiers can be modified: member variables, member methods, constructors, members of the inner class

Permissions can be modified outside of class modifiers: default and public

Property privatization

1, in front of the private property plus

2, to provide get / set methods according to the needs

  1 public class Student{
  2 	private String name;
  3 	private int age;
  4 	private boolean marry;
  5 
  6 	public void setName(String name){
  7 		this.name = name;
  8 	}
  9 	public String getName(){
 10 		return name;
 11 	}
 12     public void setAge(int age){
 13         this.age = age;
 14     }
 15     public  int getAge () {
 16          return Age;
 . 17      }
 18 is      public  void setMarry ( Boolean Marry) {
 . 19          the this .marry = Marry;
 20 is      }
 21 is      public  Boolean isMarry () { // Boolean type property, which get standard method , is replaced with the GET 
22 is          return Marry;
 23 is      }
 24 }

package

1, the role of the package

(1) control the visibility range

If a class or member in this package, it's the default permissions modifier, it means the class or membership is limited to the use of this package

(2) avoid duplicate names class

(3) according to different themes to classify and Management

Because the package is compiled in the corresponding folder

Common package:

java.long: the core language libraries, for example: String, System, Math, Thread ...

java.io: Input Output related

java.util: tools

java.net: network programming and related

java.sql: database and related

2, how to declare package

  1  Package Penalty for package name;

The position must be the first line of .java files, a file can have a .java

Naming names package: (1) all the words are lowercase, using an inverted split between words + module with a company domain name (2) habits.

3, how to compile source files with the package

  1 javac -d. .Java source file name
  • -d: Create a package directory structure
  • .: That in the current directory

4, how to run the class representatives

  1 the Java package. Class name

5, how to use other package classes

Full Name (1) practical classes

  1 java.util.Scanner input = new java.util.Scanner(System.in);

(2) use the import statement leader packet used in the code name Jane

  . 1  Import package name class name;.
   2  Import package name. *; // * Only the class name is omitted in the last stage 
  . 3  
  . 4  // static import 
  . 5  Import  static package name class name static member;..
   . 6  Import  static package names The class name *;

When two different packages, but when the class name the same two classes, can only use a lead bag, use a full name, you can not use both lead pack.

Class or members of other packages to be used, it must be the authority modifier> default

Guess you like

Origin www.cnblogs.com/daidai66/p/11992838.html