{ "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:" ], "text/plain": [ "# 2 methods for generic function \"area\":\n", "area(C::circle) at In[17]:6\n", "area(R::rect) at In[17]:10" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methods(area) # we can look at the functions that have been defined with the name area" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "3-element Array{Method,1}:" ], "text/plain": [ "3-element Array{Method,1}:\n", " quad(btmleft::point,topright::point)\n", " circle(x0::point,r::Float64) \n", " rect(btmleft::point,topright::point)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methodswith(point) # we can look at the functions that take a point as an argument" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "1-element Array{Method,1}:" ], "text/plain": [ "1-element Array{Method,1}:\n", " area(C::circle) at In[17]:6" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "methodswith(circle) # we can look at the functions that take a circle as an argument" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Create a 2d vector type, more or less the same as the point we allready have, and a new function \n", "# that takes as arguments a shape type and a vector. It should shift the position of the shape by\n", "# the given vector and return a new translated shape.\n", "\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 0.3.7", "language": "julia", "name": "julia 0.3" }, "language_info": { "name": "julia", "version": "0.3.7" } }, "nbformat": 4, "nbformat_minor": 0 }