1. Classes and objects
1.1 what is the object?
Everything is an object, and everything that exists objectively is an object
1.2 what is object-oriented?
The action of paying attention to objects is called object-oriented
For example, I'm going to the supermarket to buy fruit. Fruit is an object. I pay attention to its type, size, acidity and sweetness. This process is called object-oriented
1.3 what are classes?
Class is an abstraction of a class of things with the same attributes and behaviors in real life.
Characteristics of class:
1. Class is the data type of object
2. A class is a set of collections with the same attributes and behaviors
1.4 what are the attributes of an object?
Attribute: various features of an object. Each feature of each object has a specific value.
1.5 what is the behavior of the object?
Behavior: what an object can do
1.6 relationship between class and object
Class: it is the abstraction of things with the same attributes and behaviors in real life
Object: an entity that can be seen and touched
A class is an abstraction of an object
An object is an entity of a class
1.7 definition of class
Importance of class: it is the basic unit of Java program
Class composition: properties and behavior
Attribute: represented by member variables in the class (variables defined outside the method)
Behavior: use member methods in the class (methods without static keyword)
Class definition steps:
① Define class
② Write the member variable of the class
③ Write member methods of classes
For example:
Mobile phone class: Class Name: mobile phone
Member variables: brand, price
Member method: call and send text messages
public class phone { //Write the member variable of the class String brand; int price; //Member method public void call(){ System.out.println("phone"); } public void sentMessage(){ System.out.println("send message"); } }
1.8 use of objects
① Create object
Format: class name object name = new class name ();
Example: phone p =new phone();
② Use object
(1) Assign values to member variables:
Format: object name Member variable = variable value;
Example: p.brand = "Huawei";
(2) Call member method
Format: object name Membership method;
Example: p.call();
Case: Students
Requirements: first define a student class, then define a student test class, and complete the use of member variables and member methods through objects in the student test class
Analysis: member variables: name, age
Member method: study and do homework
public class student { //Define member variables String name; int age; //Define member methods public void study(){ System.out.println("I love learning."); } public void doHomework(){ System.out.println("I'm doing my homework"); } }
/* Student testing */ public class studentDemo { public static void main(String[] args) { //create object student s=new student(); //Using member variables System.out.println(s.name); System.out.println(s.age); s.name = "Xiaobai"; s.age = 22; System.out.println(s.name); System.out.println(s.age); //Use member method s.study(); s.doHomework(); } }
2. Member variables and local variables
2.1 what are member variables and local variables?
Member variable: refers to the variable outside the method defined in the class
Local variables: variables in methods
2.2 differences between member variables and local variables
3. Packaging
3.1 private keyword
① Is a permission modifier
② Ability to modify members (member methods and member variables)
③ The function is to protect members from being used by other classes. Members modified by private class can only be accessed in this class
For members modified by private, if they need to be used by other classes, set variable name () and get variable name () methods need to be used
① The set variable name () method is used to set the value of the member variable. The method is decorated with public
② The get variable name () method is used to obtain the value of the member variable. The method is decorated with public
3.2 use of private keyword
/* Student class */ public class student { //Define member variables private int age; String name; //Define set method public void setAge(int a){ if(a<0||a>120){ System.out.println("The age you entered is incorrect"); }else{ age=a; } } //Define get method public int getAge(){ return age; } //Define member methods public void show(){ System.out.println(name+","+age); } }
/* Student testing */ public class studentTest { public static void main(String[] args) { //create object student s = new student(); //Use member variables and assign values // s.age=30; // s.age=-30; s.name="Xue You Zhang"; //Use member method s.setAge(30); // s.setAge(-30); s.show(); } }
3.3 this keyword
① The variable modified by this keyword is a member variable
② When to use member variables?
When the member variable and the local variable have the same name, the local variable is solved to hide the member variable.
3.4 packaging
① Encapsulation concept
Is one of the three characteristics of object-oriented (encapsulation, inheritance, polymorphism)
The member variables and member methods composed of the objective world are hidden inside the object and cannot be directly accessed by the outside world
② Encapsulation principle
Some information of the class is hidden inside and cannot be accessed directly by external programs. Members modified by private can only be accessed through the methods provided by the class, such as setXXX() and getXXX() methods
3. Packaging benefits
The operation of members is controlled by methods, which improves the security of the code
The code is encapsulated to improve the reusability of the code
4. Construction method
4.1 overview of construction method
To: create objects
Function: complete the initialization of the object
Format: modifier class name (parameter) {}
Modifiers are generally public
4.2 precautions for construction method
1. When there is no custom construction method, the system will give a parameter free construction method
2. When the construction method is given, the nonparametric construction method will disappear
3. If the construction method with parameters is given, if the construction method without parameters is required, it needs to be defined by itself
4. Write a parameterless construction method manually whether it is used or not
4.3 production of standards
1. Member variables are decorated with private
2. Construction method provides a parameter free construction method and a construction method with multiple parameters 3 The member method provides the setxxx()/getxxx() method corresponding to each member variable, and provides a show() method that displays object information
4. There are two ways to create an object and assign values to its member variables. After creating an object, use setxxx() to assign values, and use the construction method with parameters to directly create an object with attribute values
/* Student class */ public class student { //Member variable private int age; private int weight; private String name; private String classroom; //Construction method public student(){ } public student(int age,int weight,String name,String classroom){ this.age=age; this.weight=weight; this.name=name; this.classroom=classroom; } //The member method setxxx() modifies the variable value public void setAge(int age){ this.age=age; } public void setName(String name){ this.name=name; } public void setWeight(int weight){ this.weight=weight; } public void setClassroom(String classroom){ this.classroom=classroom; } //The member method getxxx () gets the variable value public int getAge(){ return age; } public String getName(){ return name; } public int getWeight(){ return weight; } public String getClassroom(){ return classroom; } //Create a show () method public void show(){ System.out.println(name+","+age+","+weight+","+classroom); } }
/* Student testing */ public class studentTest { public static void main(String[] args) { //create object student s1=new student(); student s2 =new student(30,60,"Xiaobai","Six classes in three years"); //Assign values to nonparametric objects s1.setAge(30); s1.setName("Xiao Gang"); s1.setWeight(80); s1.setClassroom("Class two of three years"); //Call the show() method s1.show(); s2.show(); } }