Encapsulation: it is mainly used to set the access permission of the member name (class variable) in the class through the private keyword. After using private, the member variable can only be accessed in the current class. If it exceeds the class, the access prompt does not exist. Of course, it can also be used in methods, but less. If you want to access the member variable in other classes, you must indirectly access or set the value of the member variable through the public method.
This: this keyword indicates the object name when the member variable is called by the object name. It is simply understood that compared to the same class, two different objects (object name is different, e.g: object a, object B) are created at the same time. If object a calls the member variable in the object, then this represents object A. similarly, if object B calls the member variable in the object, then this represents object B.
1 package debug; 2 3 import java.util.Scanner; 4 5 class Phone{ 6 private String brand; 7 private int price; 8 private String color; 9 10 public String getBrand() { 11 return brand; 12 } 13 14 public void setBrand(String brand) { 15 this.brand = brand; 16 } 17 18 public int getPrice() { 19 return price; 20 } 21 22 public void setPrice(int price) { 23 this.price = price; 24 } 25 26 public String getColor() { 27 return color; 28 } 29 30 public void setColor(String color) { 31 this.color = color; 32 } 33 } 34 35 36 37 public class Demo13 { 38 public static void main(String[] args) { 39 Scanner sc = new Scanner(System.in); 40 Phone p = new Phone(); 41 System.out.println(p.getBrand() + "-----" + p.getPrice() + "------" + p.getColor()); 42 System.out.println("Please enter your mobile brand name:"); 43 String brand = sc.nextLine(); 44 p.setBrand(brand); 45 System.out.println("Please enter the phone color:"); 46 String color = sc.nextLine(); 47 p.setColor(color); 48 System.out.println("Please enter the price of mobile phone:"); 49 int price = sc.nextInt(); 50 p.setPrice(price); 51 System.out.println(p.getBrand() + "-----" + p.getPrice() + "------" + p.getColor()); 52 } 53 54 }