Java coding some worthless syntax

xiaoxiao2021-04-07  378

1. Constants generally use capital, defined as static, such as Static Final Pi = 3.1415

2. Static method cannot access non-static methods or non-static member variables, and it can be.

3. Special Variable Super provides access to the parent class, you can use Super Access to variables hidden by subclass or subclass coverage; each subclass is constructed, it is implicitly called Supper (), so said, parent class The corresponding constructor must be provided; when the subclasses are instantiated, the construction method of the parent class is first modulated before the subclass is called. Since the subclass, the support () method is implicitly called.

4. Polymoritizes: Implementing the method of overwriting the parent class, invoking the corresponding method based on the reference to the delivery object at runtime. If the reference is passed is a reference to the subclass object, the method of the subclass is called. If the subclass does not have this method, the method of the parent class is called. If the delivery is a reference to the parent class object, the method of the parent class is called.

5. Access specifier of classes: (1) Public (2) DEFAULT (1) Final: Public Final Class Test {} - Do not want this class inheritance. (2) Abstract: Public Abstract Class Test {} - The class containing the abstract method is an abstract class. If a subclass does not implement all the abstract methods in the abstract base class, the subclass is also an abstract class, so if a class is a subclass of an abstract class, and the class is not an abstract class, you must implement all the abstract classes. Abstract method. Since there is no entity in the abstract method in the abstract class, it is impossible to determine its form, so abstract classes cannot be directly instantiated. We can also declare a class without any abstract method as Abstract to avoid any objects from this class.

6. Instructions for the method of the method (1) Public: All can access. (2) Protected: Different packages, and the class does not have access (if it is a subclass of the package can be accessed). (3) DEFAULT (when not adding an explanation): You can access it in the same bag. (4) Private: is limited to access in the same kind. Other modifiers (1) static: Static method belongs to class (2) Final: To ensure that behavior of a function remains unchanged during inheritance and cannot be overridden (Overridden), FINAL method can be used. All private and static methods in the Class are naturally final. (3) Abstract: The method of the method in the class is the abstraction method. Such as: public abstract void test (); (behind a semicolon); constructor, static method, private method, final method cannot be declared as an abstract method. (4) Native: Native method is the method that the user can use in Java, but cannot be written. JNI (Java Native Interface), which allows Java code within the Java Virtual Machine (JVM) to interoperate with applications and libraries written in other programming languages, such as C, C, C , assembly languages. The greatest advantage of JNi is that it does not impose any restrictions on the implementation of the underlying Java virtual machine, and therefore, Java virtual machine vendors can add support to JNI without affecting the other parts of the virtual machine. Programmakers only need to write a version of local (Native) applications and libraries to work with all JNI Java virtual machines. JNI can be understood as an intermediary between Java and local applications (5) Synchronized: Native method is a method that the user can use in Java, but cannot write. JNI (Java Native Interface), which allows Java code within the Java Virtual Machine (JVM) to interoperate with applications and libraries written in other programming languages, such as C, C, C , assembly languages. The greatest advantage of JNi is that it does not impose any restrictions on the implementation of the underlying Java virtual machine, and therefore, Java virtual machine vendors can add support to JNI without affecting the other parts of the virtual machine. Programmakers only need to write a version of local (Native) applications and libraries to work with all JNI Java virtual machines. JNI can be understood as an intermediary between Java and local applications. 7. The garbage collector garbage collector is a low-excellent line of thread and is automatically overlink in the background. When it only recovered garbage in memory, or the spam will be released when the program exits. You can print out the recycling object by the following method: portected void finalize () {system.out.println ("test");}, you can display the print garbage collection process by system.gc ();

8. Interface When a class implements an interface, the method in this type of interface must be public. Similarly, when a interface is declared, all methods under the interface must be a public abstract modifier, or the method does not know the modifier (default as: public abstract).

9. Internal class 1. Declining another class in a class, this class is called an internal class or built-in class (Inner Class). Internal classes allow us to organize a logically related set of classes and control the visibility of internal classes (Outer Class). When we establish an Inner Class, its object has a relationship between an external object, which is formed by a special THIS Reference, enabling internal class objects to access all members in the external class. 2. In an internal class, you can access the private member variable corresponding to its external class. 3. If the variable A is defined in an internal class, a variable A is also defined in the method of the internal class, and the outer class corresponding to the internal class also defines the variable A, the method in the internal class. In the order, the order is: a; this.a; Outer.This.a (Outer is the class name). Note: 1. If in another class, the first method is required to access the method with internal clashes and access the internal class: must do two points, first: define a returning internal class in a class with internal classes Method of objects. Second: When instantiating the object's object, use: Outer Outer = New Outer (); Outer.inner Inner = Outer.getinner (); GetInner () is an Outer class definition to return to an Outer class in the Outer class Object. Due to additional classes, it is hidden for an internal class, so you must use Outer.inner. Second method: Direct instantiation Outer Outer = new outer (); Outer.inner Inner = Outer.new Inner (); Second, when a class A is defined in a certain method C () When the range of use of this class is limited, it can only be accessed in the method C () of the class A. Class A, can also be placed in a method C () in a certain IF condition. So the definition of internal classes is very flexible, regardless of the nesting hierarchy of the internal class, class A can access all member variables of class B. Such as: Class name .this. Member variable name in the method C () defined in the method, if the parameters of the local variables or methods defined in the method are to be accessed, the variable must be declared FINAL (ie, declare constant). The variables in the c () method can be accessed by the internal class in the method. Third, the internal class can be declared as private or protected; can also be declared as Abstract or Final. Internal classes can be declared as static, but at this time, the non-Static member variable and non-Static member method can no longer be used; the members in the internal class of non-Static cannot be declared as static, only on top level or static Static members can be declared in the internal class. 4. Why do we use internal classes 1. Inner class (Inner Class), we can freely access the external class members, which allows us to organize our code and enhance the readability of the code. 2. Internal classes can be used to create an adapter class, and the adapter class is a class for implementing the interface. Use the internal class to implement the interface, which can better locate the location associated with the interface in the location of the code. 3, more usage of internal classes. (Defined to Private by internal class, implement detail hidden, through interface classes to implement internal PRIAVTE access) as follows:

Interface Animal {Void Eat (); Void Sleep ();} class zoo {private class tiger import () {system.out.println ("tiger eat");} public void sleep () {system. Out.println ("Tiger Sleep");}} Animal GetAnimal () {Return New Tiger ();}} Class Test {Public Static Void Main (String [] args) {zoo z = new zoo (); animal an = Z.GetAnimal (); An.eat (); An.Sleep ();}} In short, the benefits of internal classes do not stop these, truly participate in project practical applications, you will find the practical value of internal classes. Another example is, a class has achieved multiple inheritance through an internal class.

Class A {Void Fn1 () {}} Abstract Class B {Abstract Void Fn2 ();} Class C Extends A {B Getb () {Return New B () {Public Void Fn2 () {}};}} Class Test {Static void method1 (a) {a.fn1 ();} static void method2 (b) {b.fn2 ();} public static void main (string [] args) {c = new c (); Method1 (C); Method2 (C.Getb ());}}

5. When a class A inherits the internal class C of another class B, you must first instantiate B. Below

Class B

{

Class C

{

}

}

Class a extends b.c

{

A (B b)

{

B. Super (); //

Create a reference relationship from internal class objects to the specified external class object}

Public static void main (string [] args)

{

B b = new b ();

A a = new a (b);

}

}

转载请注明原文地址:https://www.9cbs.com/read-132549.html

New Post(0)