This article is a supplement to the content of a Java topic. You are welcome to watch and comment on Xiaobian's blog. If you like Xiaobian's blog, don't forget to pay attention to and collect it!
catalogue
What exactly does new do to objects?
Constructor (usually under class attribute)
this always points to the "current instance object" that calls it
setter and getter methods to view and modify properties
What exactly does new do to objects?
When Java creates a new object, it will first check whether the class of the object is loaded into memory. If not, it will be loaded into memory through the name. After loading and initialization, create.
Process of creating objects
1. Allocating objects in the heap requires memory.
2. It is the default value for all instance variables,
3. Execute the construction method (the class is loaded into memory and placed in the method area)
4. Define the reference variable in the stack area, and then assign the object address in the heap to the reference variable.
Constructor (usually under class attribute)
Constructors are sometimes called construction methods. When we create an object with new, the jvm will give us a constructor (a constructor without parameters and method body) for nothing. If we write it, we won't send it.
Nonparametric structure
import java.util.Objects; public class Student implements Comparable<Student> { //full name private String name; //Gender private String sex; //Age private int age; //fraction private double score; public Student() { //Nonparametric structure } }
Parametric structure
import java.util.Objects; public class Student implements Comparable<Student> { //full name private String name; //Gender private String sex; //Age private int age; //fraction private double score; public Student(String name, String sex, int age, double soore) { //Parametric structure this.name = name; this.sex = sex; this.age = age; this.score = score; } }
this keyword
this always points to the "current instance object" that calls it
Important: each method will pass in a variable this by default, which always points to the "current instance object" calling it. (this is the of the method)
When the class is not called, this does not know who it points to
This method accesses the constructor (one constructor accesses another constructor, this when the constructor can only be placed on the first line)
import java.util.Objects; public class Student implements Comparable<Student> { //full name private String name; //Gender private String sex; //Age private int age; //fraction private double score; public Student() { //Nonparametric structure this("xiaoming","male",18,88); //Accessing parameterized constructs using nonparametric constructs System.out.println("this When the constructor can only be placed on the first line"); } public Student(String name, String sex, int age, double soore) { //Parametric structure this.name = name; this.sex = sex; this.age = age; this.score = score; } }
setter and getter methods
When you find that the properties are privatized, the security of the program is improved, but what do you do when you need to modify and view the requirements of the properties?
At this time, we need to use methods to modify and view methods.
setter and getter methods to view and modify properties
get method is used to obtain the properties of the object (you can format data as required)
The set method modifies the properties of the object uniformly (limited to the parameters passed in by the verification method and hiding the internal data structure of the object)
import java.util.Objects; public class Student implements Comparable<Student> { //full name private String name; //Gender private String sex; //Age private int age; //fraction private double score; //Override the comparison rule of TreeSet @Override public int compareTo(Student other){ return this.age-other.age; } public Student() { //Nonparametric structure this("xiaoming","male",18,88); System.out.println("this When the constructor can only be placed on the first line"); } public Student(String name, String sex, int age, double soore) { //Parametric structure this.name = name; this.sex = sex; this.age = age; this.score = score; } //Generate get method public String getName() { return name; } public String getSex() { return sex; } public int getAge() { return age; } public double getScore() { return score; } //Generate set method public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public void setAge(int age) { this.age = age; } public void setScore(double score) { this.score = score; } }
A complete Java Bean
JavaBean s are JAVA language Reusable components written as. To be written as a JavaBean, a class must be concrete, public, and parameterless constructor . JavaBeans expose the internal domain to member properties by providing public methods that conform to the consistent design pattern, and get the set and get methods. As we all know, attribute names conform to this pattern. Other Java classes can discover and manipulate the attributes of these JavaBeans through introspection mechanism (reflection mechanism).
import java.util.Objects; //A complete javaBean public class Student implements Comparable<Student> { //full name private String name; //Gender private String sex; //Age private int age; //fraction private double score; public Student() { //Nonparametric structure this("xiaoming","male",18,88); System.out.println("this When the constructor can only be placed on the first line"); } public Student(String name, String sex, int age, double soore) { //Parametric structure this.name = name; this.sex = sex; this.age = age; this.score = score; } //Generate get method public String getName() { return name; } public String getSex() { return sex; } public int getAge() { return age; } public double getScore() { return score; } //Generate set method public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public void setAge(int age) { this.age = age; } public void setScore(double score) { this.score = score; } //Generate toString method @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + ", score=" + score + '}'; } }