Java Basics - classes, objects
1. Class
What is a class
A class is a collection of entities with some common characteristics, which is an abstraction of entities with the same characteristics.
Basic syntax for defining classes in Java
Access modifier class name {}
public class Student{ //Defines a student class }
Member variable
A member variable is an attribute owned by an object
Basic grammar
Access modifier [optional modifier] data type variable name;
public class Student{ //Defines a student class public int age; String name; protected long tel; private double a; }
method
Play can be used to describe object actions
Basic grammar
Access modifier [optional modifier] data type method name (parameter list) {method body};
public class Student{ public void print(){ System.out.print("method") } public int sum(int a, int b){ return a + b; } }
2. Object
What is an object
An object is an instance of a class
Object creation
Basic grammar
Type object name = new type ();
public class StudentTest{ public static void main(String[] args){ Student stu = new Student(); } }
3. Overloading of methods
What are overloaded methods
Method overloading means that there are multiple methods with the same name in a class;
These methods follow the following rules
1. Same method name
2. The parameter list of the method is different
1) the number of parameters is different
2) the types of parameters are different, including data types and the order of data types
3) independent of parameter name
3. It has nothing to do with the return value type and access modifier of the method
public class Student{ /** *Define a summation method to sum according to the value of the passed in parameter and return the result */ public int sum(int a,int b){ return a + b; } //The number of parameters is different public int sum(int a,int b,int c){ return a + b + c; } //The type of parameter list is different public int sum(int a,short b){ return a + b; } //The order of parameter list types is different public int sum(short a,int b){ return a + b; } }
4. Constructor
What is a construction method
-
The structure of the construction method is similar to that of the ordinary method, but it is completely different from the ordinary method;
-
The construction method is also a method, which means that when the construction method is used, the method will execute and complete the function
-
A construction method is a code block [a structure. Like a field method, a construction method is a member of a class
-
Construction methods also exist in the structure of the class
Construction method syntax:
-
The name of the constructor is completely consistent with the class name (including case consistency)
-
There is no return value type (nor void)
-
The constructor body cannot return any value (that is, it cannot return a value in the method body)
-
Other methods are similar to ordinary methods. They can have modifiers (public, protected, private, default) and formal parameter lists
-
Construction method must have method body
-
Constructors cannot be modified by any non access modifiers, such as static, final, synchronized, abstract, etc.
Note: the whole of new Student() is not only a Student object, but also the parameterless construction method of the called Student
Characteristics of construction method
-
Characteristics of construction methods in class:
① Each class has at least one constructor;
② If it is not seen (shown), there is an implicit parameterless construction method;
③ If there are explicit constructors in a class, the implicit parameterless constructors do not exist;
Function of construction method
Assign values to the object's member variables while creating the object (initialization)
Template syntax
public class Class name{ Member variable (field); Construction method (with and without parameters); method (A specific functional behavior); }
public class Student{ public String name; public int age; /** Nonparametric structure */ public Student(){} /** Parametric structure */ public Student(String name,int age){ } }
5.this
- this refers to the current object, that is, which object is called refers to which object
2. this * * * * purpose:
-
Solve the ambiguity of local variables and member variables
-
In this class, the mutual calls between construction methods. this() calls the construction method without parameters. This (...) can add parameters, indicating that the construction method with parameters is called
-
This is passed as a parameter and this as a return value
public class Student{ public String name; public int age; /** Nonparametric structure */ public Student(){} /** Parametric structure */ public Student(String name,int age){ this();//Call the constructor, which can only be written in the first sentence this.name = name //Call properties } }
6. Packaging
Encapsulation: it refers to the privatization of members in a class that do not want to be accessed by the outside world.
Function of encapsulation
Encapsulation is to protect the security of internal data:
-
You don't want to access the object's member variables in an external class
-
Only those who meet the permission requirements can access
How to package
- How to control access in the program?
Encapsulation (access control) is realized by adding access right modifiers to members (fields, methods, constructors) in the class
- What is access permission: simply think that access permission is that people at different levels can do things at different levels, and people at different levels can see different pages
step
1. Privatize member variables (modify member variables with private)
2. Provide reasonable for each member variable
**The getXxx() * * method obtains the value of the member variable. If the current member variable type is boolean, change getXxx() to isXxx()
**The setXxx(...) * * method sets the value of the member variable
3. Provide a nonparametric structure and a fully parametric structure
4. This class is decorated with public
public class Student{ private String name; private int age; public String getName(){ return this.name; } public void setName(String name){ this.name = name; } public String getAge(){ return this.age; } public void setAge(String age){ this.age = age; } }