<>1 Construction method
<>1.1 concept
The construction method is a special method , It is a method with the same name as a class and no return value type
The main function of construction method is to create or initialize objects
When a class creates an object ( instantiation ) Time , The constructor is called automatically
Construction methods can be overloaded just like normal methods .
<>1.2 form
Same name as class , And there is no return value type , It can be with or without parameters
<>1.3 practice : Construct methods to create objects / Assignment of construction method
create package : cn.tedu.oop
Create class : TestConstructor.java
package cn.tedu.oop; /** This class is used to test construction methods */ public class TestConstructor {
//4. Creating an entry function in a public class public static void main(String[] args) { //5. establish Person Class
/**1. every time new( instantiation ) Object automatically calls the constructor */ Person p = new Person(); System.out.println(p.name
); System.out.println(p.age); System.out.println(p.address); p.eat(); Person p2
= new Person(" Spongebob "); System.out.println(p2.name); System.out.println(p2.age);
System.out.println(p2.address); p.eat(); Person p3 = new Person(" Patrick Star ",3," seabed ");
System.out.println(p3.name); System.out.println(p3.age); System.out.println(p3.
address); p.eat(); } } //1. establish Person class , Used to describe the type of person class Person{ //2. attribute -- Create member variable
String name;// full name int age;// Age String address;// address /**2. Nonparametric construction exists by default , When new
Person() This parameterless construct is automatically triggered */ /**3. The construction method is overloaded : Overload of method : In the same class , The phenomenon of the same method name and different parameter list */
// Definition of common methods : Modifier return type Method name ( parameter list ){ Method body } // Definition of construction method : Modifier Method name ( parameter list ){ Method body } -- The method name is consistent with the class name
//6.1 Create parameterless construction -- When new Person() Time , Will trigger public Person() { System.out.println(
" I am Person Nonparametric construction of classes "); } /** Quick copy down :Ctrl+Alt+ Down key */
/**4. When only parametric constructions are provided , The default nonparametric construction is overridden , So when creating overloaded construction methods , You must pay attention to adding nonparametric constructs manually */ //6.2 establish 1 The construction of two parameters public
Person(String s) { System.out.println(" I am Person Class 1 The construction method of two parameters "+s); }
//6.3 Create full parameter construction -- Several properties are defined , Just a few parameters ,new Person(" Patrick Star ",3," seabed "); Will trigger public Person(String n,
int a,String addr) { name = n;//n Is a local variable ,name Is a member variable , Transfer the parameter value of the user n Assign to member variable name age = a;
//a Is a local variable ,age Is a member variable , Transfer the parameter value of the user a Assign to member variable age address = addr;
//addr Is a local variable ,address Is a member variable , Transfer the parameter value of the user addr Assign to member variable address System.out.println(
" I am Person Total parameter construction of class "); } //3. behavior -- Creation method public void eat() { System.out.println(
" It's time , It's time for supper ~"); } }
How to remember about constructors :
* characteristic : The method name is the same as the class name , And there is no return value type
* Timing of implementation : Execute as soon as the object is created
* By default, a parameterless construct is created , however , If you customize the construction with parameters , The default nonparametric construction is overridden , Pay attention to add it manually
<>2 Construct code block and local code block
<>2.1 form :
{ code … }
<>2.2 Characteristics of building code blocks
* position : Inside the class , Outside of the method
* effect : It is used to extract common code in construction method
* Timing of implementation : Every time a constructor is called, the constructor block is called
* matters needing attention : Construction code blocks are loaded prior to construction methods
<>2.3 Local code block
* position : Code blocks in methods
* effect : It is usually used to control the scope of the variable , The curly brackets are invalid
* matters needing attention : The smaller the scope of the variable, the better , Member variables are thread safe
<>2.4 practice : Load order of test code blocks
create package : cn.tedu.oop
Create class : TestBlock.java
package cn.tedu.oop; /** This class is used to test the loading order of code blocks */ /** * summary : *
1. When creating an object , The program automatically mobilizes the construction method , But if there is a construction code block , The construction code block is executed first , Reexecution construction method *
2. When a method is called through an object , Performs the functions in the method , If there is a local block of code in the method , The local code block is executed * 3. Execution sequence :
Construct code block --> Construction method --> Local code block [ premise : Call method ( If the method has a local block of code , Local code blocks are executed )] * 4. Function of constructing code block : It is used to extract the commonness in the construction method
* 5. Function of local code block : Scope of control variables * */ public class TestBlock { public static void main(
String[] args) { //5. stay main() Create objects for testing in Teacher t = new Teacher();
/** Every time an object is created /new Time , The code block is constructed once */ Teacher t2 = new Teacher(); Teacher t3 = new
Teacher(" Greater China "); t.study(); } } //1. Create a Teacher Class for testing class Teacher{ String
subject;//2. Define member variable account , Take effect globally //6. Define construction code block { /** The location where the code block is constructed : Inside class and outside method * effect : It is used to extract common content in construction method *
Timing of implementation : Prior to constructor execution */ subject = "Java Peiyou "; System.out.println(" Construct code block "); } //3. Define construction method
//3.1 Create a parameterless construction of this class public Teacher() { System.out.println(" I am Teacher Nonparametric construction of classes "+subject);
} //3.2 Create the parametric construction of this class public Teacher(String n) { System.out.println(" I am Teacher Parametric construction of classes "
+n+subject); } //4. Defining common methods // Method definition : Modifier return type Method name ( parameter list ){ Method body ...} public void study()
{ //7. Create local code block /** Location of local code blocks : In the method * effect : Scope of control variables ( The smaller the range of action, the better , Because smaller is safer ) * Timing of implementation : When calling this method */ {
int i = 10; System.out.println(i); } //System.out.println(i);// report errors , Because beyond i Scope of action
System.out.println(" Preparing lessons ..."); } }
<>3 this
<>3.1 concept
this A reference object that represents an object of this class
<>3.2 form
<>3.3 practice : this Use the same variable name in the exercise
Using the package : cn.tedu.oop
Usage class : TestVariableThis1.java
package cn.tedu.oop; /** This class is used for this test * When there are two variables with the same name in a class , A member variable , A local variable *
When you want to use a member variable of this class , You can use it this Specify it * this That's what it stands for " This category ", The member variable belongs to the class * */ public class
TestVariableThis1 { public static void main(String[] args) { Cat c = new Cat();
c.eat(); } } //1. establish Cat class class Cat{ int sum = 20; int s = 30; public void eat() {
int sum = 10;// local variable System.out.println(sum);//10, Local variables are used sum, Principle of proximity System.out.
println(s); // Can pass this Keyword to call a member variable , premise : When a member variable has the same name as a local variable
// If not used this appoint , So print 10, Because of the principle of variable proximity , Local variables are used sum System.out.println(this.sum);
// adopt this. Member variables of this class are accessed sum } }
<>3.4 practice :this Call between construction methods of exercises
create package : cn.tedu.oop
Create class : TestConsThis2.java
package cn.tedu.oop; /** This class is in progress this test 2*/ public class TestConsThis2 { public
static void main(String[] args) { // The constructor is called automatically when the object is created Dog d = new Dog(); Dog d2 =
new Dog(" Xiaowangwang "); } } /** * this You can also call each other between construction methods * But please pay attention : It's one-way , It's not a two-way round-trip call , It's a dead cycle * */
class Dog{ // Nonparametric structure public Dog() { /** In nonparametric construction Call the function of construction with parameters */
/** regulations :this The keyword must be on the first line of the constructor */ //this(" Wangcai "); System.out.println(" Nonparametric structure "); } // Parametric structure
public Dog(String s) { /** In parametric structures Call the function of nonparametric construction */ /** regulations :this The keyword must be on the first line of the constructor */ this();
System.out.println(" Parametric structure "+s); } }
Technology