Java essentials: Object-oriented,

Source: Internet
Author: User

Java has 5 pieces of memory.

1: Register. 2: Local method area. 3: Method area. 4: Stack. 5: Heap.

Stacks : Stores are local variables (variables defined in functions, arguments on functions, variables in statements), and the data is freed as long as the area where the data operation is completed ends.

heap : Used to store arrays and objects , that is, entities. What is an entity? is used to encapsulate multiple data.

1): Each entity has a memory header address value .

2): Variables in heap memory have default initialization values. Because the data types are different, the values are not the same.

3): garbage collection mechanism.

----------------------------------------------------------------------------------------------

One: Object-oriented:

Features: 1: Simplify complex things.

2: Object-oriented converts the performer in the previous process into a conductor.

3: Object-oriented This idea is in line with the current thinking habits of people.

How do processes and objects manifest in our programs? The process is actually a function, and the object is to encapsulate some content such as functions.

Anonymous object Usage Scenarios:

1: Anonymous objects can be used when the other method only makes one call.

2: You cannot use an anonymous object when an object makes multiple calls to a member. The object must be given a name.

The definition in a class is actually called a member. There are two types of members:

1: member variables : In fact, the corresponding is the property of the thing.

2: member function : In fact, the corresponding is the behavior of things.

So, in fact, defining a class is defining member variables and member functions. But before the definition, it is necessary to analyze the property and behavior of things before they can be embodied in code.

Private int age;//has the lowest access rights, and only access in this class is valid .

Note: Private is only a form of embodiment of encapsulation.

Private members : Other classes can not directly create object access, so only through this class to provide specific access to the private access to complete the use of a function to provide access to it in the form of external.

Benefits : You can add logic judgment and other operations to the function, judge the data, and so on.

Summary: When developing, remember that attributes are used to store data, are accessed directly, are prone to security risks, so the properties in the class are usually privatized and provide public access methods externally.

This method generally has two, the canonical wording: for attribute xxx, you can use Setxxx (), GetXXX () to manipulate it.

Why is the main function not defined in the class?

Note: The main function exists only if the class needs to run independently, and if it is not needed, the main function is not defined.

interpretation of the main function: to ensure the independent operation of the class, is the entry of the program, called by the JVM.

The difference between a member variable and a local variable:

1: Member variables are defined directly in the class.

A local variable is defined in a method, on a parameter, in a statement.

2: Member variables are valid in this class.

A local variable is valid only within the curly braces it belongs to, the curly brace ends, and the local variable loses scope.

3: Member variables exist in heap memory and disappear as objects are created.

A local variable exists in the stack memory and is released as the owning region runs and ends.

constructor : Used to initialize an object, it is to initialize the corresponding object, it is targeted, one of the functions.

Characteristics:

1: The name of the function is the same as the name of the class in which it resides.

2: You do not need to define a return value type.

3: The function does not have a specific return value.

Remember: When all objects are created, they need to be initialized before they can be used.

Precautions:

1) When a class is defined, if a constructor is not defined, the constructor of an empty argument is automatically generated in the class, and initialization is done to facilitate the class to create the object. If a constructor is customized in a class, then the default constructor is gone.

2) in a class, you can have multiple constructors, because their function names are the same, so they can only be distinguished by a parameter list. So, if more than one constructor appears in a class. Their existence is manifested by overloading.

3) What is the difference between a constructor and a general function?

1: Two function definition formats are different.

2: Constructors are called when an object is created, used for initialization, and initialization actions are performed only once .

The general function is that after the object is created, it needs to be called to execute and can be called multiple times.

4) What is the difference between constructing a code block and a constructor function?

Constructing blocks of code: Initializes all objects, that is, all objects call a block of code. As soon as Object one is established. This code block is called.

Constructor: Initializes the object to which it corresponds. It is targeted.

Person p = new person ();

What does creating an object do in memory?

1: Load the Person.class file of the specified location on the hard disk into memory first.

2: When the Main method is executed, the space of the Main method (stack-to-stack) is opened in the stack memory, and a variable p is assigned to the stack area of the main method.

