1. What is class?
Class: a collection of related properties and behaviors. It can be regarded as the template of a kind of things, and the attribute characteristics and behavior characteristics of things are used to describe this kind of things.
In reality, describe a class of things:
Attribute: is the state information of the thing.
Behavior: is what the thing can do. Example: kitten. Attributes: name, weight, age, color. Behavior: walk, run and shout.
What is an object?
Object: it is the concrete embodiment of a kind of things. An object is an instance of a class (an object is not looking for a girlfriend), and it must have the properties and behavior of such things. In reality, an example of a kind of thing: a kitten. Example: a kitten. Attributes: tom, 5kg, 2 years, yellow. Behavior: sneaking around the wall, jumping and running, meowing.
Relationship between class and object:
Class is the description of a class of things, which is abstract.
An object is an instance of a class of things and is concrete.
Class is the template of object, and object is the entity of class.
1.1 definition of class
public class Student { //Member variable String name;//full name int age;//Age //Member method //Learning methods publicvoid study() { System.out.println("study hard and make progress every day"); } //How to eat publicvoid eat() { System.out.println("Learn to eat when you are hungry"); } }
1.2 definition of object
public class Test01_Student { public static void main(String[] args) { //Create object format: class name object name = new class name (); Student s = new Student(); System.out.println("s:"+s); //cn.itcast.Student@100363 //Direct output of member variable values System.out.println("full name:"+s.name); //null System.out.println("Age:"+s.age); //0 System.out.println("‐‐‐‐‐‐‐‐‐‐"); //Assign values to member variables s.name = "Zhao Liying"; s.age = 18; //Output the value of the member variable again System.out.println("full name:"+s.name); //Zhao Liying System.out.println("Age:"+s.age); //18 System.out.println("‐‐‐‐‐‐‐‐‐‐"); //Call member method s.study(); // "Study hard and make progress every day" s.eat(); // "Learn to eat when you're hungry" }
2. Packaging
Overview: object oriented programming language is a simulation of the objective world. In the objective world, member variables are hidden inside the object, and the outside world can not be operated and modified directly. Encapsulation can be considered as a protective barrier to prevent the code and data of this class from being freely accessed by other classes. To access the data of this class, you must use the specified method. Proper encapsulation can make the code easier to understand and maintain, and also strengthen the security of the code.
Principle: hide attributes. If you need to access an attribute, provide public methods to access it.
2.2 steps of packaging
1. Use the private keyword to modify member variables.
2. For the member variables to be accessed, provide a corresponding pair of getXxx methods and setXxx methods. 2.3 encapsulated operation - meaning of private keyword private
1. private is a permission modifier, representing the minimum permission. 2. Member variables and member methods can be modified. 3. Member variables and member methods modified by private can only be accessed in this class.
1. Modify the member variable with private. The code is as follows:
public class Student { private String name; private int age; }
2. Provide getXxx method / setXxx method to access member variables. The code is as follows:
public class Student { private String name; private int age; public void setName(String n) { name = n; } public String getName() { return name; } public void setAge(int a) { age = a; } public int getAge() { return age; } }
this keyword:
Use the variable in this modification method to solve the problem that the member variable is hidden. The code is as follows:
public class Student { private String name; private int age; public void setName(String name) { //name = name; this.name = name; } public String getName() { return name; } public void setAge(int age) { //age = age; this.age = age; } public int getAge() { return age; } }
Construction method:
Define format:
Modifier constructor name(parameter list){ // Method body }
The method name is the same as the class name of the constructor. It doesn't have a return value, so it doesn't need a return value type, or even void. After using the construction method, the code is as follows:
public class Student { private String name; private int age; // Nonparametric construction method public Student() {} // Parametric construction method public Student(String name,int age) { this.name = name; this.age = age; } }
matters needing attention
1. If you do not provide a construction method, the system will give a parameterless construction method.
2. If you provide a construction method, the system will no longer provide a parameterless construction method.
3. The construction method can be overloaded, and parameters can be defined or not.