/ concept-collection / jupyterlite-numbl-kernel
Sign in
concept-collection / jupyterlite-numbl-kernel
jupyterlite-numbl-kernel / demo / content / 09-classes-and-oop.ipynb
105 lines · 3.5 KBCodeBlameHistory
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "id": "oo-0",
6 "metadata": {},
7 "source": "# Classes and object-oriented programming\n\nnumbl supports MATLAB's `classdef` object model: value and handle classes,\nconstructors, methods, inheritance, abstract methods, operator overloading,\nand static methods. Classes are defined in `.m` files next to the notebook\n(you can open and edit them in the file browser)."
8 },
9 {
10 "cell_type": "markdown",
11 "id": "oo-1",
12 "metadata": {},
13 "source": "## A value class with operator overloading\n\n[Vec2.m](./Vec2.m) overloads `+`, `-`, and `*` and defines `norm`."
14 },
15 {
16 "cell_type": "code",
17 "id": "oo-2",
18 "metadata": {},
19 "execution_count": null,
20 "outputs": [],
21 "source": "a = Vec2(3, 4);\nb = Vec2(1, 2);\nfprintf('a + b = %s\\n', char(a + b));\nfprintf('a - b = %s\\n', char(a - b));\nfprintf('3 * a = %s\\n', char(3 * a));\nfprintf('norm(a) = %g\\n', norm(a));"
22 },
23 {
24 "cell_type": "markdown",
25 "id": "oo-3",
26 "metadata": {},
27 "source": "## A handle class has reference semantics\n\n[Account.m](./Account.m) is a `handle` class, so assigning it does not copy:\nboth names refer to the same object."
28 },
29 {
30 "cell_type": "code",
31 "id": "oo-4",
32 "metadata": {},
33 "execution_count": null,
34 "outputs": [],
35 "source": "acct = Account(100);\ndeposit(acct, 50);\nalias = acct;\ndeposit(alias, 25);\nacct.Balance % 175: updates are seen through both names"
36 },
37 {
38 "cell_type": "markdown",
39 "id": "oo-5",
40 "metadata": {},
41 "source": "## Inheritance, abstract methods, and polymorphism\n\n[Shape.m](./Shape.m) declares an abstract `area()` and a concrete\n`describe()`. [Circle.m](./Circle.m) and [Square.m](./Square.m) implement\n`area()`."
42 },
43 {
44 "cell_type": "code",
45 "id": "oo-6",
46 "metadata": {},
47 "execution_count": null,
48 "outputs": [],
49 "source": "shapes = {Circle(2), Square(3)};\nareas = cellfun(@(s) s.area(), shapes)\ncellfun(@(s) isa(s, 'Shape'), shapes)"
50 },
51 {
52 "cell_type": "code",
53 "id": "oo-7",
54 "metadata": {},
55 "execution_count": null,
56 "outputs": [],
57 "source": "for i = 1:numel(shapes)\n shapes{i}.describe();\nend"
58 },
59 {
60 "cell_type": "markdown",
61 "id": "oo-8",
62 "metadata": {},
63 "source": "## Static methods and methods across files\n\n[MathX.m](./MathX.m) has a static method (called on the class, not an\ninstance). [@Poly/](./@Poly/Poly.m) is a class whose methods live in\nseparate files in its `@Poly` folder."
64 },
65 {
66 "cell_type": "code",
67 "id": "oo-9",
68 "metadata": {},
69 "execution_count": null,
70 "outputs": [],
71 "source": "MathX.hypot3(3, 4, 12)\np = Poly([1 -3 2]); % x^2 - 3x + 2\np.evalAt(5) % method from @Poly/evalAt.m\nroots(p.coeffs)'"
72 },
73 {
74 "cell_type": "markdown",
75 "id": "oo-10",
76 "metadata": {},
77 "source": "## Private helper methods\n\n[Temperature.m](./Temperature.m) keeps its conversion in a\n`methods (Access = private)` block, used internally by `fahrenheit`."
78 },
79 {
80 "cell_type": "code",
81 "id": "oo-11",
82 "metadata": {},
83 "execution_count": null,
84 "outputs": [],
85 "source": "t = Temperature(100);\nt.fahrenheit()"
86 }
87 ],
88 "metadata": {
89 "kernelspec": {
90 "display_name": "numbl (MATLAB syntax)",
91 "language": "numbl",
92 "name": "numbl"
93 },
94 "language_info": {
95 "codemirror_mode": "octave",
96 "file_extension": ".m",
97 "mimetype": "text/x-octave",
98 "name": "numbl",
99 "nbconvert_exporter": "script",
100 "pygments_lexer": "matlab"
101 }
102 },
103 "nbformat": 4,
104 "nbformat_minor": 5
moveopenescclose