{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "type point\n", " x::FloatingPoint\n", " y::FloatingPoint\n", "end\n", "# we can define custom types in julia that behave somewhat like OOP classes" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "point(4.9,4.5)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "point(4.9,4.5) # a defined type automaticaly comes with a constructor" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# we can now use our point type to create further types\n", "type circle\n", " x0::point #center \n", " r::FloatingPoint #radius\n", "end\n", "\n", "type rect\n", " btmleft::point\n", " topright::point\n", "end" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "circle(point(0.0,0.0),1.0)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "center = point(0,0)\n", "C = circle(center,1) # create a circle type" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "rect(point(0.0,0.0),point(1.0,1.0))" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = point(0,0)\n", "B = point(1,1)\n", "R = rect(A,B) # create a rectangle type" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# try to create a julia type that defines either a triangle or a regular n sided polygon. Use the point type we have \n", "# created in your new type...\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "area (generic function with 2 methods)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Unlike usual OOP a class, or a type in julia, does not contain its methods. Instead functions are defined that\n", "# have different behaviour based on the input type. We will define a function that calculates the area of our circle\n", "# and rectangel types...\n", "\n", "function area(C::circle) # create function area which takes a circle type as input\n", " pi*C.r^2\n", "end\n", "\n", "function area(R::rect) # create another function area that takes a rect as input\n", " w = R.topright.x - R.btmleft.x\n", " h = R.topright.y - R.btmleft.y\n", " h*w\n", "end" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "area(C) " ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "area(R)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "2 methods for generic function area: