1. Interface
1.1 relationship between interfaces
There is inheritance relationship between classes (single inheritance), implementation relationship between classes and interfaces (multiple implementation implementations), and inheritance relationship between interfaces. Multiple inheritance is supported. An interface can inherit multiple interfaces at the same time interface A extends B,C,D{}
1.2 implementation class or abstract class
The implementation class implements the interface and rewrites some abstract methods. The implementation class is still an abstract class
public interface A { public abstract void a1(); public abstract void a2(); }
public abstract class B implements A { public void a1(){ } // public abstract void a2(); }
1.3 interface rule cases
/** * Notebook computer */ public class Computer { /** * Notebook computer, using external devices through USB interface * Method definition to realize the use of equipment * Return value type: the data type of the result of the operation of this method * Parameters: in fact, the parameters of the method are unknown data in the process of method operation * * Notebook computers use external devices through USB interface. The unknown data of this function is external devices * Core: unknown devices, all of which have common characteristics: meet interface rules * * Question: what does this method call pass * The parameter is a reference class. To pass the object, pass the interface to implement the class object */ public void useUSB(USB usb){ // USB usb = new Mouse(); //Interface reference calling method usb.start(); usb.end(); } }
/** * Keyboard class: meet interface standards */ public class KeyBoard implements USB{ @Override public void start() { System.out.println("Keyboard work"); } @Override public void end() { System.out.println("The keyboard stops working"); }
/** * Mouse class: access USB interface to work * Meet the rules of the interface: implement the interface in the program */ public class Mouse implements USB{ @Override public void start() { System.out.println("Mouse on switch,Key,Roller"); } @Override public void end() { System.out.println("The switch is off"); } }
/** * Defined USB interface: rule assignment * Rules of interfaces: abstract methods in programs */ public interface USB { //The equipment starts working public abstract void start(); //End of equipment public abstract void end(); }
public class USBTest { public static void main(String[] args) { //Test notebook case //Create notebook object Computer computer = new Computer(); //Call the method of the notebook /* Mouse m = new Mouse(); computer.useUSB(m);*/ computer.useUSB( new Mouse() ); //Call the method of notebook and pass the keyboard object (USB interface implementation class object) computer.useUSB( new KeyBoard()); } }
2. Static modifier
2.1 statically decorated member variables
Static modified member variable is the data shared by all objects Member variables not modified by static are exclusive data or unique data of each object
public class Person { String name; static String country = "China"; }
public class StaticTest { public static void main(String[] args) { Person p1 = new Person(); p1.name = "Zhang San"; Person p2 = new Person(); p2.name = "Li Si"; //Use the object p1 to modify the value of the variable country p1.country = "U.S.A"; System.out.println(p2.country); } }
2.2 static memory
- Characteristics of static member memory
- Static members follow their classes into the metadata area (static area)
- Static members belong to their own classes, not objects
- After a static member enters memory, it is assigned a default value
- Static member variables are actually initialized earlier than objects
2.3 calling method of static member
Static belongs to its own class, not an object. The static calling method should be the class name Static member
Person.country ;//Call static member
public static void main(String[] args) { System.out.println(Person.country); Person p1 = new Person(); p1.name = "Zhang San"; Person p2 = new Person(); p2.name = "Li Si"; //Use the object p1 to modify the value of the variable country Person.country = "U.S.A"; System.out.println(Person.country); }
There are two static invocation methods: class name invocation and object invocation Non static members can only call objects
The static member invocation method has only the class name Non static members can only be objects Object Static call mode, which will be compiled by javac as a class name call
2.4 static method
Static methods are called directly from the class name Non - static members cannot be used directly in static methods
Why can't I call a non static member
Static is the ancestor, non - static is the descendant Static appears in memory earlier than non static
public class Person { String name; static String country = "China"; /** * Static method */ public static void eat(){ System.out.println("People are eating" + country); System.out.println(name); //Wrong writing, not allowed } }
Static memory takes precedence over objects. this and super cannot be used in static methods
2.5 main method
public static void main(String[] args){ }
- main method details
- public maximum permission: the caller of the main method is the JVM
- static needs no object and is called directly by the class name. The class name is used when the JVM is started main startup program
- void has no return value, the caller is the JVM, and the return values of methods are returned to the caller. The JVM does not need a return value, which is meaningless
- Mainfixed method name
- Array of args string. The JVM must pass parameters when calling the method main. Later, set parameters for the JVM
2.6 when to define static
- Static member variable: specific function specific analysis
- When you need to define a class, analyze whether the new object of this class has shared data. If there is shared data, it should be defined as a static variable
- Static member method:
- When a method in your class has not used a non - static member, it should be defined as static
3. Four authorities
Four permissions public protected default private
private | default | protected | public | |
---|---|---|---|---|
Same class | OK | OK | OK | OK |
Same package | NO | OK | OK | OK |
Different steamed stuffed bun classes | NO | NO | OK | OK |
Non subclasses of different packages | NO | NO | NO | OK |
- Protected permissions protected
- A member of permission that provides access to subclasses
- The Object class is the parent class of all classes. The method permissions in the class are public and protected
- The key way is that the method of protected permission can only be super call in subclass!!
- Subclass objects cannot be called, and subclasses cannot be generated
public class Person { protected void eat(){ System.out.println("Humans are eating"); } }
public class student extends Person { void show(){ Person p = new Person(); super.eat(); //super call in subclass!! } }
public class Test { public static void main(String[] args) { student s = new student(); s.show(); } }
4. final modifier
The final modifier is the final meaning and cannot be changed Final can modify classes, methods, member variables and local variables
4.1 final modifier class
The class modified by final is called the final class. It cannot be inherited by other classes and has no subclasses
public final class A{} //This class A cannot have subclasses. If it inherits, an error will be reported directly public class B extends A{} //Error, compilation error, final class cannot inherit
4.2 final modification method
The method modified by final, the final method, cannot be overridden by subclasses and has nothing to do with calls
Some methods in one class are perfect, but others need to be completed and designed into two parts The perfect way is final
public class A{ public final void a(){} //Methods cannot be overridden by subclasses } public class B extends A{ public void a(){} //The final method cannot be overridden }
4.3 final modifier local variable
The variable is defined inside the method and is a local variable. After being modified by final, it is assigned once and remains unchanged for life. The value of the variable is locked and regarded as a constant
- Basic type of final modifier, lock value
- The reference type modified by final locks the memory address (the members in the reference type are not affected)
- final modifies the parameters of the method. After the caller passes the value, the parameter value of the method will be locked
public static void main(String[] args) { /** * Student student Object stores a memory address * final After modification, the fixed and unchangeable address is the address saved by the student variable * However, members in the Student object are not affected */ final Student student = new Student(); student.age = 20; student.age = 30; System.out.println(student.age); final int[] arr = {1,2,3};//The value of the arr variable is fixed to the memory address and cannot be changed arr[1] = 200; show(5); } public static void show(final int x){ x = 6; //final modification, unalterable, error reported } }
4.4 final modifier member variable
The definition position of member variable is in the class and outside the method Member variables have default values in memory When final modifies the member variable, the lock is not the memory default value, but the manual assignment of our programmers
- Member variable assignment, you can define the direct write value int age = 0;
- For member variable assignment, you can use the construction method public Student(int age){this.age=age;}
- The assignment of member variables can be completed by using the set method
- The member variable modified by final can construct method assignment, but cannot set method assignment
- The constructor executes the new object once and only once
- The set method can be executed repeatedly!!
public class Student { final int age ; public Student (int age ){ this.age = age; } /*public void setAge(int age){ this.age = age; }*/ }
5. Code block
5.1 static code block
Write outside the method in the class: static {}
Execution timing of static code block: as long as the members of this class (new object, calling static method and static variable) are used, the static code block will be executed once
5.2 building blocks
The {} written outside the method in the class is run when the object is created. new is run once and run once
5.3 local code block
The {} local code block written inside the method is useless
public class code { { System.out.println("This is the construction code block"); } static { System.out.println("This is a static block of code that is executed only once"); } }
public class codeTest { public static void main(String[] args) { new code(); new code(); new code(); } }
6. Object initialization process (subclass and parent)
- Parent class Class file enters memory first
- Subclass class file enters memory again
- Initialize the static members of the parent class (variables, code blocks, methods)
- Initializing static members of subclasses
- Run the static code block of the parent class
- A static block of code that runs a subclass
- Run the constructor code block of the parent class
- Run the constructor of the parent class
- Construct code block for running subclasses
- Construction method of running subclass
public class FU { static { //1 System.out.println("Parent static code block"); } { //3 System.out.println("Parent class construction code block"); } public FU() { //4 System.out.println("Parent class constructor"); } }
public class ZI extends FU{ static { //2 System.out.println("Subclass static code block"); } { //5 System.out.println("Subclass construction code block"); } public ZI() { //6 System.out.println("Subclass constructor"); } }
public class test { public static void main(String[] args) { new ZI(); } }
7. Internal class
Overview: the so-called internal class means that another class is defined inside a class
class A{ //External class class B{} //Nested class }
Objects are things that exist in life, and there is another concrete thing in one thing
class multi-storied building{ class Elevator.{} }
7.1 member internal class
Member inner class is a class defined in the member position of another class This inner class can use the member modifier, public static final private
For the internal: you can use the members of the external class directly. If the external class wants to use the members of the internal class, you must create an object
//Formulas: external class names Internal class name = new external class object () New inner class object ()
//External class public class Outer { public void outer(){ System.out.println("Methods of external classes outer"); } //Inner class public class Inner{ public void inner(){ System.out.println("Methods of inner classes inner"); } } }
public static void main(String[] args) { //Call the inner() method of the inner class Outer.Inner oi = new Outer().new Inner(); oi.inner(); }
Question: is there a class file after compiling the internal class Inner? There is a class file with the name of external class $internal class
Internal classes are also classes that inherit Object and implement interfaces
7.2 use of static internal classes
The inner class is called statically: the outer class name Internal class name variable name = new external class Inner class ()
public class Outer { public static class Inner{ public void inner(){ System.out.println("inner Static method in class"); } } }
public class test { public static void main(String[] args) { Outer.Inner oi = new Outer.Inner(); oi.inner(); } }
7.3 local internal class
Local inner class: to be defined in the method Method is a local location. Member modifiers and permissions cannot be used. Static cannot be used
class A{ public void a(){ class B{} //Local inner class } }
public class Outer { /** * Inner Class, which is a part of the method Outer * We can only be accessed by the outside world if we still use the method */ public void outer(){ class Inner{ public void inner(){ System.out.println("Methods of local inner classes!!"); } } //Method to create an object Inner inner = new Inner(); inner.inner(); } }
public static void main(String[] args) { //Call the inner() method of the inner class //Call directly, cannot call Outer outer = new Outer(); outer.outer(); }
Local internal class, access to local variables, variables must be final modified
7.4 method internal class external class variable has the same name
public class Outer { int x= 5; public class Inner{ int x=4; public void inner(){ int x=3; System.out.println("Inner class method"+x); System.out.println("Inner class method"+this.x); System.out.println("Inner class method"+Outer.this.x); } } }
public class test { public static void main(String[] args) { Outer.Inner oi = new Outer().new Inner(); oi.inner(); } }
7.3 anonymous inner class
Anonymous inner classes are unnamed inner classes that can only be written in methods to simplify code writing
Simplification: implement classes, implement interfaces, rewrite methods, and create objects Or subclasses inherit the parent class, override methods, and create objects There is little content in the code
-
Prerequisites for using anonymous inner classes:
-
There must be an interface implementation or class inheritance
-
Format:
new Interface or parent class(){ //Override abstract methods }; format == Implementation class,Implementation interface,override method ,create object
public interface MyInter { public abstract void inter(); public abstract void inter2(); }
public class InnerClassTest { public static void main(String[] args) { //Anonymous internal class, simplified writing, no implementation class //Call multiple override methods at the same time /* * new MyInter(){}; Is an anonymous object of an interface implementation class * Polymorphism: interface variable = implementation class object */ MyInter my = new MyInter(){ @Override public void inter() { System.out.println("Implementation class implementation interface rewriting method"); } @Override public void inter2() { System.out.println("Implementation class implementation interface rewriting method 2222"); } }; my.inter(); my.inter2(); } }
-
7.3 examples
public class hide implements hideclass{ public void show() { System.out.println("Anonymous inner class 1"); } public void showb() { System.out.println("Anonymous inner class 2"); } }
public interface hideclass { public abstract void show(); public abstract void showb(); }
public class test { public static void main(String[] args) { //1. The first method hide h = new hide(); h.show(); //2. Call a single anonymous inner class method new hideclass(){ public void show() { System.out.println("Anonymous Inner Class "); } }.show(); //3. Call multiple anonymous inner class methods hideclass h = new hideclass(){ public void show() { System.out.println("Anonymous inner class 1"); } public void showb() { System.out.println("Anonymous inner class 1"); } }; h.show(); h.showb(); } }
8. Illegal modifier combination
Illegal modifier combination mainly refers to abstract abstract
- abstract and private are illegal combinations. abstract methods need to be rewritten and private cannot inherit
- abstract and final are illegal combinations. abstract methods need to be rewritten, and final modifications cannot be rewritten
- abstract and static are illegal combinations. Static method class names are called directly