Home> Java> javaTutorial> body text

Try This Improve the Queue class

WBOY
Release: 2024-08-24 06:47:32
Original
671 people have browsed it

Tente Isto  Melhore a classe Queue

You can use theprivatemodifier to make an important improvement to theQueueclass developed in the Try This 5-2 section of Chapter 5. In that version, all members of the Queue class use 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 theputlocandgetlocindexes, thus tampering with the queue. Fortunately, these types of problems are easy to avoid by applying theprivate.

specifier

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

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

Apply the 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.

The above is the detailed content of Try This Improve the Queue class. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!