3: Creates an entity space in the heap memory and assigns a memory header address value. New

4: Spatial allocation of attributes in the entity space, and default initialization.

5: Display initialization of the properties in the space.

6: The construction code block of the entity is initialized.

7: Call the constructor of the entity to initialize the constructor function. ()

8: The first address is assigned to P, and the P variable refers to the entity. (pointing to the object)

--------------------------------------------------------------------------------------------

Encapsulation (one object-oriented feature): Refers to the properties and implementation details of hidden objects, and provides public access only externally.

Benefits : Isolation of changes, ease of use, improved reusability, and security.

Encapsulation principle: to hide the content that does not need to be provided externally, to hide the attributes, and to provide public methods to access them.

This : represents an object. is the reference to the object to which the function belongs.

What does this mean? which object invokes the function of this, which represents which object, which is the reference to which object .

When do you use this when developing?

When defining a feature, this is used to represent the object if it is used internally to the object that invokes the feature.

This can also be used to construct calls between functions.

Call format: This (actual parameter);

The This object follows. The member properties and member methods are called (the General method);

The This object is followed by the () constructor that calls the corresponding parameter in this class.

Note: The constructor is called with this and must be defined in the first row of the constructor. Because constructors are used for initialization, the initialization action must be performed . Otherwise, the compilation fails.

static: the keyword, which is a modifier used to decorate members (member variables and member functions).

Characteristics:

1, object sharing that wants to implement the commonality data in the object. This data can be modified statically.

2, a statically decorated member can be called directly by the class name. In other words, static members are more of a calling method. The class name. Static mode.

3, static loads as the class loads. and takes precedence over the existence of the object.

Disadvantages:

1, some data are object-specific data, can not be statically modified. Because of that, the unique data becomes the shared data of the object. There is a problem with the description of the thing. Therefore, when defining static, it is important to be clear whether the data is shared by the object.

2, a static method can access only static members and cannot access non-static members.

Because the static method loads, it takes precedence over the existence of the object, so there is no way to access the members of the object.

3, you cannot use the This,super keyword in a static method.

This cannot be used because this represents an object, while static is possible without an object.

  4, the main function is static.

when do you define static members ? Or, when defining a member, does it need to be statically decorated?

There are two types of members:

1, member variable. (data sharing is static)

Whether the data for this member variable is the same for all objects:

If so, then the variable needs to be statically decorated because it is a shared data.

If not, then say that this is the object's unique data to be stored in the object.

2, member function. (defined as static when no unique data is called in the method)

What if the member function needs to be statically decorated?

For reference, the function has access to the unique data within the object:

If there is access to unique data, the method cannot be statically decorated.

If no unique data has been accessed, then this method needs to be statically modified.

The difference between a member variable and a static variable:

1, the member variable belongs to the object. So also known as instance variables.

Static variables belong to the class. So it is also called a class variable.

2, the member variable exists in heap memory .

Static variables exist in the method area .

3, member variables exist with the creation of the object. Disappears as the object is recycled.

Static variables exist as classes are loaded. Disappears as the class disappears.

4, member variables can only be called by the object.

A static variable can be called by an object or by a class name.

Therefore, member variables can be called unique data for objects, and static variables are called shared data for objects .

Static Note: The static life cycle is very long.

Static code block: a code block area with a static keyword. defined in the class.

Function: The initialization of the class can be completed. A static block of code executes as the class is loaded and executes only once (new objects are executed only once). If the main function is in the same class, it takes precedence over the main function execution.

Public: most access rights.

Static: No object required, direct class name.

void: The main function has no return value.

Main: The name of the main function-specific.

(string[] args): The parameter of the main function, which is a parameter of the string array type, the actual argument passed when the JVM calls the main method is new String[0].

By default, the JVM passes a string array of length 0, and when we run the class, we can also specify specific parameters for delivery. You can add parameters later in the console, when you run the class. The parameters are separated by a space. The JVM automatically stores these string parameters as elements in the args array.

The execution order of static code blocks, construction code blocks, and constructors when they exist: static code blocks, construction code blocks, and constructors;

generate Java Help document : Command format: Javadoc–d folder name –auther–version *.java

