A method that installs multiple data files

I am just learning object oriented programming, and this is the purpose I just gave:

"Create a class called Book that contains the stock number, author, title, price and number of pages for the book. Include a method that sets all data files and another that prints values ​​for each data field. Create a class diagram and write a pseudocode that defines class. "

(remember, I am not writing code for a specific language, because I do not know) I created 5 attributes on request:


-stockNumber: num -price: num -pageCount: num -author: string -title: string 

Now I need to create one method that immediately sets all the data. This is what turns me off.

Is there a general way to handle this in one method? or is my teacher wrong, and is it better to have several recruitment methods?

+4
source share
1 answer

A method that sets all data fields at the same time will most likely be a constructor , which in UML is just an operation with the same name as the class (and with all the necessary arguments) that does not have a return type - for example, see the first diagram in this PDF file: http://cs.nyu.edu/courses/spring10/V22.0101-003/CircleUML.pdf .

It [the constructor] prepares a new object for use, often takes parameters that the constructor uses to set the necessary member variables .

A class can have several constructors. Given your use case (in the context of the book inventory management application that I assume), it seems reasonable that an object book require all five attributes at creation time.

However, the designer does not need to set all the attributes. In contrast, several frameworks (such as JavaBeans) explicitly require a default constructor without any arguments.

+4
source

Source: https://habr.com/ru/post/1501376/


All Articles