Java review Summary 2 static method

Source: Internet
Author: User
Tags abstract definition define abstract

Java review Summary 2 static method
Static Method
Like static variables, we can also use static methods, called static methods or class methods.
In fact, the main method we have been writing is the static method.
Static members of the same type can be called directly in static methods, but non-static members cannot be called directly.
To call non-static variables in a static method, you can create a Class Object and access non-static variables through the object.
In common member methods, you can directly access non-static and static variables of the same type.
Non-static methods cannot be called directly in static methods. You must use objects to access non-static methods.
Static initialization Block
In Java, values can be assigned through initialization blocks.
The class declaration can contain multiple initialization blocks. When a class instance is created, these code blocks are executed in sequence. If you use
Static modifier initialization block is called static initialization block.
Note: The static initialization block is only executed when the class is loaded and will only be executed once. At the same time, the static initialization block can only assign values to the static variables and Cannot initialize common member variables.
Object-oriented features
Three main features of object-oriented: encapsulation, inheritance, and Polymorphism
Encapsulation: some information of the class is hidden inside the class, and external programs are not allowed to access it directly,
This class provides methods to perform operations and access to hidden information.
Advantages: 1. You can only access data in the specified method. 2. Hide the instance details of the class to facilitate modification and implementation.


Role of the java package: Managing java files to resolve conflicts with files of the same name
Package usage: You can use classes in other files in a file using the import keyword.
In java, the package naming rules are all lowercase letters spelling.
Access modifier in java
Access modifier: you can modify the access range of attributes and methods.
Access modifier Summary
The access modifier. This class is similar to other subcategories.
Private is
The default value is
Protected is yes
Public is yes
This keyword in java
This keyword indicates the current object
This. Attribute operation on the attributes of the current object
This. The method calls the method of the current object.
This keyword is often used when encapsulating object attributes.
Internal classes in java
An Inner Class is a Class defined in another Class. Classes that contain internal classes are called external classes.
The main functions of internal classes are as follows:
1. Internal classes provide better encapsulation. Internal classes can be hidden from external classes, and other classes in the same package are not allowed to access this class.
2. Internal class methods can directly access all data of the external class, including private data
3. functions implemented by internal classes can also be implemented using external classes, but sometimes internal classes are more convenient.
Internal classes can be divided into the following types:
Member internal class
Static internal class
Internal method class
Anonymous internal class
Static internal classes in java
Static internal classes are static modified internal classes, which are characterized:


1. static internal classes cannot directly access non-static members of external classes, but they can be accessed through new external classes (). members.


2. If the static member name of the external class is the same as that of the internal class, you can use the "class name. "Static members" Access static members of the external class. If the static members of the external class are different from those of the internal class, you can directly call the static members of the external class using the "member name ".


3. When creating static internal class objects without external class objects, you can directly create an internal Class Object Name = new internal class ();
Internal classes of methods in java
The internal class of the method is the method in which the internal class is defined as the external class. The internal class of the method is only visible within the method, that is, it can be used only within the method.
Note: because the internal class of the method cannot be used outside the external class, the internal class of the method cannot use the access controller and static modifier.
Internal classes cannot have their own member methods and member variables.

