Including: introduction to the two systems of classes, encapsulation, private keywords, this keyword, construction methods, standard JavaBeans and keyboard input
Class and object basics
kind
Classes are composed of two parts: attributes and behaviors.
- Attributes: reflected in the class through member variables (variables outside the methods in the class)
- Behavior: reflected in the class through member methods (compared to the previous method, just remove the static keyword)
Class definition steps:
①Define class
②Write member variables of the class
③Write member methods of the class
public class 类名 {
// 成员变量
变量1的数据类型 变量1;
变量2的数据类型 变量2;
…
// 成员方法
方法1;
方法2;
}
real column
/*
手机类:
类名:
手机(Phone)
成员变量:
品牌(brand)
价格(price)
成员方法:
打电话(call)
发短信(sendMessage)
*/
public class Phone {
//成员变量
String brand;
int price;
//成员方法
public void call() {
System.out.println("打电话");
}
public void sendMessage() {
System.out.println("发短信");
}
}
Demo
Create a mobile phone class
Create an object of Xiaomi mobile phone
encapsulation
-
Overview of encapsulation
is one of the three major features of object-oriented (encapsulation, inheritance, polymorphism)Whatever the object represents, the corresponding data must be encapsulated and the behavior corresponding to the data must be provided.
-
Encapsulation code implementation
hides some information of the class inside the class and does not allow direct access by external programs. Instead, the methods provided by the class are used to operate the hidden information and access
private member variables, and provide the corresponding getXxx()/setXxx ()method
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. For member variables modified by private, if they need to be used by other classes, corresponding operations are provided.
- Provide the "get variable name()" method to obtain the value of the member variable. The method is decorated with public
- Provides the "set variable name (parameter)" method for setting the value of member variables. The method is modified with public
Code display
public class GirlFriend {
//属性
private String name;
private int age;
private String gender;
//针对每一个私有化的成员变量,都要提供get和set方法
//使用set方法给成员变量赋值
//使用get方法获取成员变量的值
//给成员变量name进行赋值
//没有返回值,用形参name接受
public void setName(String name){
//局部变量表示测试类中调用方法传递来的数据
//把传递过来的name赋值给等号左边表示成员位置的name
this.name = name;
}
//对外提供name的属性
public String getName(){
return name;
}
//age
//set
public void setAge(int age){
if (age>=18 && age<=50){
this.age = age;
}else {
System.out.println("非法参数");
}
}
//get
public int getAge(){
return age;
}
//gender
//set
public void setGender(String gender){
this.gender = gender;
}
//get
public String getGender(){
return gender;
}
//行为
public void sleep() {
System.out.println("她在休息");
}
public void eat() {
System.out.println("她在吃饭");
}
}
test code
public class GirlFriendTest {
public static void main(String[] args) {
//创建GirlFriend的对象
GirlFriend gf1 = new GirlFriend();
//用private、set和get后的赋值不能再用下面操做,需要重新更改
//gf1.name = "小诗是";
//gf1.age = 18;
//gf1.gender = "女";
//System.out.println(gf1.name);
//System.out.println(gf1.age);
//System.out.println(gf1.gender);
//gf1.eat();
//gf1.sleep();
//赋值
gf1.setName("林青霞");
gf1.setAge(18);
gf1.setGender("女");
System.out.println(gf1.getName());
System.out.println(gf1.getAge());
System.out.println(gf1.getGender());
gf1.eat();
gf1.sleep();
}
}
operation result
this keyword
- Variables modified by this are used to refer to member variables, and their main function is to distinguish the problem of duplicate names of local variables and member variables.
- If the formal parameter of a method has the same name as a member variable, the variable without this modification refers to the formal parameter, not the member variable.
- The formal parameter of the method does not have the same name as the member variable. The variable without this modification refers to the member variable.
The principle of proximity
Construction method
A constructor is a special method
- Function: Create object Student stu = new Student();
- When creating an object, the virtual machine automatically calls the constructor method, which is used to initialize member variables.
The constructor has no return value and cannot be written to void.
Creating the object is done by new, which is just one of the steps in the constructor.
Standard JavaBeans
Shortcut key
Alt+INS
click Constructor, then
click on the red part, empty parameters will appear.
Press Shift and click on the last line to select all, and then all structures with parameters will appear. Use
the same method to select
and then click Ctrl+A to select all, and then all will be selected. All parameters appear get and set.
Finally, you can install the plug-in ptg to generate it with one click.
After setting the properties of the class
, right-click on the blank space and select the red part to generate it with one click.
Two systems for keyboard entry
- The first system:
nextInt(); accepts integers
nextDouble(); accepts decimals
next(); accepts strings.
This system will notify acceptance when encountering spaces, tabs and carriage returns. Data following these symbols will not be accepted.
- The second system
nextLine(); accepts string
Generally, the two systems should not be used together.
TIPS
In idea, ctrl+d can copy the previous line.
Formatting code: Ctrl+Alt+L
Car car = arr[i];//先输入右边,再按ctrl + alt + v 可自动生成左边
The following operations can view two code pages at the same time