Java self-study notes (11): [object-oriented] package, import, access control modifier, static

package与import

In Java each define a class, after passing through the compiler java, will generate a .class file name extension, when the scale of the program is gradually huge, there will be the same name as the class phenomenon.

That package is the mechanism of the management class

How to define the package? Use the package keyword     package package name;   package can be overlapped with the sub-packet and the packet between the mother separated.   Package 1. The package names package name 2;

Note: When the class package name specified in the package statement needs to be placed in the first line of the program, must be the first line in the file non-commented code

Naming package:       by lowercase letters

Compile and run the program with the package:

package exaggerate;
public class demo{
    public static void main(String[] args){
        System.out.println("Welcome to Java");
    }
}

In cmd:

   (- d is in the current directory.)

 Operation of the first line of code will generate a folder (i.e., package)

The second line can be run again

(Java with a package can not run the normal way)

How to introduce the package?   Use import

These Packs import mechanism:

    Single Type introduced: as: import exaggerate.Person; import package name class name; // increase speed, to avoid conflict.

    Import demand type: such as:. Import exaggerate *; // * wildcard tells the compiler, the unknown class to find this package, this method affects the code compilation speed

Use package: classify and organize and permitted the same name


Access control modifiers

Java provides access control modifiers, you can give library developers to indicate to the client programmer who is available, who is unavailable

Access control levels: public> protected> default> private

Kind of Java class: class // external use only public, default

  // inner class can

                                                                         Modified class:

  public protected default private
similar
Package with different classes ×
Different packages × × ×

The distinction between protected and default on inheritance

                          Modified class members

 

public

protected default private
Within the same class
Package with different classes ×
Different packages × × ×
Subclasses (in different packages) × ×

The static keyword

Static members: can be modified using the static member variables, constants, methods and code blocks

Static member is global, all owned by the entire class, not dependent on a particular object, is shared by all classes of objects

As long as the virtual machine is loaded, you can find them in the global data field based on the class name

Access format: name of the class static member

 

Static variables, also known as static member variables, refer to the modified keyword static member variables. The same life cycle and life-cycle class, all objects are shared classes

Example:

public class sellTickets {
    String name;
    int tickets=10;   //非静态
    sellTickets(String name){
        this.name=name;
    }
    void selling() {
        while(tickets>0) {
            tickets--;
            System.out.println(name+"剩余票数: "+tickets);
        }
    }
    public static void main(String[] args) {
        sellTickets demo1 =new sellTickets("第一台");
        sellTickets demo2 =newsellTickets ( "second stage" ); 
        demo1.selling (); 
        demo2.selling (); 
    } 

}

operation result:

public class sellTickets {
    String name;
    static int tickets=10;  //静态
    sellTickets(String name){
        this.name=name;
    }
    void selling() {
        while(tickets>0) {
            tickets--;
            System.out.println(name+"剩余票数: "+tickets);
        }
    }
    public static void main(String[] args) {
        sellTickets demo1 =new sellTickets("第一台");
        sellTickets demo2 =new sellTickets("第二台");
        demo1.selling();
        demo2.selling();
    }

}

 

运行结果:

 

可见tickets被共享

 静态方法同理

 非静态方法中可以进行静态方法的调用,反之不可


累死窝啦

                                                                              

 

To  Be  Continued·········· 

 

Guess you like

Origin www.cnblogs.com/tkj521Ya/p/11224521.html