Java Basics Small Summary (2)

Source: Internet
Author: User

DAY07:

1. If local variables and global variables have the same data type and variable names, first call local variables to implement the nearest principle;

2. The anonymous object is usually called only once for reasons of limitation, and is only used when the actual parameter is passed;

3. Three basic features of object-oriented language: encapsulation, inheritance, polymorphism

4. Encapsulation Benefits: Security, convenience, with private (can only be accessed in this category) embodied;

5. The constructor is for display initialization, and no return value type

6. In the constructor function can call the general function, but in the general function can not call the constructor;

7. Details of the constructor definition:

1. The constructor is used to create the object, and it does not need to write the return value type.

2. The name of the constructor must be the same as the current class name. Therefore, the name of the constructor is written in accordance with the class name.

3, the parameter list, can be the same as the general function of the parameter list.

8. The function of the constructor: it is used to initialize the object. (Just to initialize values for non-static member variables).

9. In the new class, the compiler does not automatically add an parameterless construct as long as the constructor is written. So the parameter list must be the same, or error. If there is no write constructor, then the compiler will automatically add an parameterless constructor at compile time (this method is called the default parameterless constructor);

10. The construction method can exist in the same class in an overloaded manner;

11. The similarities and differences between constructors and general functions

1, their execution time is different:

The constructor is executed during the creation of the object. When the object creation is complete, the constructor finishes executing.

There are two scenarios for general function execution time:

1) If you call a function in another class: The General function is invoked through a reference to the object after the object is created.

2) If you call a function in this class: When to use and when to invoke.

2. They have different invocation times:

The constructor is called only when the new object is created, and we cannot manually invoke the constructor as soon as the object creation is complete.

The general function can be arbitrarily called by the object, with no limit to the number of times.

3. They call each other questions:

You can call a general function in a constructor, but you cannot call a constructor in a general function.

(The difference between a constructor and a general function:

1. The constructor does not return a value type, and the general function has a return value type;

2. Constructors are used only when new objects are created, because new ones exist, disappear with the end of the new, cannot be arbitrarily called, and the general function can exist in the same class or non-homogeneous, the number of calls is unlimited, if defined in the same class can be directly called by the function name, if defined in different classes, The call needs to be called by the object name, and with the invocation, it disappears with the end of the method;

3. Call to each other: constructors can call general functions, and general functions cannot call constructors;)

How do I use this to invoke other constructors?

This  call the format of the other constructor: this ( parameter list); equivalent to a constructor ( parameter list)

in the Java whenever a function is called by name, it is called a generic function, and the constructor cannot be called from the function name.

The issue to note when invoking the constructor with this:

1. Constructors cannot be nested within each other, resulting in an unrestricted call to the constructor, which can never stop the call.

2. This invokes the constructor, which must be placed in the first sentence of the constructor. The purpose of calling the constructor through this is that we want to complete the initialization action through the other constructors, so that the initialization of the other constructors must be initialized before the statement executes in this constructor.

The second function of 13.this: When constructing a method or invoking a method in a class through an object name, there is a hidden this variable in the statement, and this is the one that saves the name of the new address in the heap memory, so it can access the value of the member variable in the heap memory after new.

The third function of 14.this is to distinguish between member variables and local variables, who call, this represents who;

To use this, there must be an object, because this remembers the address name that the object produces in heap memory

DAY08:

1. Each class has a default parameterless constructor;

2. Execute the process on the new object class, executing the static code block (which will be used later, mainly to initialize the static member variable, and the static code block will only be executed once), if more than one is executed from top to bottom, and there is an implicit three-step (1) when calling the constructor. Super (), 2. Initializes the member variables, 3. Executes the construction code block, and if there are more than one construction code block, it is executed sequentially in the upper and lower order;

3. Static functions cannot have non-static member variables and non-static member functions, while non-static member functions can have static member variables and static functions;

4.Static decorated member variables and member functions can be called in other classes through the class name and object name, regardless of the object, mainly related to the class, as the class is loaded and executed and loaded, but not static and non-static member variables in other classes can only be called through the object;

