Packages, classes, access rights (note 14)

Packages, classes, access rights

One, package

If you execute a class with a package name, you need java package name. class name (cmd)
1) Package declaration
  package 包名;  =>package lession1;
2) Guide package //To refer to classes that are not in the same package
  import package name
3) The classes in the same source file are all under the same package, refer to each other, and do not need to import the package
4) Between aaa.bbb.ccc and aaa.bbb, we can think of it as two completely different packages without any affiliation
  //Please refer to the example in the video
  import aaa.A;
  import aaa.bbb.B;
  import aaa.bbb.ccc.C;

public class Teacher {
public static void main(String[] args) {
A a=new A();
B b=new B();
C c=new C();
}
}
5) If a class is declared with a package name, it must be placed in the folder hierarchy corresponding to the package name
         And the compiled bytecode files should also be placed in such a folder hierarchy     
6) If you want to generate the corresponding folder hierarchy when compiling, you can use 
         javac -d . Cat.java      
7) If it is found that the class cannot be found during development in the future, there may be several reasons:
         class name is wrong
         package name is wrong
         classpath is set wrong   
8) The classes under the java.lang package do not need to be packaged when used

二、classpath

The previously learned path represents the path where the executable tool is located
  classpath represents the path where the class to be executed is located (classpath)
  
  Command to set classpath:
  set classpath= d:/;c:/xxx;d:/java/bin;
  
  The search rule of the path we learned before is: first look in the current directory, if you can't find it, then go to the path specified by path to find it
  classpath It is first found in the path we specified, if it is not found, is it in the current directory? It is hard to say
  
  set classpath=E:/ //Indicates to find under E:, even if you can't find it
  set classpath=E:/; //Indicates to find under E:, if not found, then go to the current directory to find
  
  If you explicitly want to specify that you want to find in the current directory, you can use . to indicate
  set classpath=.;E:/
 
  Note: Do not have spaces before and after the equal sign, do not use Chinese
  
  View classpath => set classpath in command prompt 

3. Access rights

To access classes in different packages, the accessed class must be declared as public type, and the class cannot be modified by private or protected
-- public The classes, properties, and methods modified by it can be accessed not only across classes, but also across packages
-- private can modify data members, constructors, methods, but cannot modify classes, and the members modified by it can only be accessed by the class itself, not by subclasses
-- protected can modify data members, constructors, methods, but cannot modify classes, and can be accessed by members of this class, the same package, or its subclasses (if it is a subclass, it can cross packages)
-- default without any modifiers, only allow access in the same package

//Example in the case of the same package
class Dog {
public void publicMethod(){
System.out.println("publicMethod");
}
protected void protectedMethod(){
System.out.println("protectedMethod");
}
void defaultMethod(){
System.out.println("defaultMethod");
}
private void privateMethod(){
System.out.println("privateMethod");
}
}

//Note that it is in the same package
class T{
public static void main(String[] args) {
Dog dog=new Dog();
dog.publicMethod();
dog.protectedMethod();
dog.defaultMethod();
// dog.privateMethod(); // not allowed
}
}

//Example 2 is not the case of the same package
package cat;
import dog.Dog;
class Cat {
void test(){
  Dog dog=new Dog(); //Dog and cat are in different packages, Dog must be declared as public
  dog.publicMethod();
// dog.protectedMethod(); //No, protected cannot be accessed across packages
// dog. defaultMethod(); // not allowed
// dog.private(); // even more impossible
}
}

//Inherited from Dog, and not the same package
class NiceDog extends Dog{
void test(){
Dog dog=new Dog(); 
dog.publicMethod();
protectedMethod(); //Yes
//defaultMethod(); not allowed
//dog.protectedMethod(); No
//dog.defaultMethod(); No
//dog.private(); not allowed
}
}

Fourth, the jar package

The jar file in java is loaded with .class file
It is a compression, compatible with zip
called jar package
Many classes, compressing these classes and their directories into a file for others, looks professional and not scattered
After someone gets the jar package, as long as the jar file is included in the setting of his classpath, the java virtual machine will automatically decompress the jar file when loading the class.
Treat it as a directory, and then look for the class we want and the package name of the class and the structure of the corresponding directory in the directory
Many classes provided by jdk are also provided in the form of jars

The command used to open the jar package is
=> jar -cvf myutil.jar cn
-c create a new document
-v means to output some detailed information
-f specifies the filename
cn refers to the top-level directory where the .class file is located

Five, finalize method

It is a method in Object class
It is called when the object is collected by the garbage collector 
  
public class Test extends Object {
public static void main(String[] args) {
Student stu=new Student();
stu=null;
System.gc(); //Arouse the garbage collector to work, then it will output the sentence "Ah... I'm game over"
System.out.println("over");
}
}

class Student{
int age;
protected void finalize() throws Throwable {
System.out.println("Ah... I'm game over");
}
}

6. Other records

1. Please state the difference between the scope of public, private, protected, and when it is not written

private : can only be used in this class, not out of
class
. Classes and subclasses of the package can also be called directly, but they cannot be called directly on the object. Instead, they can be called directly after inheriting the method - for example: protectedMethod();

2. Can an interface inherit an interface? Can an abstract class implement an interface? Can an abstract class inherit a concrete class? 
   Is it possible to have a static main method in an abstract class?
Answer: An interface can inherit from an interface. Abstract classes can implement interfaces, and abstract classes can inherit ordinary classes. An abstract class can have a static main method.
Remarks: As long as you understand the nature and function of interfaces and abstract classes, these questions are easy to answer. Think about whether you would provide such support if you were the designer of the java language.
If not, is there any reason? If you have no reason not to provide it, then the answer is yes.
  //Just remember that the only difference between an abstract class and a normal class is that you cannot create instance objects and allow abstract methods.

3. What is GC? Why is there GC?
   GC means garbage collection (Gabage Collection)
   Forgotten or wrong memory reclamation can lead to program or system instability or even crash
   The GC function provided by Java can automatically monitor whether the object exceeds the scope to achieve the purpose of automatically reclaiming memory.
   The Java language does not provide an explicit operation method for freeing allocated memory.
   
4. What is the basic principle of the garbage collector? Can the garbage collector reclaim memory right away? Is there any way to proactively notify the virtual machine for garbage collection? 
     For the GC, when the object is created, the GC starts to monitor the address, size and usage of the object.
     Generally, GC records and manages all objects in the heap in a directed graph manner. Determine which objects are "reachable" in this way,
     Which objects are "unreachable". When the GC determines that some objects are "unreachable", it is the responsibility of the GC to reclaim the memory space.
     Can. Programmers can manually execute System.gc() to notify the GC to run, but the Java language specification does not guarantee that the GC will be executed. 
     
5. Is there any error in the execution of the following program? 
class T {
    final int i; // wrong here, it should be initialized (or initialized in the constructor)
    public void test() {
        System.out.println("i = " + i);
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325560878&siteId=291194637