constructor functionRequirements:In order to describe things more accurately, there are some initialized data when many objects are found that correspond to things. How this is done in the class. Done with another small technique in Java: the constructor. The object itself is constructed, and when constructed, it is necessary to do some actions which should be defined within the function.What is the use of constructors (Constructor)? Object creation can be initialized.How does the constructor function in the code? 1, there is no return value type. Because the object is constructed, it ends when it is created and requires a result. Void also do not write, because to have a difference with the general function. 2, the constructor name is consistent with the class name. 3, there is no specific return value. Next, define a constructor in the person class according to the above three points. Once the object is created, it has a name. Found a problem? When the constructor is not learned, the object is created. So does the object have to be constructed? Or is there a call constructor? Yes. Must be called! So what is the constructor in the class?
Class person{ //person () {}// default null argument constructor. } main () {person p = new person ();//This has already called the constructor of the null argument in the person class. //Does the constructor be defined in the class? Some. As long as you define a class, //The constructor that has an empty argument by default in that class is added to the class file when the compiler compiles. //Note: If a constructor is customized in a class, the default null argument constructor compiler is not added. //principle: There is no initialization process defined for the object, and the compiler adds a default initialization process. // If the specified object initialization process is defined, the default is not added. }
details of the constructor:1, there can be multiple constructors in a class, and their existence is manifested in an overloaded form. 2, there is also a return statement in the constructor,used to end the initialization action. 3, can the constructor be modified by private? Can, function: Other programs cannot create objects of this class.
Class person{ Private person () {}} main () {person p = new person ();//Create object cannot initialize. }
What is the difference between a constructor and a general function? 1, the wording is not the same. Not important. 2, there is a difference in running, the corresponding constructor is called when the object is created. The general method is to invoke the desired generic function after the object is created. Q: Do I need a SetName method to initialize the name with the constructor function? Required, because after the object is created, if the data needs to be modified, it can be done by set. 3, the constructor is called only once when the object is created (the initialization action is done only once and executed first). A generic method can be called multiple times by an object.
Class person{ //person () {} void person () {}//This is a general function. But do not write these, because the general function name has a specification. } main () {person p = new Person ();} */class person{ //attribute: Name, age. private String name; private int age; One initialization, no name, no age. Person () { }// person (int a)// {// if (a<0)// return;//Age = a;// } //define constructor, object is created with name. Person (String N) { //assigns the received value to the object's name name = n; } An initialization has both a name and an age. Person (String n,int a) { name = N; age = A; } Behavior. Public Void Speak () { System.out.println ("Name=" +name+ ", age=" +age);} } Class persondemo2{public static void Main (string[] args) {person p1 = new Person (); Person p = new Person ("Lisi");//When the object is created, the corresponding constructor must be called because the object needs to be initialized. // person P2 = new Person ("Wangwu", +);} }
Constructor call plots
Basic data reference data type parameter pass plot
Packaging
Performance:
1, a function is the most basic package.
2, the class is actually a package.
Draw conclusions from the above two points:
Benefits:
1, improve the reusability of the code.
2, hide the implementation details, but also to provide external access to the way. Facilitates the use of callers. This is one of the core, and can be understood as the concept of encapsulation.
3, improved security.
It is also one of the characteristics of object-oriented thinking.
A total of three features: encapsulation, inheritance, polymorphism.
Example: chassis. It hides the details of the card-making device and provides a way to access internal details such as sockets and switches.
*/
Describe people. Person
Attribute: Age.
Behavior: Speak: Say your age.
/*
Summarize:
Content in a class that does not need to be provided externally is privatized, including attributes and behavior.
Selectsort (int[] arr) {Swap (}bubblesort (int[] arr) {Swap (}private swap (int[] Arr,int A,int b) {}
Focus: Describe things later, properties are privatized, and provide setxxx getxxx methods to access them.
*/class person{//Property: Private int age;//age is being modified in order to be privately owned. which is hidden. This is a manifestation of encapsulation. Behavior: void Speak () {System.out.println ("age=" +age); /*
The age has been private, the wrong value can not be assigned, but the correct value can not be assigned, No.
What to do, according to the previous study of the principle of encapsulation, after hiding, but also provide access to the way.
By way of means, let the other program access to, it can be. More importantly, logical judgments can be added to the method.
Remember: There are two actions to access a variable: assign (set), take a value (get get)
So, the way to access private variables is the set variable get variable---Setage Getage
*///defines the method that assigns a value to age. void Setage (int a) {//Joins logical judgment. if (a>0 && a<130) ages = a;else//system.out.println ("Sorry, your Age value" +a+ "is illegal.) //throw New RuntimeException ("Sorry, your Age value" +a+ "is illegal.) ");//Abnormal! Once it appears, the program ends. Need to fix the code. }//defines a method that gets the age value. int Getage () {return age;}} Class Persondemo{public static void Main (string[] args) {//test Person.class. Creates an object. Person p = new person ();/* Assignment-20 is possible because the age property is of type int, but does not conform to real-life things. How to solve this problem? Just don't let it go. How to implement it in code? Need to use a keyword in Java is also a modifier private (private, permission modifier) Remember: The private is only the embodiment of the encapsulation form. *///p.age = -20;//age cannot be accessed directly in a program other than the person class. Demonstrates the embodiment of the age setting and getting method. P.setage ( -20); int a = P.getage (); System.out.println ("a=" +a);//p.speak ();}}
This
/*
How do you fix the constructors when they are called each other?
The constructor is called when the object is initialized.
Which object to initialize? Use the This keyword to record the address of the object. And through this to explicitly initialize the object.
The format of the other constructor is called in the constructor: this (argument list), and the corresponding constructor is called.
Section:
What does this mean? ★★★★★
This represents the object, which object does it represent? Which object invokes this function, which represents which object .
*/class person{private String name;private int age; Person () {}//initializes the name. Private person (String N) {name = n;} Initializes the name and age. Since there is an action to initialize the name. Call it directly. Person (string n,int a) {this (n);// constructor that invokes a string argument. Note: The statement that calls the other constructor,//must be defined in the first row of the constructor. Cause: The initialization action should be performed first. //name = N;age = A;}} Class PersonDemo3 {public static void main (string[] args) {person p = new person ("Lisi", 20); person P1 = new Person ("LISI1", 21);}}
Graphic
/*
The This keyword has another effect:
You can use this to identify which variable is a member variable. This logo can be omitted without writing.
However, when the authority variable and member variable have the same name, this must be used to identify the member variable .
*/
Class person{private String name;private int age; Person (String Name,int age) {this.name = Name;this.age = age;} public void Speak () {System.out.println ("Name=" +this.name+ ", age=" +this.age);} public void Method () {this.speak ();//this can be omitted . }
Practice, define the function and determine whether two people are peers.
1, clear result, Boolean 2, clear unknown content, 1, person type.
public boolean equalsage (person pp) {/*if (pp.age = = this.age)//When the function is used in an object that invokes the function, this object is represented by this. Return True;return False;*///return (pp.age = = this.age)? true:false;return pp.age = This.age;}}
java-Object-Encapsulated-this-constructors