Hello, I am Brother Rabbit, let us continue this journey of JavaSE becoming a god today!
In this section, what we are going to learn is Java encapsulation.
1. What is encapsulation
Java encapsulation refers to hiding some important information in a class so that it cannot be directly accessed by the outside.
Java's encapsulation is an object-oriented programming technology. It encapsulates the attributes and methods in the class together, so that the outside can only access the attributes and methods in the class through specific methods, so that the outside cannot directly modify the attributes in the class. and methods to effectively control the internal state of the class.
For example, define a Person class, which has two attributes name and age, we can encapsulate it with the following code:
public class Person{ private String name; private int age; 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; } }
In the above code, the name and age attributes are encapsulated in the Person class, and getter and setter methods are provided for them, so that the outside can only obtain and modify the attributes in the class through these methods, thus effectively controlling the class internal state.
Moreover, we added private modifiers to the name and age attributes, so that the outside world cannot be directly manipulated. If you want to manipulate the name and age attributes, you have to call the corresponding public methods.
To give another example, it is like an ATM machine. The banknotes inside are important information and are private. You cannot directly see how much money is in it and where is it stored?
However, the ATM machine has a card slot, a cash outlet, and a display screen. These components are public methods. You can deposit and withdraw money by inserting a card and entering a password.
This is encapsulation.
2. How to implement encapsulation with Java code
This is the example above, in two steps.
01: Set the attribute to private, representing private.
02: Set the public get/set method to operate private properties.
There are shortcut keys that can be used to quickly generate get/set methods.
In idea:
-
Select the property to generate getter and setter methods, right click and select Generate;
-
Select Getter and Setter in the Generate menu;
-
Check the properties to be generated and click OK to generate getter and setter methods.
In eclipse:
-
Select the property to generate getter and setter methods, right click and select Source;
-
Select Generate Getters and Setters from the Source menu;
-
Check the properties to be generated and click OK to generate getter and setter methods.
3. Last homework
Create a car class Car, which has two attributes: brand and color. Use the constructor with parameters and the constructor without parameters to instantiate the Car class, and write the toString method. Write another test class and test it in the main method of the test class.
Reference answer:
public class Car { private String brand; private String color; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } //parameterless constructor public Car(){ this.brand=""; this.color=""; } //Constructor with parameters public Car(String brand,String color){ this.brand=brand; this.color=color; } //toString method public String toString(){ return "Car[brand="+brand+",color="+color+"]"; } }
Test class:
public class CarTest { public static void main(String[] args) { //Instantiate Car with no-argument constructor Car car1 = new Car(); car1.setBrand("Bmw"); car1.setColor("White"); System.out.println(car1.toString()); //Output: Car[brand=Bmw,color=White] //Instantiate Car using a parameterized constructor Car car2 = new Car("Benz","Black"); System.out.println(car2.toString()); //Output: Car[brand=Benz,color=Black] } }
4. Homework
It is required to reflect the idea of encapsulation:
An ATM machine has a savings account, please write a Java class Account, which requires:
(1) Define a member variable: int balance, indicating the account balance;
(2) Define the construction method and initialize the account balance;
(3) Define the deposit method void deposit(int amount), the input parameter is the deposited amount, and the account balance increases by amount;
(4) Define the withdrawal method void withdraw(int amount), the input parameter is the withdrawn amount, the account balance is reduced by amount, if the balance is insufficient, it will prompt "insufficient balance";
(5) Define the query method int getBalance() to return the account balance;