"Xin chào, Amigo! Hôm nay bạn sẽ trở thành một chuyên gia đóng gói xuất sắc. Hãy thử các bài tập sau:"
3
Nhiệm vụ
Java Core, mức độ, bài học
Private encapsulated cat
In the lesson on encapsulation, Rishi explained that occasionally several classes will interact with the same object, making the object's data inconsistent. Here we have a cat. We need to make each cat instance retains its integrity, because nobody likes inconsistent cats! Do you know what you need to do?
3
Nhiệm vụ
Java Core, mức độ, bài học
Our cat is too public!
Foes (with crooked upper limbs) don't waste any time testing the robustness of your code, including your Cat class. We'll protect the cat by hiding some of its internal variables - just the ones that will still be accessible via a method. Cats need some way to interact with the outside world after all.
3
Nhiệm vụ
Java Core, mức độ, bài học
The unapproachable cat
The cat on our spaceship is very sensitive. He doesn't want anyone prying into his private life. He prefers the darkness of depression over the company of fun-loving programmers. Let's help hide all the Cat class's internal variables as well as any methods capable of changing the internal state of Cat objects.
3
Nhiệm vụ
Java Core, mức độ, bài học
Like cats and dogs
Cats and dogs aren't the best of friends, but they can get bored without one another. Let's not get in the way of their stormy relationship. We'll leave public the corresponding methods of the Cat and Dog classes. We'll hide the remaining methods as well as the internal variables - from sight and mischievous paws.
3
Nhiệm vụ
Java Core, mức độ, bài học
Don't forget to encapsulate
Encapsulation is kinda like placing an object in a capsule to protect its integrity. When writing this task, we started creating capsules, or special methods, but we forgot to add the fields that needed encapsulating. Study the code and correct our oversight, so the capsules can fulfill their destiny.