5. The difference between a static member variable and a non-static member variable:

A. The time of creation is different, static will load with the load of the class, but not static will be loaded with the creation of the object;

B. In the memory location is different, static will be in the class Load method area of the static zone load, and not static will be in the creation of the object in the heap memory load;

C. The vanishing time in memory is different, static only disappears when the JVM exits, and non-static disappears as the object disappears;

D. The initialization time is different, the static variable will be loaded with the class loading, the assignment is initialized at that time, and the non-static is initialized with the creation of the object.

6. When the class is loaded, the JVM loads all the static member variables in the class first, and when all static member variables are loaded, the default initialization of all static member variables in the class is initiated, and when all static member variables in the class are initialized by default, The assignment to display all static member variables is then started. Static code blocks run only after all static member variables in the class show that the assignment ends.

Summary:

1. Static code block executes as the class is loaded and executes only once

2, Static code block function: to initialize the class.

7. Implicit three-step of the constructor: A. Super (); B. member variables display initialization; C. Building code block execution

8. Loading process for classes:

A. First load the class file in the method area into the non-static zone, and then load the static member variable, static member method and static code block into the static zone;

B. After all static loads are completed, the static member variables are initialized by default, and then the display is initialized;

C. Static member variables show that initialization is complete before you run a static block of code;

D. All static code blocks are loaded to complete the entire class;

To create an object's loading process:

A. Creating an object using the keyword new will open up space in the heap memory and randomly assign the address name;

B. In the open space, all non-static member variables are loaded into the heap memory allocation space for default initialization;

C. The matching constructor is called when loading is complete: (implicit three-step) 1.super (); 2. All non-static member variables are displayed initialized, 3. Run the construction code block, and finally other code that runs the constructor;

D. Execution of the constructor completes, object creation is complete;

9. Single Case design mode:

A. In order to create only one object, all constructors must be privatized to allow other classes to be called;

B. When the constructor is privatized, the object can only be created in-house;

C. An external function implementation is required to return the created object, but since other classes cannot create new objects, the calling function cannot be called through the object name, only the class name, so the function must be static, and the return object should be static;

D. This allows the function to be called by the class name to return the object created by the caller;

E. However, the creation of the above process also has a vulnerability, that is, the expression of the creation of a new object has not been privatized, although static decoration, but still can be called through the class name, so that does not meet the requirements, so the creation of the new object of the expression must be privatized, so that the external access is not possible to create new objects

F. Perfect implementation of a single case design mode

DAY09:

1.static decorated functions can not have this,super, because this is to remember the address name of the object, and the object, super is called the parent class constructor, the constructor is also related to the object, so can not;

The difference between 2.this and super:

1) This represents a member of this class, and super represents a member in the parent class.

2) in Java, this is a reference variable that holds the memory address of the object that is currently calling this function.

Super simply represents the memory space of the parent class. But not an object of the parent class. Super is just a token that marks which is the parent class.

3) This represents the entire space that the subclass object opens up in the heap, and the super tag is the space occupied by the parent class in the subspace where the subclass object is opened.

Note: In development, member variables are usually defined in the parent class, and subclasses are used directly after inheritance, and no new member variables need to be defined again.

3. Features of the method (function):

function overloading (overload): occurs in the same class

The method names are the same and the argument lists are different. is not related to the return value

function override (Override): Occurs in a child parent class

Method name is the same, parameter list is the same, return value type is the same (method in child parent class is identical)

4. Subclasses when a function of a parent class is replicated, the subclass's method access permission must be greater than or equal to the parent class's permissions. member variables and member functions that are private decorated in the parent class cannot be called and overridden;

5. The final modified variable is compiled as a constant, and the variable name must be capitalized to indicate a distinction;

6. The method of the abstract function, the subclass must replicate all the abstract functions, and the abstract function must be modified with an abstract.

7. Once a class has an abstract function, the class becomes an abstract class. The same class name also requires an abstract modification, with the emphasis that the abstract function cannot have a method body;

