"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Try This Improve the Queue class

Try This Improve the Queue class

Published on 2024-11-08
Browse:818

Tente Isto  Melhore a classe Queue

You can use the private modifier to make an important improvement to the Queue class developed in the Try This 5-2 section of Chapter 5. In that version, all members of the Queue class use the default access, which is
basically public. In other words, it would be possible for a program that used Queue to directly access the underlying array, possibly accessing its elements out of order. Since the point of a queue is to provide a “first in, first out” list, it is not desirable to allow out-of-order access. Also
It would be possible for a malicious programmer to change the values ​​stored in the indexes putloc and getloc, thus tampering with the queue. Fortunately, these types of problems are easy to avoid by applying the private.

specifier.

Purpose:
Improve the Queue class from the "Try This 5-2" section by applying the private access modifier to protect the internal members of the class.

Steps for Improvement:
Copy Original Class
Copy the original Queue class from the "Try This 5-2" section to a new file called Queue.java.

Apply private Modifier:
Add the private modifier to the internal members of the Queue class:

  • The array q, which stores the elements of the queue.
  • The putloc and getloc indexes, which manage insertion and removal operations in the queue.
Release Statement This article is reproduced at: https://dev.to/devsjavagirls/tente-isto-6-1-melhore-a-classe-queue-3maf?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3