Inheritance
Inheritance is a relationship between classes and classes. It is a "is a" relationship.
In java, the inheritance is a single inheritance.
Benefits of inheritance: subclass has all attributes and methods of the parent class (private modification is not allowed)
Code reuse
Syntax Rules:
Class subclass extends parent class
Method Rewriting
If the subclass is not satisfied with the method that inherits the parent class, you can override the method that inherits the parent class.
When a method is called, The subclass method is preferentially called.
Syntax Rules:
Return Value Type
Method Name
Parameter type and quantity
It must be the same as the method inherited by the parent class.
Inherited initialization sequence
1. initialize the parent class in the initial subclass.
2. Execute the attributes in the initialization object and initialize them in the execution constructor.
Final keywords
Using final keywords for identification has "final" meaning
Final can modify classes, methods, attributes, and variables.
Final modifier class, the class cannot be inherited
If the final modifier is used, the method cannot be overwritten)
Final modifier attributes
The class attributes are not implicitly initialized (class initialization attributes must have values)
Or assign values to the constructor (but only one of them can be selected)
If final modifies a variable, the value of the variable can only be assigned once, that is, it becomes a constant.
Super keyword
Used inside an object to represent a parent class Object
Application: the constructor of its parent class must be called during subclass construction.
If the constructor of the subclass does not display the constructor that calls the parent class,
The system calls the constructor without parameters of the parent class by default.
If the displayed constructor is called, it must be in the first line of the constructor of the subclass.
If the constructor of the parent class is not displayed in the subclass constructor, and the parent class does not have a constructor without parameters, the compilation fails ,.
Object Class
The Object class is the parent class of all classes. If a class does not use the extends keyword to explicitly identify and inherit from another class, this class inherits by default.
Object Class
Methods In the Object class, suitable for all subclasses.
1 toString () method
The hash code (Object address string) of the returned Object when the toString () method is defined in the Object class)
You can override toString () to indicate the attributes of an object.
2 equal () method
Compare whether the object reference points to the same memory address

Generally, when two objects are compared, check whether their values are consistent. Therefore, rewrite them.

Polymorphism
Multiple forms of Objects
1 Reference Polymorphism
The reference of the parent class can point to the object of this class.
The reference of the parent class can point to the object of the subclass.
2 method Polymorphism
The method called when creating this class object is this class Method
When a subclass object is created, the method called is the method rewritten by the subclass or the inherited method.
Conversion of reference types in Polymorphism
1. Upward type conversion (implicit/automatic type conversion), which is a conversion from small type to large type.
2. Downward type conversion (forced type conversion): large to small type
3 instanceof operator to solve the type of referenced object and avoid the security problem of type conversion
Abstract classes in java
Syntax definition: If abstract keywords are used before an abstract class, the class is an abstract class.
Application scenarios:
1. In some cases, a parent class only knows how its subclass should contain.
But cannot know exactly how these sub-classes implement these methods.
2. Abstract An abstract class from multiple classes with the same features and use this abstract class as the template of the subclass,
This avoids the randomness of subclass design.
Purpose: Restrict sub-classes to implement certain methods, but ignore implementation details.
Rules
1 abstract definition abstract class
2 abstract: define abstract methods. Only declarations are required and do not need to be implemented.
3. Classes containing abstract methods are abstract classes.
4. abstract classes can contain common methods or abstract methods.
5. abstract classes cannot be directly created. You can define referenced variables.
Java Interfaces
Interface concepts
An interface can be understood as a special class consisting of global constants and common abstract methods.
A class is a specific implementation body, and an interface defines the specifications that a batch of classes need to comply with, the interface does not care
The internal data of these classes does not care that some methods must be provided in these classes.
Interfaces are used for inheritance and implementation. public is recommended for modifiers,
Note: The private and protected modifier interfaces cannot be used.
Constant
The attribute in the interface is a constant. Even if the public static final modifier is not added during definition, the system automatically adds
Method:
Methods In the interface can only be abstract methods, which are always used. Even if the public abstract modifier is not added during definition, the system automatically adds
Interface:
A class can implement one or more interfaces. The Implemented interfaces use the implements keyword. A class in java can only inherit one parent class,
It is not flexible enough to implement multiple interfaces.
If you want to inherit the parent class, the inherited parent class must be before the interface is implemented
Interface:
APIs are often used in combination with anonymous internal classes.


An anonymous internal class is an internal class without a name. It is mostly used to focus on the implementation rather than the name of the implementation class.

UML
Concept
Unified Modeling Language (UML)
Also known as unified modeling language or standard modeling language
Is a graphical language that supports modeling and software system development.
Provide modeling and visualization support for all stages of Software Development
Common UML diagrams
Use case diagram
The use case diagram shows how the system meets the collected business rules in a visualized manner, and
Specific user requirements and other information.
Sequence Diagram
A Sequence Chart is used to display the interaction between objects in a series of order of interaction.
Class Diagram
UML class diagrams, business logic, and all supported structures are used together to define all the code structures.

 

Related Article

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.