1. Member variables and local face changes
1.1 What are member variables and local variables
package javadoy5;//包名
//局部变量和成员变量
/**
1.
2.
* */
public class doy55 {
String brand;//名字
double price;//价钱
String color;//颜色
//成员变量
//private
public void call (String who) {
System.out.println("给"+who+"取法");
//局部变量
}
public void sendMessage () {
//局部变量
//int a;
System.out.println("打电话");
}
}
1.2. The difference between member variables and local variables
2. Package
2.1private keyword
-
private is a permission modifier
Can modify members (member variables and member methods)
The function is to protect members from being used by other classes, and members modified by private can only be accessed in this class
For privately modified member variables, if they need to be used by other classes, provide corresponding operations
Provide "get variable name()" method to get the value of member variable, the method is decorated with public
Provide a "set variable name ("parameter") method, used to set the value of a member variable, the method is decorated with public
2.2 The use of private keywords
A standard preparation
Modify member variables with private
Provide the corresponding getXx0/setXx(method
format:
package stent;
public class day6 {
private String name;
private int age;
public day6() {
System.out.println("午餐");
}
public day6(String name,int age) {
System.out.println("午餐");
this.name = name;
this.age = age;
if(age>=0 && age<=150)
this.age = age;
else
System.out.println("假的");
}
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 void show()
{
System.out.println("我叫"+this.name+this.age+"岁");
}
}
2.3this keyword
2.4this memory principle
2.4 Package
1. Package overview
Is one of the three characteristics of object-oriented (questioning, inheritance, polymorphism)
It is an object-oriented programming language that simulates the objective world. The member variables in the objective world are
2. Packaging principles
Some information of the class is hidden inside the class, and no external program is allowed to directly access it. Instead, the member variables are private, and the corresponding getXxx0/setXox0 methods are provided.
3. Packaging benefits
Use methods to control the operation of member variables, improve the security of the code, encapsulate the code with methods, and improve the reusability of the code