Summary of learning contents of textbook
The fourth chapter is the basic and key point of the whole Java language, and we should focus on learning and grasping.
Fourth Chapter Essentials
- Basis
- Class
- Construction method and object creation
- Basic structure of class and program
- Focus
- Parameter Passing value
- Object combinations
- Java unique syntax
- Instance members and class members
- Method overloading
- this keyword
- Package
- Import statement
- Practical
- JRE extension with Jar file
Problems in teaching materials learning and the solution process I. Object-oriented
- Features: encapsulation, inheritance, polymorphism
- Focus on understanding: Class
Second, class
- Classes: Important Data types
- The meaning of the class : encapsulating the data and manipulating the data
- Contents of class : class declaration + class body
- class declaration: A variable is called an object variable, a short object, with a
class 类名
representation
- Class Body: Variable declaration (embodying data, attributes ) + method (embodying action, behavior ),
{ }
all content included after class declaration
- The contents of the class body
- member variables/domain variables: variable declarations partially declared variables
- The effective range is independent of the entire class and writing position
- Variable naming habit first word lowercase (if more than one word starts with the second word capitalize, eg:heightlightflight, without underlining, hump habit)
- Member variables have default values
- Method: Define a method
- Method = Method Header + Method body
- Method Body: Local variable + statement, C-like function
- A parameter defined by a local variable in a method body is valid only within a method and is related to a position, starting after declaring its position.
- The local variable has the same name as the member variable, and the member variable is temporarily invalidated within the method
- Local variable has no default value
Third, construct method and create object
- Construction method
- When creating an object, the name is the same as the class name, with no type
- Default constructor methods and custom construction methods
- Default: Default One class has only one parameterless construction method
- Custom: The system does not provide a construction method when it is customized, there are several constructs. You can have more than one constructor in a class, but you must ensure that the constructor names are the same as the class names and that the parameters are different (quantity or type)
- Creating objects
- Create object = Declaration of Object + assign variable to object
- Object declaration:
类名 对象名
- Assigning a variable to an object: How to construct thenew operator + class
- The memory model of the object
new运算符
The result of the operation is a hexadecimal number, a reference
- First, allocate memory space to the member object, and then execute the statement in the constructor method
Iv. parameter value (emphasis)
- Pass-through mechanism: all parameters in the method are copies of the specified values are called
- Two methods: the pass value of the basic data type parameter, the value of the reference type parameter
- Basic data type parameter: The level of the value passed to this parameter is not higher than the level of the parameter, and does not change the value of the passed in parameter itself
- Reference type parameter: pass the "reference" in the variable, not the entity that the variable refers to, change the parameter variable entity to change the entity of the original variable, change the parameter variable "reference" does not change the original variable "reference", and vice versa
- Variable parameters:
eg. public void f(int … x)
- Parameter type consistent
- A variable parameter represents the last
- Use to
…
represent several parameters
V. Combination of objects (emphasis)
- Combination and reuse: two objects declared by a class if they have the same reference, the two have exactly the same variables
Vi. instance members and class members
- instance variable declaration and class variable declaration
- Instance variables (no adornments):
float x;
- Different object instance variables
- Access the instance variable from this object
- Class variable (static variable/statically variable):
static int y
- Static needs to be placed in front of the variable type
- All objects share class variables
- Accessing class variables directly through the class name
- Instance methods and class methods
- Example method:
float max(float x,float y) { … }
- Object Invocation instance method (direct use)
- Class method:
static float jerry() { … }
- Static is placed before the method type
- Class names Call class methods (like C-language function calls)
Vii. Method Overloading
- Java polymorphism: Overloading, overriding (inheritance related)
- Method overloading: Polymorphism of object behavior
- A class can have more than one method with the same name, but must have different parameters (number or type, two choices)
Eight, this keyword
- In the construction method: can be omitted
- In the instance method:
- Instance member variables:
this.成员变量;
- Class member variables:
类名.成员变量;
- Instance member name is same as local variable name,
this.
or 类名.
cannot be omitted
- Instance methods are invoked in the instance method:
this.方法;
- The class method is called in the instance method:
类名.方法;
- When an instance method calls another method, it can omit
this.
and类名.
- This cannot be called in a class method because the class method can be called directly through the class name and may not be called when the object is not yet born
Problems and resolutions in code debugging I. Compilation and operation of several classes of programs distributed across multiple source files
An error occurred while not being placed in the same folder:
Check to find out why, put these several source files in a separate folder, and then compile the main class where the source file Example4_5.java:
Therefore, when several classes of the program are distributed across multiple source files, all source files need to be placed in the same folder and compiled in the terminal to succeed, and only the main class can be compiled.
- As you can see, the system will automatically compile Rect.java and Lader.java when compiling the main class directly, because the byte codes generated by the two source files are needed in the main class
Compiled with three bytecode file generation, where Rect and Lader are reusable code, equivalent to the callable function in C, the main class is equivalent to the main function, directly call the code can be calculated
Second, the return type is different (again emphasize!!) Avoid careless making mistakes)
An error occurred at compile time:
found that the class name and file name do not match in the source file and cannot return a value
You can compile and run it correctly after you modify it.
Third, compile and run of package statement and import statement
The classes in the custom package are introduced in Example4_17 and Example4_18, and the first compilation did not place the Example4_18.java in the package file hello/nihao
causing an error:
Put in and then compile and run:
- This is because the main class in Example4_18.java does not have a package name, so you need to put the custom package name into the same folder as the catalog and the class without the package name
- Are all nameless packages and the classes under the same folder can use each other
- Nameless Package classes can use the import statement to use the class of a well-known package
Class with the name of the package cannot use the class of the nameless package
Code Hosting
Last week code see Code Cloud CH04
Last week's summary of the wrong quiz
A second test has not been conducted and will be supplemented later
Feeling and thinking
This chapter is much more difficult and deeper than the first two weeks, and because of the delay in the illness, it takes a week to learn more than before, but it is still within reach. See the fourth chapter of the learning video also learned that this chapter is the entire Java learning in the foundation and focus, the importance of the class is self-evident. After the test on the blue ink cloud, I also have a deeper understanding of the knowledge point, although the perception of some points is still floating on the surface, but I believe that in the next study will gradually understand the importance of it, such as the book from the beginning of the first chapter has been repeated the words "a class declaration of two objects if they have the same reference, Then the two have exactly the same variable ", to this chapter finally has a more clear understanding.
Learning progress Bar
|
lines of code (new/cumulative) |
Blog Volume (Add/accumulate) |
Learning Time (new/cumulative) |
Important Growth |
Goal |
3000 rows |
30 Articles |
400 hours |
|
First week |
178/200 |
1/2 |
20/20 |
Basis |
Second week |
368/200 |
2/2 |
20/20 |
Basis |
Third week |
865/200 |
3/2 |
20/20 |
Gradually deepen |
Resources
- The fourth chapter teaching video
- Debugging Java programs with JDB
20165223 the third week of Java Program Design Study summary