java--Object-oriented

Source: Internet
Author: User

Take notes in the video tutorial of teacher bi:

1. Class: Class defined with class. Defining a class is defining attributes (variables) and behaviors (functions (methods)). Properties and behaviors are common to members (member variables and member functions) in a class.
2. Object: Create an entity in heap memory with new.
Note: All that is used to store multiple data is called an entity, and the entity is placed in heap memory, for example: an array.
Eg:class Car//define Class
{
Member variables
String color = "Red";
int num = 4;
void Run ()
{
System.out.println (color+ "..." +num);
}
}
Class Cardemo
{
public static void Main (string[] args)
{
Generates an entity with the new heap memory. That is, the object.
C is a class-type variable (it is in the stack memory). class type variable points to object (object in heap memory)
Car c = new car (); Assigns the address of the entity in the heap memory to the class type variable C
}
}
2.1 Member variables and local variables:
Scope: the member variable acts on the entire class. A local variable acts in a function, or in a statement.
2.2 In-memory location:
Member variable: In heap memory, exists in memory because the object (the keyword new creation) exists.
Local variable: exists in stack memory.

3. Package (encapsulation)
Concept: Refers to the hidden object's properties and implementation details, only provide public access to the external way.
Benefits:
Isolate the changes.
Easy to use.
Improve reusability.
Improve security.
Encapsulation principle:
Hide content that you don't need to provide externally.
Hides properties, providing public methods for accessing them.

4. Related modifiers:
Private: Proprietary, permission modifier: Used to decorate a member (member variable, member function) in a class. Private is only valid in this class.
Eg:private int age; After the age is privatized, objects outside the class cannot be accessed directly, even if an object is established. If you want to access, you can provide the corresponding interface:
Naming conventions: Setting: set+ private variable first letter capitalization.
Get: get+ Private variable first letter capitalization.
private int age;
public void Setage (int a)//with parameter no return value
{
if (a>0 && a<130)
age = A;
Else
System.out.println ("Error age!")
}
------------------------------------------------------
public int getage ()//no parameter has return value
{
return age;
}
Note: Private is only a representation of the package. The reason why external access is provided is that logic judgments and other statements can be added to the access mode. Improve code robustness by manipulating access to data.

5. Constructors:

Characteristics:
The function name is the same as the class name
Do not define return value types
You cannot write a return statement
Role:
Initializes the object. (When Object one is established (the class name Variable name = new class name ()), the corresponding constructor is called. )

Format:

Class name () {}

Attention:
When a constructor is not defined in a class, the system defaults to the constructor that adds an empty argument to the class. (class name () {})
When a constructor is customized in a class, the default constructor does not exist.
Constructors and general functions differ in their notation.
are also different on the run.
The constructor is run as soon as the object is established and initialized to the object. The general method is that object invocation is performed, which is the function of adding objects to objects.
An object is established, and the constructor is allowed only once. The generic method can be called multiple times by the object.
Multiple constructor functions exist in the form of overloading.
When do you define a constructor?
When a thing is analyzed, it has some characteristics or behavior, then the content is defined in the constructor.

6. Construct code blocks: (The initialization content defined in the construction code block is the commonality of different objects.) )
Function: initializes the object.
The object runs as soon as it is established, and takes precedence over the constructor execution.
To construct a code block and constructor differences:
Constructing a block of code is a uniform initialization of all objects, and constructors are initialized to the corresponding object. Constructing code blocks takes precedence over constructor execution
Format

{}

7. Keywords: this
Represents an object of this class. (a reference to the object to which the function belongs.) Simply put: Which object is the object that represents the function that this is located in. Eg:p. class name (); So this is P)
This applies: When defining a function in a class, the function is used internally to represent the object when it is called by this function.
This keyword is a call between constructors:
Person (String name)
{
THIS.name = name;
}
Person (String name, int age)
{
This (name); Call the constructor, which is equivalent to P (name)->new person (name);
This.age = age;
}
Note: this (); Can only be placed in the first row of the constructor (because initialization actions are performed first.) )

8. Keywords: static
Usage: is a modifier used to decorate members (member variables, member functions).
When a member is statically modified (static member), one more invocation method can be called directly by the class name in addition to being called by the object. (class name. Static member)
Static Features:
1. Load as the class loads. (Create a method area (a shared or data area). In other words: Static disappears as the class disappears. Indicates that it has the longest life cycle.
2. Precedence over object existence. (Make it clear: the static is first, the object is a post-existence.) )
3. Shared by all objects.
4. Can be called directly by the class name.
The difference between an instance variable (member variable) and a class variable (a static member variable):
->1. Storage location:
Class variables exist in the method area as the class is loaded.
Instance variables exist in heap memory as the object is established.
->2. Life cycle:
The class variable has the longest life cycle and disappears as the class disappears.
The instance variable life cycle disappears as the object disappears.

