"Java study notes," Chapter 9 - File Management

In Java, each defined class, will be compiled in a file extension to the .class stored. In the commonly used Java packages in a hierarchical way to manage class file.

1, the embedded category

Class may also be re-defined class, called inline type (Inner Class) or a nested class (Nested Class). Non-static inner class can be divided into three types: members of the inner class (Member Inner Class), the region nested class (Local Inner Class) and anonymous inner class (Anonymous Inner Class), the main purpose of the embedded category, is the existence of hidden outside class.

1) members of the class and the embedded region embedded class

The benefits of using embedded classes: one is embedded in its class can directly access the private members of the class; the other is when a class entirely Slave only serve a Master class, it can be set to inline class, so using the Master class people do not know exist Slave class; Furthermore, as in "static factory" (static factory) mode, the static method call on the object to hide implementation details or return the object generation mode.

Direct declare a class in a class when members of inner classes. E.g:

 

public class OuterClass{

    // inner class

    private class InnerClass{

    // ...

    }

}

 

Embedded class members can also be modified using its access to public, protected or private.

 

EX9.1 PointDemo.java

public class JavaNotesChap0901_PointDemo
{
    private class Point
    {
        public int x;
        public int y;

        public Point()
        {
        }

        public void setPoint(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public int getX()
        {
            return x;
        }

        public int getY()
        {
            return y;
        }
    }

    Point[] points;

    public JavaNotesChap0901_PointDemo(int length)
    {
        points = new Point[length];
        for(int i = 0; i < points.length; i++)
        {
            points[i] = new Point();
            points[i].setPoint(i*5, i*5);
        }
    }

    public void showPoints()
    {
        for(int i = 0; i < points.length; i++)
        {
            System.out.printf("Points[%d]: x = %d, y = %d\n", i, points[i].getX(), points[i].getY());
        }
    }
}

 

The program assumes that only serve PointDemo Point class category, the outside world does not have to know the existence of the Point class, as long as know how to achieve instances PointDemo on it.

 

EX9.2 PointShow.java

public class JavaNotesChap0901_PointShow
{
    public static void main(String[] args)
    {
        JavaNotesChap0901_PointDemo demo = new JavaNotesChap0901_PointDemo(5);
        demo.showPoints();
    }
}

 

In document management, file members of the inner class after the compilation is complete, the resulting called "external embedded class name class name $ .class".

Regional embedded with members of the class using the embedded category similar. Embedded in a region defined class methods. Visual range class of the object is limited to the generating process.

2) anonymous inner class

Inline anonymous class can not declare the class name, and a direct result of using the new object, which can be inherited or a class implements an interface. Its declaration as follows:

new [class or interface ()] {

    //achieve

}

An example of the use of inline anonymous class as shown below, directly attributable to it an anonymous class Object class definition, redefinition toString () method, and a direct result of the use of anonymous class object.

 

EX9.3 AnonymousClassDemo.java

public class JavaNotesChap0902_AnonymousClassDemo
{
    public static void main(String[] args)
    {
        final int i = 10;
        Object obj = new Object()
        {
            public String toString()
            {
                return "AnonymousClass Object " + String.valueOf(i);
            }
        };

        System.out.println(obj);
    }
}

 

When using System.out.println () when the object if passed, will call the object's toString () method String instance.

Note that if you want to use an external class area in the embedded variable, the variable must be declared in the final, otherwise during compilation, the compiler will complain.

In document management, inline anonymous class generated after the completion of the translation "$ name outside the class number .class", numbered 1,2,3, ..., n, each corresponding to the file number n of the n-th anonymous class .

 

2、package 与 import

1) setting packet (Package)

com tom directory under the directory to manage Java package provides classes, packages are designed to correspond to the structure of the file system, if the package is set to tom.com, then the class should be able to access Classpath to find the path. Package Manager is not set class will be classified as pre-package (Default Package), compiled class files must be placed in the path of Classpath.

In order to establish a file structure corresponding to the package, it can be added at compile time -d parameter, and the generated class file specified to be stored in a directory under which.

 

EX 9.4 PackageDemo.java

package tom.com;

public class JavaNotesChap0903_PackageDemo
{
    public static void main(String[] args)
    {
        System.out.println("Hello, Package!");
    }
}

Guess you like

Origin www.cnblogs.com/Tom-1103/p/12057842.html