8. The default modifier for the interface is public abstract, so the modifier of the overriding interface's implementation function must be public

9.final Summary:

1) A final modified member variable, compiled into a constant, cannot modify its value;

2) A member function that is final modified, cannot be overridden by a quilt class method;

3) The class that is final modified cannot inherit the quilt class;

4) Final It can be decorated with a class that modifies a member (member variable, member function), and modifies a local variable. constructors cannot be modified;

10. Abstract classes are inherited abstract functions must be replicated, the abstract function of the class is the necessary abstract class, but abstract classes do not necessarily have abstract functions;

11. Subclasses want to call the parent class function, you must write super in the body of the sub-class function of the replication. function name ();

12. The abstract class must have a subclass, but if there is no subclass compilation will not error, there is also a parent class, at least the object class, the abstract class cannot be created objects, cannot be instantiated, must have a subclass implementation, abstract functions must be replicated, if the abstract class can create objects, then this object can call abstract functions, But the abstract function has no method body and has no meaning;

13. The default modifier for the member variable in the interface is public static final, and the variable name is capitalized; the name of the interface is used to invoke. Variable names;

DAY10:

1. There can be only member variables and member functions in the interface

2. Invoking a member variable in an interface in a class that implements an interface can directly write the variable name, which is a somewhat inherited relationship; but the test class wants to use the interface name. variable name call;

3. If the function in the interface does not rewrite will be an error? The interface must be replicated for all functions in the interface.

4. If a class implements more than one interface, and the same function is in multiple interfaces, the same function can be replicated only once, because the function is not a function body, so there is no uncertainty;

5. If a class implements more than one interface, and the same member variable is in multiple interfaces, it must be called the interface name. Variable name, or compile error, because there is uncertainty;

6. Knock code: Adapter code, the idea of design mode is to define an abstract class, the replication interface of all methods, but the function body is empty, so it is more convenient to call and make a copy. But the adapter has to be abstract, because it is not abstract and meaningless;

7. Why are adapters abstract classes? Because it is not an abstract function can create sub-class object, but the function in the adapter is empty function body, even if the object is created, call function does not have any meaning; in fact, abstract can not write, but rookie;

8. Can the return value of an abstract function be void only? No, it can be other types, see the specific needs.

9. Can the polymorphism occur in the interface? An interface is instantiated by an object implementation, which can be used (the interface name Object name = new Implementation class name ()) so that the object name can call functions in the implementation class, similar to polymorphism;

/*

This uses the interface to pass parameters, with the implementation of the class object to do the argument, also occurs polymorphic, called the implementation of the class object function; So polymorphism can also occur in the interface

*/

9. Abstract classes can also be polymorphic, and the rules for compiling and running are the same as for general classes;

10. Using a polymorphic scenario: If the parent class has more member functions and the subclass wants to invoke it, the method body that calls the subclass name can be raised, and the argument is the parent class type. Other subclasses ' unique functions can be judged using the instanceof keyword and then transformed downward using a forced transformation;

11. Multi-State Malpractice Summary:

When using polymorphic techniques, the program uses polymorphic call members (variables and functions) at compile time, requiring the called member to be present in the parent class and failing if no compilation is in the parent class. (You cannot use subclass-specific features or attributes)

Note: As long as there are polymorphic places, there must be an elevation of the type (it is certainly the object of the class using the parent class type in the representation).

1) When to use downward transformation:

The downward transformation is used only when we need to use the unique properties or behaviors (methods, functions) of subclasses in the program.

2) Whether it's up or down, eventually, the subclass object does the type change. is not related to the parent class object.

Summary:

As long as there is polymorphism, there is a type of conversion.

When a reference to a parent class is assigned to a class object, an upward transformation (implicit type conversion) occurs.

If we need to use the specific behavior or attributes of a subclass, then we have to go down and we need to turn the reference of the parent class into the type of the object that it refers to.

in the downward transformation must make the type of judgment, to prevent classcastexception the occurrence of an exception.

Judging format:

if ( parent class Reference variable name instanceof class name to which the subclass object belongs )

