Construction Method
One: Concept :
Initialize the object's data (properties)
Two: Features :
A. Method name has the same name as the class (same letter case)
B. No return value type
C. No specific return value returned
Three: Constructor method overloading :
Method names are the same, regardless of the return value type (because the constructor does not return a value), only the argument list
Note: If you do not write a construction method, the system automatically provides a non-parametric construction method, if we give a construction method,
The system no longer provides a construction method.
1 class student{ 2 Below is the construction method 3 public student{ 4 non-parametric construction method 5 " 6 public person (String name,int 7 // There is a method for constructing a parameter 8 } 9 }
Member variables
One: Assign value
A. Through the Setxxx () method
B. By way of construction
Example : Student class
classstudent{PrivateString name;Private intAge ;//Here are the construction methods PublicStudent (String name,intAge ) { //Construct method Assignment value//this.name = name;//this.age = age; } Public voidsetName (String name) { This. Name =name;} Public voidSetage (intAge ) { This. Age =Age ;} }
Steps to create an object
Student s = new Student ();
1,student.class Loading into memory
2, declaring a student type reference s
3, the object is created in heap memory,
4, default initialization value for property in Object
5, the property is displayed for initialization
6, constructs the method into the stack, assigns the value to the attribute in the object, constructs the method the bomb stack
7, assign the object's address value to s
Static keyword
One: Concept:
Static is used to decorate a member of a class, such as a member variable, a member method, a code block, a static
The modified members have some peculiarities.
Two: particularity
A: Loaded with the load of the class
B: Precedence over object existence
C: Shared by all objects of the class
Example: Students in our class should share the same class number. In fact, this feature is also
Is it telling us when to use static?
If a member variable is shared by all objects, it should be defined as a static
Of
Example:
Water dispenser (with static modification)
Water Cup (cannot be modified by static)
Common use static, non-static characteristics
D: Can be called by the class name
In fact, it can also be called by the object name itself.
It is recommended to call with the class name.
Content that is statically modified is generally referred to as: Class-related, Class-member
Three: Precautions
A: There is no this keyword in the static method
How to understand it?
Static is loaded as the class loads, and this is present as the object is created.
Static than object exists first.
B: Static methods can only access static member variables and static member methods
static method:
Member variables: Only static variables can be accessed
Member method: Only static member methods can be accessed
Non-static methods:
Member variable: can be static or non-static
Member method: However, it is a static member method or a non-static member method.
Easy to remember:
Static can only be accessed statically.
Four: The following
Classstatic String name; // Static Variables Static {System.out.println ("This is a static code block" publicstaticvoid SayHello () {System.out.println ("This is a static method");}}
Five: The difference between a static variable and a member variable
A: Belong to different
* Static variables belong to classes, so they are also referred to as class variables
* Member variables belong to an object, so also known as instance variables (object variables)
B: Different locations in memory
* Static variables are stored in the static area of the method area
* Member variables are stored in heap memory
C: Memory occurrence time is different
* Static variables are loaded as the class is loaded and disappear as the class disappears
* Member variables exist as the object is created and disappear as the object disappears
D: Call Different
* Static variables can be called through the class name, or through object invocation
* Member variables can only be called by object name
The format of the main method is explained in detail
A: Format
* public static void Main (string[] args) {}
B: Explanation of the format
* Public is called by the JVM and the access permissions are large enough.
* Static is called by the JVM without creating objects, direct class name access
* Void is called by the JVM and does not need to return a value to the JVM
* Main a generic name, although not a keyword, but is recognized by the JVM
* string[] args was previously used to receive keyboard input
Using the help documentation provided by the JDK
A: Locate the document, open the document
B: Click on the display, find the index, the input box appears
C: You should know who you're looking for. Example: Scanner
D: Look at the structure of this class (requires no guide package)
* Member Variable Fields
* Constructing method of construction method
* Member Method method
Math class
One: Concept :
class contains methods for performing basic mathematical operations
Two: The method of obtaining the random number :
public static Double Random (): Returns a double with a positive number that is greater than or equal to 0.0
and less than 1.0.
Children's shoes can be verified by themselves.
code block
Code block Overview:
* In Java, code enclosed in {} is called a code block.
Code block Classification:
* Depending on its location and declaration, it can be divided into local code blocks, construction code blocks, static code
block, and synchronize the code block (multithreaded instruction).
Common code blocks:
* A: Local code block
To appear in a method; limit a variable's life cycle, release early, and improve memory utilization
* B: Constructs a block of code (initialization block)
Occurs outside of a method in a class, and the same code is stored together in multiple construction method methods, each
Executed with a construct and executed before the method is constructed
* C: Static code block
Occurs outside of a method in a class, and is added with a static modifier, used to initialize the class, in the loaded
, and executes only once.
Typically used to load the drive
classText {Static{System.out.println ("Static code block");} {System.out.println ("Building Code Blocks");} PublicText () {System.out.println ("Construction Method");}} Public classDemo {
Static{System.out.println ("Demo Static code block");} Public Static voidMain (string[] args) {
System.out.println ("I am the Main method"); Text S1=NewText ();
Text s2 =NewText ();}}
Operation Result:
As you can see, execute sequential static code blocks > construct code blocks > Constructors
Object-Oriented inheritance
Inheritance (extends) concept:
To have a relationship between a class and a class, the child parent class relationship
Benefits of Inheritance:
* A: Improved reusability of the code
* B: Improved maintainability of the code
* C: Make a relationship between classes and classes, which is a prerequisite for polymorphism
Disadvantages of Inheritance:
* The coupling of the class is enhanced.
* Principles of development: cohesion, low coupling.
* Coupling: Class-to-class relationships
* Cohesion: The ability to do something yourself
Characteristics of Inheritance:
* A:java only supports single inheritance and does not support multiple inheritance. (a son can only have one father)
* Some languages are supported multi-inheritance, format: Extends Class 1, Class 2,...
* B:java supports multilayer inheritance (inheritance system) * If you want to use all the functions of this system
The Bottommost class creates the object
* If you want to see the general function of this system, look at the top-level class
Considerations for Inheritance:
* A: Subclasses can only inherit all non-private members of the parent class (member methods and member variables)
*b: Subclasses cannot inherit the constructor of a parent class, but can be visited by super (immediately speaking) keywords
Ask the parent class to construct the method.
* C: Do not inherit for part of the function
When to use inheritance:
* Inheritance actually embodies a relationship: "Isa". If there are two classes, a, B.
Only if they meet the one of A is B, or B is a, you can consider using inheritance
The difference and application of this and super:
This and super are very similar in usage.
This represents a reference to this class of objects.
Super represents the identity of the memory space of the parent class.
The use of this and super is different:
* A: Call member variable
*this. member variables call member variables of this class, or you can call member variables of the parent class
*super. member variables call the member variables of the parent class
B: Call construction method
*this (...) call the constructor of this class
*super (...) constructor method for calling parent class
* C: Call member method
*this. Member methods call member methods of this class, or they can call methods of the parent class
*super. Member method call member method of parent class
Final keyword
Final retouching features
The 1,final decorated class is a final class and cannot be derived from a subclass.
The method of 2,final modification is the final method and cannot be overridden.
A 3,final modified variable is a constant that can only be assigned once. The name of the constant that is final modified
All the letters are in uppercase. If a word consists of multiple words, connect through _.
4, you can assign a value once by constructing a code block or constructor function.
* Basic type, value cannot be changed
* Reference type, is the address value cannot be changed, the property in the object can be changed
Java Programming Fundamentals Chapter Sixth