1. Classes and objects
1.1 class
- Class is an abstraction with the same properties and behaviors in real life
- Class is the type of object , Is a collection with the same properties and behaviors
- Class consists of two parts: attribute and behavior
1.2 Class definition
Format :
public class Class name {
Permission modifier type Property name 1;
Permission modifier type Property name 2;
......
Permission modifier return type /void Method name ( Parameter type Parameters ,...){
...
}
}
Example :
public class User {
private String name;
private int age;
public void sleep() {
System.out.println(this.name + " and sleep.");
}
}
1.3 Create the object and use
Create objects
Class name Object name = new Class name ();
The above just creates the object , Object needs to be used after it is created :
Use attributes :
Object name . Property name ;
Use behavior :
Object name . Method name ();
Example :
public class ClassTest1 {
public static void main(String[] args) {
User user = new User();
user.name = " Zhang San ";
user.age = 18;
System.out.println(user.name);
user.sleep();
}
}
1.4 Assign values to properties
In a normal class , Property is private , Assignment and access are implemented through methods . Examples are as follows :
public class Dog {
private String name;
private int age;
public void sleep() {
System.out.println(this.name + " and sleep.");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
The properties can be obtained through
Object name .getName()
You can set properties through
Object name .setName("123123")
1.5 Construction method
In the process of property setting , You can also use the construction method
Format :
public Class name ( parameter list ){
}
Example :
public class Dog {
private String name;
private int age;
// Construction method
public Dog() {
}
// Parametric construction method
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public void sleep() {
System.out.println(this.name + " and sleep.");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
- The construction method is a special method , The program has a default construction method . When not created, there will be a default constructor , Once the default constructor is created, it will be replaced .
- In theory, the default construction is not required after the construction method is created , However, the default construction method is used in many frameworks , Therefore, it is recommended to create a default constructor after each custom constructor .
1.6 Member variables and local variables
- Class
Member variables : Class
local variable : Method class - In the memory
Member variables : In the team
local variable : In the stack - Life cycle
Member variables : With object
local variable : With the method - initialize value
Member variables : There are default initialization values
local variable : No default , Initialization value must be defined before using
2. encapsulation
- Encapsulation is one of the three characteristics of object-oriented
- Object oriented is the simulation of the objective world , The objective world cannot manipulate the internal members of objects , And direct operation is also unsafe .
- You can set the member variable to private, Provide special ways to access , Reduce risk .
- Improve security and reusability through methods
- this Keywords represent objects , Which object calls , It's what object .
3. Class creation details
3.1 Create a class ( Access security )
Example :
public class Student {
private String name;
private int age;
private String sex;
public Student() {
}
public Student(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
3.2 Class creation process
public class Main {
public static void main(String[] args) {
Student student = new Student(" Zhang San ", 20, " male ");
Student student1 = new Student();
student1.setName(" Li Si ");
student1.setAge(18);
student1.setSex("nv");
}
}
Student Is a class ,student and student1 It's a single object , In other words, all school students are collectively referred to as Student, But specific students in a class ( For example, Zhang San ) It's the object . Abstracting things with the same properties and behavior is class , Specific to an individual is the object .
This chapter ends , For personal learning and introduction to Xiaobai , Bosses do not spray !
Source code 【GitHub】 【 Code cloud 】