/**//Format

* Class Description

* @author Author name

* @version Version number

*/

/**

* Method Description

* @param parameter description

* @return Return value description

*/

---------------------------------------------------------------------------------------------

Design Patterns: The most effective ideas for solving problems. is a set of repeated use, most people know, through the classification purposes, code design experience Summary. Design patterns are used in order to reuse code, make code easier for others to understand, and ensure code reliability.

There are 23 design patterns in Java:

Single case design mode: ★★★★★

Problem solved: Ensure that the object uniqueness of a class in memory .

For example: When multiple programs read a configuration file, it is recommended that the configuration file be encapsulated as an object. It is convenient to manipulate the data, and to ensure that multiple programs read the same profile object, it is necessary that the configuration file object is unique in memory.

The Runtime () method is designed in a single-instance design pattern.

How to ensure the uniqueness of the object?

Thought:

1, do not allow other programs to create the class object.

2, create an object of this class in this class.

3, provide methods for external, let other programs get this object.

Steps:

1, because the creation of objects requires constructor initialization, as long as the constructors in this class are privatized, other programs can no longer create the class object;

2, create an object of this class in a class;

3, define a method that returns the object so that other programs can get this type of object by means of the method. (Effect: controllable)

The code reflects:

1, privatization of the constructor function;

2, create private and static objects of this class;

3, defines a public and static method that returns the object.

---------------------------------------------

A Hungry man type

Class single{

Private single () {}//privatization constructor.

private static Single S = new single (); Creates a private and static object of this class.

public static single getinstance () {//defines a publicly-owned and static method that returns the object.

return s;

}

}

---------------------------------------------

Lazy type: Lazy loading mode.

Class single2{

Private Single2 () {}

private static Single2 s = null;

public static Single2 getinstance () {

if (s==null)

s = new Single2 ();

return s;

}

}

-------------------------------------------------------------------------------------------------

Inheritance (one of the object-oriented features)

Benefits:

1: Improved reusability of code.

2: The relationship between classes and classes provides a precondition for another feature polymorphism.

The origin of the parent class: In fact, more than one class is constantly extracting the common content.

In Java for inheritance,Java only supports single inheritance . Although Java does not directly support multiple inheritance, it retains this multi-inheritance mechanism for improvement.

Single inheritance: A class can have only one parent class.

Multiple inheritance: A class can have more than one parent class.

Why not support multiple inheritance?

Because when a class inherits two parent classes, the same functionality is available in two parent classes, which one does the subclass object run when it calls the function? Because the method body exists in the method in the parent class.

But Java supports multiple inheritance . A inherits b b inherits C C inherits D.

The emergence of multiple inheritance, there is an inheritance system. The top-level parent class in the system is drawn from the constant upward. It defines the function of the most basic and common content of the system.

Therefore, a system to be used, direct access to the function of the parent class in the system to know the basic use of the system. When you want to use a system, you need to create objects. It is recommended that you establish the most child class object, because the most subclasses can not only use the functionality in the parent class. You can also use some of the features that are specific to subclasses.

Simply put: For the use of an inheritance system, consult the contents of the top-level parent class to create the object of the bottommost subclass.

After the child parent class appears, what are the characteristics of the members in the class:

1: Member variable.

When the same property appears in the child parent class, the object of the subclass type is called, and the value is the property value of the child class.

If you want to invoke a property value in the parent class, you need to use a keyword: super

This: Represents an object reference of this class type.

Super: Represents a memory space reference in the parent class to which the subclass belongs.

Note: There is usually no member variable of the same name in the child parent class, because as long as the parent class is defined, the subclass is not defined and inherited directly.

2: member function.

When the same method appears in the child parent class, creating a subclass object runs the method in the subclass. As if the methods in the parent class were overwritten. So this is another feature of the function: overwrite (duplicate, rewrite)

When do I use overlays? When the functional content of a class needs to be modified, it can be implemented by overwriting.

3: constructor.

When the subclass constructor is found to run, the constructor for the parent class is run first. Why is it?

Cause: The first line in all the constructors of a subclass has a stealth statement super ();

