catalogue
1.1 understanding of classes and objects
2. Member variables and local variables
3.1. Overview of construction methods
3.2. Precautions for construction method
3.3 how to call another class in a class.
introduction
Hello, my friends. The article I brought you today is object-oriented and encapsulation foundation. I hope it will be helpful to you who are beginning to learn
1, Classes and objects
1.1 understanding of classes and objects
Class
Class understanding
* class is an abstraction of a class of things with common attributes and behaviors in real life
* a class is the data type of an object, and a class is a collection of objects with the same attributes and behaviors
* simple understanding: class is a description of real things
Composition of classes
* attribute: refers to the characteristics of things, such as mobile phone things (brand, price, size)
* behavior: refers to the operations that things can perform, such as mobile phone things (making phone calls and sending text messages)
Relationship between classes and objects
Class
Class is an abstraction of a class of things with common attributes and behaviors in real life
Object
It is a real entity that can be seen and touched
Simple understanding
Class is a description of things, and object is a concrete thing
How to define classes?
1.2. Definition of class 1
There are three steps
To define a class
① definition class
② write the member variables of the class
③ write class member methods
The format is as follows
public class Class name { // Member variable Data type of variable 1; Data type of variable 2; ... // Member method Method 1; Method 2; }
Sample code
/* Mobile phone category: Class name: Mobile (Phone) Member variables: Brand Price Member method: Call Send SMS (sendMessage) */ public class Phone { //Member variable String brand; int price; //Member method public void call() { System.out.println("phone"); } public void sendMessage() { System.out.println("send message"); } }
After defining the class, we naturally want to use it. How to use it?
1.2. 2 use of objects
Format for creating objects
Class name object name = new class name ();
Format of calling member
Object name Member variable
Object name Member method ();
Sample code
/* create object Format: class name object name = new class name (); Example: Phone p = new Phone(); Use object 1: Using member variables Format: object name Variable name Example: p.brand 2: Using member methods Format: object name Method name () Example: p.call() */ public class PhoneDemo { public static void main(String[] args) { //create object Phone p = new Phone(); //Using member variables System.out.println(p.brand); System.out.println(p.price); p.brand = "millet"; p.price = 2999; System.out.println(p.brand); System.out.println(p.price); //Using member methods p.call(); p.sendMessage(); } }
2. Member variables and local variables
After talking about the definition and use of classes, let's talk about two other concepts: member variables and local variables
Definition
Member variable
Member variables are defined outside methods in the class
Local variable
Local variables are defined in methods
Distinction
* different positions in the class: member variable (outside the method in the class) local variable (inside the method or on the method declaration)
* different locations in memory: member variables (heap memory) local variables (stack memory)
* different life cycles: member variables (exist with the existence of the object and disappear with the disappearance of the object) local variables (exist with the call of the method and disappear after the call of the method)
* different initialization values: member variable (with default initialization value) local variable (without default initialization value, it must be defined before assignment)
One picture is done
II. Packaging
Before talking about encapsulation, let's learn about two keywords, private and this
2.1private
private keyword
private is a modifier that can be used to modify members (member variables, member methods)
Members modified by private can only be accessed in this class. If private modified member variables need to be used by other classes, corresponding operations are provided
* the "get variable name ()" method is provided to obtain the value of the member variable. The method is decorated with public
* the "set variable name (parameter)" method is provided to set the value of the member variable. The method is decorated with public
Sample code
/* Student class */ class Student { //Member variable String name; private int age; //Provide get/set methods public void setAge(int a) { if(a<0 || a>120) { System.out.println("You gave the wrong age"); } else { age = a; } } public int getAge() { return age; } //Member method public void show() { System.out.println(name + "," + age); } } /* Student testing */ public class StudentDemo { public static void main(String[] args) { //create object Student s = new Student(); //Assign values to member variables s.name = "Lin Qingxia"; s.setAge(30); //Call the show method s.show(); } }
2.2.this keyword
The variable modified by this is used to refer to member variables. Its main function is to distinguish the problem of duplicate names of local variables and member variables
* if the formal parameter of the method has the same name as the member variable, the variable without this modifier refers to the formal parameter rather than the member variable
*The formal parameter of the method does not have the same name as the member variable. The variable without this modifier refers to the member variable
Sample code
public class Student { private String name; private int age; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void show() { System.out.println(name + "," + age); } }
Memory principle of this
Conclusion
This represents the reference of the current calling method, the method of which object is invoked, and which object this represents.
Sample code
public class StudentDemo { public static void main(String[] args) { Student s1 = new Student(); s1.setName("Lin Qingxia"); Student s2 = new Student(); s2.setName("Zhang Manyu"); } }
this() method
this() represents the call constructor method. This call can only be used in the constructor method, that is to call the constructor method to construct the method this (argument).
1. this() and this (argument) must be placed in the first line of the construction method
public class Constractor { int year; int month; int day; //Nonparametric construction method public Constractor() { this(2019,1,1); } //Parametric construction method Constractor(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } }
2, in parametric construction method, we call this() without parameter construction. In the parametric construction method, we call the construction method with parameters (this).
public class Constractor { int year; int month; int day; //Nonparametric construction method public Constractor() { } //Parametric construction method Constractor(int year, int month, int day) { this(); this.year = year; this.month = month; this.day = day; } }
2.3. Encapsulation idea
1. Encapsulation principle
Some information of the class is hidden inside the class, which is not allowed to be directly accessed by external programs. Instead, the operation and access of hidden information are realized through the methods provided by the class
The member variable private provides the corresponding getXxx()/setXxx() methods
2. Packaging benefits
Methods are used to control the operation of member variables, which improves the security of the code
The code is encapsulated by method, which improves the reusability of the code
3, Construction method
3.1. Overview of construction methods
Definition
Construction method is a special method
Function
Create object
Student s = new Student();
Format
public class Class name{ Modifier class name( parameter ) { } }
Function
It mainly completes the initialization of object data
Sample code
class Student { private String name; private int age; //Construction method public Student() { System.out.println("Nonparametric construction method"); } public void show() { System.out.println(name + "," + age); } } /* Test class */ public class StudentDemo { public static void main(String[] args) { //create object Student s = new Student(); s.show(); } }
3.2. Precautions for construction method
Creation of construction method
If no construction method is defined, the system will give a default parameterless construction method
If a construction method is defined, the system will no longer provide the default construction method
Overloading of construction methods
If you customize the construction method with parameters and use the nonparametric construction method, you must write another nonparametric construction method
Recommended usage
Whether used or not, the parameterless construction method is written manually
Important functions
You can initialize member variables using a parameterized construct
Sample code
/* Student class */ class Student { private String name; private int age; public Student() {} public Student(String name) { this.name = name; } public Student(int age) { this.age = age; } public Student(String name,int age) { this.name = name; this.age = age; } public void show() { System.out.println(name + "," + age); } } /* Test class */ public class StudentDemo { public static void main(String[] args) { //create object Student s1 = new Student(); s1.show(); //public Student(String name) Student s2 = new Student("Lin Qingxia"); s2.show(); //public Student(int age) Student s3 = new Student(30); s3.show(); //public Student(String name,int age) Student s4 = new Student("Lin Qingxia",30); s4.show(); } }
3.3 how to call another class in a class.
Format class name (name of another class) method name
Sample code
package HomeworkTwo01; /* 1.Define a Husband husband class with name, age and wife attributes Define a Wife life class with name, age and husband attributes Husband class has a getInfo method, which can display his name and age, and his wife's name and age There is a getInfo method in the wife class, which can display her name and age, and her husband's name and age Define a test class, create wife and husband objects, and then test */ public class Husband { //Member variable private String name; private int age; private Wife wife; //Empty parameter structure public Husband() { } //Parametric structure public Husband(String name, int age, Wife wife) { this.name = name; this.age = age; this.wife = wife; } //set, get method public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setWife(Wife wife) { this.wife = wife; } public Wife getWife() { return wife; } public void getInfo() { System.out.println("full name:" + name + "\n Age:" + age + "\n Wife's name:" + wife.getName() + "\n Wife age" + wife.getAge()); } } class Wife { //Member variable private String name; private int age; private Husband husband; //Empty parameter structure public Wife() { } //Parametric structure public Wife(String name,int age,Husband husband) { this.name = name; this.age = age; this.husband = husband; } //set get method public void setName(String name) { this.name = name ; } public String getName() { return name; } public void setAge(int age ) { this.age = age; } public int getAge() { return age; } public void setHusband(Husband husband) { this.husband = husband; } public Husband getHusband() { return husband; } public void getInfo() { System.out.println("full name:" + name + "\n Age:" + age + "\n Husband's name:" + husband.getName() + "\n Husband age" + husband.getAge()); } }
Use
package HomeworkTwo01; public class Test { public static void main(String[] args) { Husband h = new Husband("Zhang San", 28, null); Wife w = new Wife("Li Meimei", 25, null); h.setWife(w); h.getInfo(); System.out.println("--------------"); w.setHusband(h); w.getInfo(); } }
I will put some examples in the next issue. You can discuss it together. Bye!