{"id":7757,"date":"2024-09-27T11:23:19","date_gmt":"2024-09-27T09:23:19","guid":{"rendered":"https:\/\/oopm.org\/?page_id=7757"},"modified":"2024-12-13T13:38:22","modified_gmt":"2024-12-13T12:38:22","slug":"8-1-3-example","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=7757","title":{"rendered":"8.1.3 Example"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages7757&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=wpv2pages7757&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\">Here we show an example of writing general <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">code<\/mark> using the extended type rule.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As an example, a customer may have different kinds of accounts. In the example below we add a <code>SavingsAccount<\/code> and a <code>CreditAccount<\/code> to <code>JohnSmithProfile<\/code>. We also add a method <code>totalBalance<\/code> to <code>Customer<\/code> &#8211; this method computes the sum of the balance of all accounts of a given customer.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Customer\n   -\"-\n   totalBalance -&gt; bal: <strong>var<\/strong> float:\n      accounts.scan\n         bal := current.balance\nJohnSmithsProfile: <strong>obj<\/strong> Customer(\"John Smith\")\nanAccount: <strong>ref<\/strong> Account\nanAccount := SavingsAccount(JohnSmithProfile)\nJohnSmitProfile.addAccount(anAccount)\nanAccount.deposit(300)\n...\naCreditAccount: <strong>ref<\/strong> CreditAccount\naCreditAccount := CreditAccount(JohnSmithProfile)\nJohnSmitProfile.addAccount(aCreditAccount)\naCreditAccount.maxCredit := 999\n...\nconsole.print(\"Total balance sum: \" + JohnSmithProfile.totalBalance<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here we see examples of assigning a reference to an object to a variable of a more general type. One example is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anAccount := SavingsAccount(JohnSmithProfile)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The expression <code>SavingsAccount(JohnSmithProfile)<\/code> generates an object of type <code>SavingsAccount<\/code> &#8211; the reference to this object is then assigned to <code>anAccount<\/code> which has the more general type <code>Account<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Another example is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JohnSmitProfile.addAccount(aCreditAccount)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here a reference variable of type <code>CreditAccount<\/code> is assigned to the parameter of <code>addAccount<\/code>, which is of type <code>Account<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The example also shows that it is possible to write code that works for all kinds of <code>Account<\/code> objects. This is the case for the method <code>addAccount<\/code> that adds the <code>Account<\/code> being passed as argument to the <code>accounts<\/code> array of the <code>Customer<\/code> object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Another example is the new method <code>totalBalance<\/code>, which has the statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>accounts.scan\n   bal := current.balance<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This statement scans through all objects in the accounts array &#8211; <code>current<\/code> refers to the current <code>Account<\/code> object, and the type of <code>current<\/code> is <code>Account<\/code>. The expression <code>current.balance<\/code> access the <code>balance<\/code> attribute of the account object bering referred to by <code>current<\/code>. This works since all objects in <code>accounts<\/code> have a <code>balance<\/code> attribute independently of being an instance of <code>Account<\/code>, <code>SavingsAccount<\/code> or <code>CreditAccount<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consider the statement<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aCreditAccount.maxCredit := 999<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here the attribute <code>maxCredit<\/code> is accessed. This is possible since <code>aCreditAccount<\/code> is of type <code>CreditAccount<\/code> and thus must refer to an object of type <code>CreditAccount<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If we had used the variable <code>anAccount<\/code> instead (as we did for generating the <code>SavingsAccount<\/code>), we cannot access the attribute <code>maxCredit<\/code> since only <code>CreditAccounts<\/code> has this attribute.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In section <script>mkRef(\"Virtual methods\")<\/script> on virtual methods, we show how to write general code that depends on objects that are instances of different subclasses.<\/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=7757\" 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>Here we show an example of writing general code using the extended type rule. As an example, a customer may have different kinds of accounts. In the example below we add a SavingsAccount and a CreditAccount to JohnSmithProfile. We also add a method totalBalance to Customer &#8211; this method computes the sum of the balance [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7310,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-7757","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\/7757","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=7757"}],"version-history":[{"count":8,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7757\/revisions"}],"predecessor-version":[{"id":10338,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7757\/revisions\/10338"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7310"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}