Static usage considerations:
->1. Static methods can only access static members (static). Non-static methods can access both static and non-static. The This,super keyword cannot be defined in the
->2. static method. Because static overrides exist for the object. Therefore, this is not available in a static method. The
->3. Main function is static.
public static void Main (string[] args)
Main function: is a special function that, as the entry of a program, can be called by the JVM.
The definition of the main function:
Public: Represents the maximum access permission for the function.
Static: Represents the main function that already exists as the class is loaded. ,
Void: The main function does not have a specific return value.
Main: Not a keyword, but a special word that can be recognized by the JVM.
String[] args: The parameters of the function. The parameter type is an array in which the elements are strings. An array of string types.
args is the array name, and string is the array element type. The
main function is in fixed format: JVM recognition. When the
JVM calls the main function, the new string[x],x is passed in to indicate the number of arguments. (String [] args = new string[x]).
Static Pros and Cons:
---save space by storing the shared data of an object in a separate space. There is no need to store a copy in each object. Can be called directly by the class name.
--Disadvantages: The life cycle is too long. Locality of access. (Static, although good, can only access static.) )
What uses static?
to do it in two ways:
because the content that is statically decorated has member variables and functions. When does the
define static variables (class variables)?
When shared data appears in an object, the data is decorated statically. The unique data in the
object to be defined as non-static exists in heap memory. When does the
define a static function?
When a feature does not have access to non-static data (unique data for an object), the function can be defined as static.

In addition: You can define the static method as a tool class in the class (the function does not have access to non-static data), and then it is called directly from the class name.
After the method is static, it is easy to use, but the class can also be used by other programs to establish objects, in order to be more rigorous, forcing the class can not establish objects.

At this point, you can do this by privatizing the constructors in the class.
eg
Class name
{
Private class name () {}
}

Static code block:
Static
{
The execution statement in a static code block.
}
Feature: Executes as the class is loaded (using the contents of the class is called loading), executes only once, and takes precedence over the main function used to initialize the class.

9. Initialization procedure when creating an object of a class: (Priority: Static code block (static{}) > construct code block ({}) > constructor (class name {}))
Eg:person p = new Person ("Zhangsan", 20);
What has been done in this sentence?
->1. Because new uses person.class, all person.class files will be found and loaded into memory first.
->2. Executes the static block of code in the class and, if any, initializes the Person.class class.
->3. Space in heap memory, allocating memory addresses.
->4. Establish the unique properties of the object in heap memory. and the default initialization.
->5. Display initialization of properties.
->6. Constructs a code block initialization for an object.
->7. Initializes the corresponding constructor for the object.
->8. Assigns the memory address to the P variable in the stack memory.

10. Single Case design mode

10.1 A Hungry Man: Initializes an object first. The most effective way to solve a certain type of problem.
There are 23 design patterns in Java:
Singleton design pattern: Resolves a class that only has one object in memory.

The

wants to guarantee that the object is unique.
1. In order to prevent other programs from creating such objects too much. Prevent other programs from establishing this class object first.
2. In order for other programs to have access to the class object, it is necessary to customize an object in this class.
3. In order to facilitate access to custom objects by other programs, you can provide some external access.
The above three is represented by code:
->1. Privatize the function.
->2. Creates a private and static object of this class in a class.
->3. Provides a way to get to the object.    
Class single
{
Private single () {}
private static single S = new single ();
    Returns single
public static single getinstance ()
{
return s;
The
}
Describes how things should be described. The
adds the above three steps when it is necessary to guarantee the object of the thing to be unique in memory.
10.2. Lazy: An object is initialized only when the method is called, and is also called the deferred load of the object.


Class single
{
private static single s = null;
Private single () {}
Returns a single
public static single getinstance ()
{
if (s==null)
{
Synchronized (Single.class)
{
if (s==null)
s = new single ();
}
}
return s;
}
}
The difference between a a hungry man and a lazy type in a single-case design pattern:
A hungry man: once the single class is in memory, the object has been created.
Lazy: Single class into memory, object does not exist yet.

At this point, s points to null, only when the GetInstance method is called, the object is established, and the object address is assigned to the class type variable in the stack memory (the process with the delay), that is, s points to the single () object.
Principle: When defining a singleton design pattern in development, a hungry man is recommended.

java--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.