{

To convert.

}

Summarize the rules of member usage in polymorphic states: member variables and static member functions, and compile and run all on the left (parent class). Only non-static member functions, compiled to look at the parent class, run the look sub-class object.

Individuals summarize the differences between abstract classes and interfaces:

A. The interface can only write member variables and functions, and functions are abstract functions, the default modifier is public abstract , the default modifier for a variable is public static final that is, the variable becomes a constant, it must have an initialization value, the abstract class can write member variables and member functions (including static and non-static), there are constructors (in order to initialize the subclass variables but cannot create objects), can have static code blocks, member functions can be abstract or non-abstract, Abstract functions are not necessarily necessary;

B. The interface must be implemented, and the implementation class must override the function in the interface (note the function modifier's permission size problem); Abstract functions do not have to inherit, can not be inherited, but once inherited, the inheriting class must rewrite the abstract function; (Note: Once an abstract function, the class must be an abstract class)

c. an interface is an extra function that represents something, and an abstract function in an abstract class means that the behavior in a class cannot be fully described, so it is written as an abstract function;

d. the implementation of the interface is implements with the keyword abstract class is inherited by the keyword extends;

E. interfaces can be implemented more than one another, and abstract classes and classes can only be inherited;

F. abstract class keyword not and private , Static , Final Cosmetic Fu Jiancun

 

Day11:

1. member Inner class:

Public decoration:

A. Creating an object can be created in an external class, Inner in = new Inner (), (hidden meaning: Outer.Inner in = new Outer.Inner ()), and then placed in a function of an outer class so that an object of the outer class is created in the other class, in the calling function, You can invoke the function of the inner class.

B. Creating an inner class object can be in another class, so that an inner class can be created through an external class object invocation: Outer o = new Outer (); Outer.Inner in = O.new Inner (); Change to one line of code: Outer.Inner in = new Outer (). New Inner ();

If we want to invoke the generic function (non-static function) in the inner class in other classes, we have to invoke it using an object of the inner class, so we create the object of the inner class, creating the way:

New Inner class class name ();--------->new Outer.Inner ();

Static modification:

If you want to invoke a static method in a static inner class in another class, we can first find the class name of the static inner class, and then according to the class name

Other classes invoke static functions and variables in the static inner class, as shown in the following: Outer.Inner.method ();

static functions and variables in a static inner class are called in the outer class, Inner.method ();

Note: The intrinsic class functions and variables at this time are modified with static

Private decoration:

When a member of a class is private, it can only be accessed in this class.

Note: The inner class of a private member can only be created in other methods of this class, and then the outside world calls this member method of the outer class, which indirectly allows the methods in the inner class to run.

2. Local Inner class:

Note: Local inner classes cannot be decorated with member modifiers.

Note: The local inner class can only be accessed in the function where the local inner class resides, out of which the function cannot be accessed.

When the inner class is defined locally, only the final decorated local variables can be accessed. The error is reported if the final decoration is not.

3. Anonymous inner class:

Format:

New parent class or interface () {

A function in a replication parent class or implementation interface

};

Description

1) Note that there is an English semicolon behind the curly braces and cannot be omitted.

2) Anonymous internal class format is fixed, later in the development encountered in the above format, to know that the anonymous internal class format can be.

3) The anonymous inner class can only be created in the local location of the outer class, and if you want to invoke multiple functions within the anonymous inner class, you may assign the anonymous inner class to the parent class or interface object. The function is then called with the object name.

That is: Parent class/Interface Variable name = new parent class/interface () {

To replicate or implement functions in the parent class, interface

};

Variable name. function name 1;

Variable name. function Name 2;

4. Summary:

1. Under what circumstances are anonymous internal classes used?

If we define a class that implements an interface or inherits a parent class, just for the purpose of copying the parent class or implementing the methods in the interface, the class that we specifically define is a bit redundant, and the anonymous inner class provided in Java can be used to complete it.

2. The anonymous inner class is an implementation class or subclass object of an interface or parent class.

Java Basics Small Summary (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.