super (): represents the constructor of the parent class and is used for the constructor in the parent class that corresponds to the parameter. Super (): is the constructor that calls the parent-class hollow argument.

Why do I need to call a function in the parent class when the subclass object is initialized? (Why join the Super () in the first line of the subclass constructor)?)

Because subclasses inherit the parent class, they inherit the data from the parent class, so it is important to see how the parent class initializes its own data. So when the subclass initializes the object, it calls the constructor of the parent class, which is the instantiation of the subclass.

Note: all constructors in a subclass access the constructor of the null argument in the parent class by default, because the first row within each sub-class construct has the default statement super ();

If there is no constructor for the null argument in the parent class, within the constructor of the subclass, you must specify the constructor in the parent class to access through the super statement.

If the subclass constructor uses this to specify the calling subclass's own constructor, the constructor that is called will also access the constructor in the parent class.

Question: Super () and this () can appear in the constructor at the same time.

Two statements can only have one definition in the first row, so only one can appear.

Super () or This (): Why must I define it in the first line?

Because super () or this () is called the constructor, the constructor is used for initialization, so the initialization action is done first.

Details of the Inheritance:

When do you use inheritance?

There is a precondition for inheritance when there is a relationship between classes and classes. A is one of the B's. A inherits B. Wolves are one of the canine branches.

English book, Affiliation: "is a"

Note: Do not inherit just to get an existing member in another class.

So to judge the relationship, you can simply see, if inherited, the function of the inherited class, can be the subclass has, then the inheritance is established. If not, you cannot inherit.

Detail two:

When the method is covered, note the two point:

1: When a subclass overrides a parent class, it must be guaranteed that the permissions of the subclass method must be greater than or equal to the parent class method permission to implement inheritance. Otherwise, the compilation fails.

2: Overwrite, either static, or not static. (Static can only be overridden by static, or statically overwritten)

One drawback of inheritance: breaking the encapsulation. For some classes, or functions in a class, they need to be inherited, or replicated.

Then how to solve the problem? Introduce a keyword, final: final.

Final features:

1: This keyword is a modifier that can be decorated with classes, methods, and variables.

2: The final modified class is a last class and cannot be inherited.

3: Final modified method is a final method and cannot be overwritten.

4: The final modified variable is a constant and can only be assigned once.

In fact, the reason for this is to give some fixed data to read a strong name.

Is it possible to use no final decoration? Then this value is a variable that can be changed. Added final, more rigorous procedures. When a constant name is defined, there are specifications, all letters are capitalized, and if it consists of multiple words, the middle is connected with _.

Abstract class: Abstraction

Abstract: Not specific, can not understand. Abstract class representation manifests itself.

In the continuous extraction process, the common content of the method declaration extraction, but the method is not the same, no extraction, then the method extracted, not specific, need to be specified by the keyword abstract marked, declared as an abstract method.

The class in which the abstract method resides must be marked as an abstract class, meaning that the class needs to be decorated with the abstract keyword.

Features of abstract classes:

1: Abstract methods can only be defined in abstract classes, abstract classes and abstract methods must be decorated by the abstract keyword (you can describe classes and methods, and you cannot describe variables).

2: Abstract methods define method declarations only and do not define method implementations.

3: Abstract class can not be created object (instantiation).

4: This subclass can be instantiated only if the subclass inherits the abstract class and overrides all abstract methods in the abstract class. Otherwise, the subclass is an abstract class.

The details of the abstract class:

1: Is there a constructor in an abstract class? There, used to initialize the subclass object.

2: Is it possible to define non-abstract methods in an abstract class?

OK. In fact, there is not much difference between abstract class and general class, it is to describe things, but abstract classes describe things, some functions are not specific. Therefore, both abstract and generic classes need to define properties and behaviors in the definition. However, there is an abstract function more than the general class. And it's a little less than a generic class. The part that creates the object.

3: Abstract keyword and which cannot coexist? Final, private, static

4: Can I not define abstract methods in an abstract class? OK. The abstract method is intended only to keep the class from creating objects.

-----------------------------------------------------------------------------------------------

Java essentials: Object-oriented,

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.