{"id":313,"date":"2023-05-11T09:38:48","date_gmt":"2023-05-11T07:38:48","guid":{"rendered":"https:\/\/oopm.org\/?page_id=313"},"modified":"2025-02-05T14:31:30","modified_gmt":"2025-02-05T13:31:30","slug":"3-1-using-class","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=313","title":{"rendered":"3.1 Using class"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages313&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages313&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">Instead of describing each account object separately, we may describe a&nbsp;<em>class<\/em>&nbsp;that defines a template\/pattern for accounts in general as <mark style=\"background-color:rgba(0, 0, 0, 0);color:#1c7817\" class=\"has-inline-color\">i<\/mark>n the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Account:\n   owner: <strong>val<\/strong> \"???\"\n   balance: <strong>var<\/strong> float\n   interestRate: <strong>var<\/strong> float\n   addInterest:\n      balance := balance + (balance * interestRate) \/ 100\n   deposit(amount: <strong>var<\/strong> float):\n      balance := balance + amount\n   withdraw(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float:\n      balance:= balance - amount\n      newB := balance<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The keyword&nbsp;<code><strong>class<\/strong><\/code>&nbsp;specifies that we describe a class as opposed to an object (using&nbsp;<strong><code>obj<\/code><\/strong>).&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may use a class as a template to generate an arbitrary number of objects having the <em>structure<\/em> as described by the class. All objects generated according to class <code>Account<\/code> thus have the attributes that are described in class <code>Account<\/code>. Each <code>Account<\/code> object will thus have data-items <code>owner<\/code> and <code>balance<\/code> and methods <code>addInterest<\/code>, <code>deposit<\/code> and <code>withdraw<\/code>. An <code>Account<\/code><em> <\/em>object<em> <\/em>is also referred to as an <em>instance <\/em>of <code>Account<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Different <code>Account<\/code> objects will typically have different values of the data-items <code>owner<\/code>, <code>balance<\/code> and <code>interestRate<\/code>. The methods in class <code>Account<\/code> may be invoked on different <code>Account<\/code> objects. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We say that these objects are <em><code>Account<\/code> objects<\/em> or <em>instances of <code>Account<\/code><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before we show how to generate <code>Account<\/code> object using class <code>Account<\/code>, we have to fix a problem with the above description of class <code>Account<\/code>. The problem is that the value of&nbsp;<font face=\"monospace\"><code>owner<\/code><\/font> is specified to be the string <code>\"???\"<\/code>. John Smith and Liza Jones and other accounts have different values of <code>owner<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To handle this, we may make&nbsp;<code>owner<\/code>&nbsp;be a parameter of class&nbsp;<code>Account<\/code>. The actual value of <code>owner<\/code> must then be supplied when we create objects from class&nbsp;<code>Account<\/code>. This is similar to how parameters works for methods as described in section <script>mkRef(\"Method with parameter\");<\/script>. Class&nbsp;<code>Account<\/code>&nbsp;with a parameter is shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code><strong>class<\/strong> Account(owner: <strong>va<\/strong>r String):\n   balance: <strong>var<\/strong> float\n   interestRate: <strong>var<\/strong> float\n   addInterest:\n      balance := balance + (balance * interestRate) \/ 100\n   deposit(amount: <strong>var<\/strong> float):\n      balance := balance + amount\n   withdraw(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float:\n      balance:= balance - amount\n      newB := balance<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Data-items specified within the parentheses are the parameters of the class, in this case&nbsp;<code>owner<\/code>. A parameter like <code>owner<\/code> is also a local data-item similar to <code>balance<\/code>. As for methods, the actual value of <code>owner<\/code> may be supplied later as a string literal, see below.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\">We use a <em>class-diagram<\/em> as shown here to illustrate a class &#8211; se chapter <script>mkRef(\"Notation\");<\/script> for an explanation of the form of a class-diagram.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"226\" height=\"211\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/08\/image.png\" alt=\"\" class=\"wp-image-6848\" style=\"width:196px;height:auto\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Class&nbsp;<code>Account<\/code>&nbsp;may be used to create different <code>Account<\/code> objects as in the example below:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code>account_1010: <strong>obj<\/strong> Account(\"John Smith\")\naccount_1022: <strong>obj<\/strong> Account(\"Liza Jones\") \naccount_1025: <strong>obj<\/strong> Account(\"Mary Pole\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The following diagram illustrates the state of these objects after they have been generated:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"818\" height=\"136\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/stateOf3AccountObjects-1.jpg\" alt=\"\" class=\"wp-image-7687\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/stateOf3AccountObjects-1.jpg 818w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/stateOf3AccountObjects-1-300x50.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/stateOf3AccountObjects-1-768x128.jpg 768w\" sizes=\"(max-width: 818px) 100vw, 818px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The objects described in the previous chapter 2 are called singular objects since there is only one of its kind for each of them. Objects generated using a class are called <em>class-defined <\/em>objects. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We of course need to be able to handle more than the three accounts shown above. We show how to do that in chapter <script>mkRef(\"Collections\");<\/script>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The representation of an account by class <code>Account<\/code> as shown does not include all properties of an account needed for a real bank system. In the paper-based version as shown in section <script>mkRef(\"Before computers\");<\/script>, we have an account number, which so far is encoded in the names of the <code>Account<\/code>-objects. In a proper model, it should be represented as an attribute of an <code>Account<\/code>-object. We leave this out to keep the examples small.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Terminology: object-descriptor again<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A class is a name associated with an object-descriptor and in the figure below, the object-descriptor for <code>Account<\/code> is enclosed in a box. An object-descriptor may thus be used to describe a singular object and\/or a <em>class-defined object<\/em> as shown in this chapter. <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"748\" height=\"301\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/DescriptorOfAccount.jpg\" alt=\"\" class=\"wp-image-7325\" style=\"width:758px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/DescriptorOfAccount.jpg 748w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/DescriptorOfAccount-300x121.jpg 300w\" sizes=\"(max-width: 748px) 100vw, 748px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, the object-descriptor includes the parameter of class <code>Account<\/code>.<\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=313\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>Instead of describing each account object separately, we may describe a&nbsp;class&nbsp;that defines a template\/pattern for accounts in general as in the following example: The keyword&nbsp;class&nbsp;specifies that we describe a class as opposed to an object (using&nbsp;obj).&nbsp; We may use a class as a template to generate an arbitrary number of objects having the structure as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":231,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-313","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=313"}],"version-history":[{"count":66,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/313\/revisions"}],"predecessor-version":[{"id":11361,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/313\/revisions\/11361"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/231"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}