Classes and objects
Relationships between classes and objects
The understanding of the class
A class is an abstraction of a class of things in real life that share common properties and behavior
A class is the data type of an object. A class is a collection of objects with the same properties and behavior
Simple understanding: a class is a description of something real
The composition of the class
Attribute: Refers to the characteristics of something, such as a mobile phone (brand, price, size)
Behavior: The actions that something can perform, e.g., something on a phone (making a call, sending a text message)
Relationships between classes and objects
A class is a description of a thing, and an object is a concrete thing
The definition of a class
2. To represent (a variable outside a method in a class) by a member variable in a class.
Behavior: Represented by member methods in a class (remove the static keyword compared to the previous method)
public class Student { String name; // int age; Public void study(){system.out.println (" learn "); } // Member methods}Copy the code
Object creation and use
Create the format of the object
Class name Object name = new class name ();
Student stu = new Student();
Copy the code
The format of the call member
Object name. Member variable
Object name. Member method ();
System.out.println(stu.name); // null System.out.println(stu.age); / / 0Copy the code
case
Demo1.java
package com.gonghr.demo1; public class Demo1 { public static void main(String[] args) { Demo2 Demo=new Demo2(); Demo.show(); }}Copy the code
Demo2.java
package com.gonghr.demo1; public class Demo2 { public void show() { System.out.println("haha"); }}Copy the code
Object memory map
When multiple object references refer to the same memory space (the address values recorded by variables are the same)
Any object that modifies the data in memory is the modified data, regardless of which object is subsequently used to retrieve the data
Member variables and local variables
encapsulation
Private key
A member modified by private can only be accessed in this class. If a member variable modified by private needs to be used by other classes, the corresponding operation is provided
Provides a “get variable name ()” method to get the value of a member variable, decorated with public
Provides a “set variable name (parameter)” method that sets the value of a member variable. The method is public
private String name;
private int age;
Copy the code
This keyword
The this modifier is used to refer to member variables. Its main function is to distinguish local variables from member variables.
Is a reference to the current object (an object)
public void setName(String name) {
this.name = name;
}
Copy the code
Packaging ideas
1. Encapsulate the overview
Is one of the three main characteristics of object orientation (encapsulation, inheritance, polymorphism)
Object oriented programming language is the simulation of the objective world, in the objective world member variables are hidden inside the object, the outside world can not be directly operated
2. Encapsulation principle
Some information of a class is hidden inside the class, and external programs are not allowed to access it directly. Instead, methods provided by the class are used to operate and access the hidden information
The member variable private provides the corresponding getXxx()/setXxx() methods
3. Encapsulate benefits
Methods to control the operation of member variables improve code security
The method encapsulates the code and improves the reusability of the code
A constructor
Constructor format and execution timing
Format notice
The method name must be the same as the class name and case must be the same
There is no return value type, not even void
No concrete return value (cannot be returned by retrun)
Execution time
Called when an object is created, and the constructor is executed each time an object is created
Constructors cannot be called manually
class Student { private String name; private int age; Public Student() {system.out.println (" no argument constructor "); } public void show() { System.out.println(name + "," + age); }}Copy the code
public class StudentDemo {
public static void main(String[] args) {
// Create an object
Student s = new Student();
s.show();
}
}
The role of constructors
Used to initialize the data (attributes) of an object
public Student(String name, int age){ this.name = name; this.age = age; System.out.println(" I am the constructor of the Student class "); }Copy the code
Stu1 = new Student("张三", 20); stu1 = new Student("张三", 20); stu1.show();Copy the code
Constructor considerations
Constructor creation
If no constructor is defined, a default no-argument constructor is given
If a constructor is defined, the system will no longer provide the default constructor
Constructor creation
If no constructor is defined, a default no-argument constructor is given. If a constructor is defined,
The system will no longer provide the default constructor
Recommended usage
Both no-argument constructors and parameterized constructors are written manually, whether used or not
JavaBean Class Quick Constructor (IDEA)
Javabeans are mainly used to transfer data, that is, to combine a group of data into a JavaBean for easy transmission.
The characteristics of
Provides a default constructor and a parameter constructor
Provide get and set functions \ for all private members of a class
Specific operation
Write the basic framework
public class Demo{ private int age; private String name; / /... All private members}Copy the code
Right-click Generate or use Alt + Insert
Constructor: You can customize both no-parameter constructors and parameter constructors
Getter and Setter: You can create custom get and set functions on private data members
The corresponding function is then automatically generated