Skip to content
Snippets Groups Projects
Commit f0c0c2f4 authored by Sadikni, Remon's avatar Sadikni, Remon
Browse files

add first oop notebook in oop folder

parents
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:99d2f07a-c903-44b9-8db8-d06a75b130df tags:
## Why Object Oriented Programming (OOP)?
Your code is more maintenable and extendible if you choose an object oriented approach
- **extendible**, because you can create new behaviour by inhereting from already existing classes without the need to copy the code.
- that makes it also more **maintenable**, because you don't have to fix several files where you had the same mistake.
%% Cell type:markdown id:882b7d42-dde8-4978-b94e-991ab1c9a3f8 tags:
## What are Objects?
- In OOP, objects play a central role.
- You achieve that by decomposing real world processes in objects and object relationships, so that the entire system is assembled of objects.
- Objects interact through well defined interfaces
TO DO: IMAGE OF 2 OBJECTS
- Object = Attributes + Methods
- Objects store data, so called **attributes**
- An attribute is a property (=variable) that is associated with an object and holds some value that is required for processing.
- Objects provide operations to process data, these functions are called **methods** in OOP
%% Cell type:markdown id:e26b2a5d-073b-4216-8003-ead723ea6cd2 tags:
## What are Classes?
- Objects are categorised in Classes, which allows hierarchies and specialisations
- What is Inheritance? You create a new class that modifies the features of an existing one.
TO DO IMAGE
- A Class provides a mechanism to **create objects**.
- Each class introduces a **new type**.
- Creating an object is called **instantiation**, allocates storage for one object and returns the address of this location (reference).
%% Cell type:markdown id:64a7fe4a-c979-4843-9bf5-3201ab5d83d0 tags:
## Classes in Python
Simplest class example in Python:
%% Cell type:code id:56cbfe55-da2b-42e1-a42e-709461902acd tags:
``` python
class Student(object):
# Here comes the code to implement a student
pass # means do nothing
```
%% Cell type:markdown id:c77ab9a3-6313-4093-bfd8-cde0e8d2b24f tags:
Instantiation example of Class Student
- Student(): Creates new object of type Student
- student1: Variable to store the reference to an object of type Student
%% Cell type:code id:24e14ee9-8663-4f79-a2a1-45741180104f tags:
``` python
student1 = Student()
print(student1) # returns the memory address of the new Student object
```
%% Output
<__main__.Student object at 0x7ff786c06750>
%% Cell type:markdown id:ebaa5116-5ff6-418e-bb03-3ee03b603da8 tags:
### Constructor
- A constructor is a special method that is called when the object is created
- It sets the attributes of the created object and has the name **_ _ init _ _** in Python.
### Keyword self
- "self" is a reference to the current instantiation of the class
- The first argument of a method is "self"
- To access any attribute or method in a class, you have to use
- self.attributeName
- self.methodName
### How to implement a class
Implementing Classes means adding attributes and methods to set, manipulate and return attribute values:
%% Cell type:code id:0502608d-d562-4c62-86f7-90ad10aeb595 tags:
``` python
class Student(object):
# constructor
def __init__(self, name, age, matriculationNumber):
self.name = name
self.age = age
self.matriculationNumber = matriculationNumber
# get attribute age
def getAge(self):
return self.age
# set attribute age
def setAge(self, age):
self.age = age
# returns a string representation
def __str__(self):
return self.name+',Age:'+str(self.age)+','+str(self.matriculationNumber)
```
%% Cell type:markdown id:a98f9ce4-540d-48d6-844f-6f99c8fe7c6e tags:
- The class is called Student and inherits from the base class in Python, called object.
- notice the captital first letter of Student!
- The constructor _ _ init _ _ sets the values of the attributes name, age, matriculationNumber
- There are normally get and set methods for every attribute in the class, in this example we only implement the ones for __age__.
- The last method _ _str_ _ returns a string representation of an object
Let's instantiate 2 students and print them by using implicitly the _ _ str _ _ method of our class Student:
%% Cell type:code id:132d8bbb-855f-4449-b019-52a1e7c48e0d tags:
``` python
student1 = Student("Mark Car", 24, "u123456")
print(student1)
student2 = Student("Petra Door", 25, "u3333333")
# change age
student2.setAge(26) # manipulate the attribute age of student2
print(student2)
```
%% Output
Mark Car,Age:24,u123456
Petra Door,Age:26,u3333333
%% Cell type:markdown id:cab7715d-15db-4847-bcca-44f5a82c5041 tags:
## Unified Modeling Language UML
- UML is a notation for describing Object Oriented Systems and has been developed since 1995, the current Version is 2.0.
- UML diagrams can be divided into 3 categories:
- Structure diagrams
- Behaviour diagrams
- Interaction diagrams
%% Cell type:markdown id:0d0ce42d-f2f2-4c2e-a206-6725f3743994 tags:
### Structure Diagrams
- Structure Diagrams show the static architecture of the system irrespective of time
- One type of Structure Diagrams (among others) are **Class Diagrams** that show classes with methods and attributes and their associations.
%% Cell type:markdown id:73d919c2-44f1-4c09-a250-79e43c3b740f tags:
| Student |
| ------------- |
| -name : String
-age: int
-matriculationNumber: String |
| ------------------------------------------------------------------------------------------- |
| +Student(name: String, age: int, matriculationNumber: String)
+setName(name: String): void
+getName():String
+setAge(age: int):void
+getAge():int
+setMatriculationNumber(number: String):void
+getMatriculationNumber(): String |
%% Cell type:markdown id:a48f521d-0e56-4baf-8b1f-5182a8fc9adf tags:
- In the first part of the diagram you can find the class Name, in the second part the attributes followed by the methods.
- The **minus** in front of the attribute names means that these are **private** attributes: They are not visible outside the class.
- The **plus** in front of the method names means that these are **public** methods: they are visible outside the class.
%% Cell type:markdown id:177180eb-a57c-4a9f-bf1e-228f05ebd93f tags:
### Private attributes in Python
- Private attributes that cannot be accessed except from inside an object don’t exist!
- But there is a convention for marking attribute as private by prefixing it with an single underscore.
- Attributes without an underscore are public.
%% Cell type:code id:3c98e247-a2f2-4ead-9ab7-bf128aed8dc4 tags:
``` python
class Student(object):
# constructor
def __init__(self, name, age):
self.name = name # public attribute
self._age = age # convention for marking private attribute
# get attribute age
def getAge(self):
return self._age
s = Student("Mark","46")
print(s.name) # public attribute
print(s._age) # „private“ attribute
```
%% Output
Mark
46
%% Cell type:markdown id:9eef4cf2-8506-4762-a269-0cd4e29bec56 tags:
### Inheritance
- If classes have something in common, you can extract the common aspects of class C1 and C2 and create a class C (superclass, baseclass) that implements these aspects.
- C1 and C2 are smaller subclasses (or derived classes), containing only unique attributes / methods.
TO DO: IMAGE
Here is an example about inheritance in Python:
TO DO: WORK ON THE EXAMPLE
%% Cell type:code id:96da9dd8-ee55-4227-9b9f-3e63d6f6fd09 tags:
``` python
class Student(object):
# constructor
def __init__(self, name, age, matriculationNumber):
self.name = name
self.age = age
self.matriculationNumber = matriculationNumber
# built in print method
def __str__(self):
return self.name+', Age: '+str(self.age)+', '+str(self.matriculationNumber)
class GraduateStudent(Student):
def __init__(self, name, age, matriculationNumber, undergradMajor):
Student.__init__(self, name, age, matriculationNumber)
self.undergradMajor = undergradMajor
# built in print method
def __str__(self):
return self.name+', Age: '+str(self.age)+', '+self.undergradMajor
stud = Student("Matt Car", 21, "u5345")
gradStud = GraduateStudent("Ramon Sad", 31, "u345345", "physics")
print(stud) # actually invokes stud.__str__()
print(gradStud) # actually invokes gradStud.__str__()
```
%% Output
Matt Car, Age: 21, u5345
Ramon Sad, Age: 31, physics
%% Cell type:markdown id:7145541a-0de6-4ebe-b879-9f9c19c80774 tags:
- The class GraduateStudent inherits its behaviour from the class Student
- The common attributes are stored in the Superclass Student, the special attribute undergradMajor is stored in the derived class.
%% Cell type:markdown id:645b5011-21c5-479e-811a-54a0f635bb40 tags:
# OOP in Earth System Sciences
How does Object Oriented Programming look like in ESS? Here is an example that uses a class diagram to show how the framework works. You can see easily how you can add your own class by looking at the diagram, and that is the **big advantage of UML and the object oriented design**!
### CHEPROO, Object-Oriented tool specialized in complex geochemical processes:
S. A. Bea; J. Carrera; C. Ayora; F. Batlle; M. W. Saaltink, CHEPROO: A Fortran 90 object-oriented module to solve chemical processes in Earth Science models, http://doi.org/10.1016/j.cageo.2008.08.010
<img src="https://ars.els-cdn.com/content/image/1-s2.0-S0098300408002744-gr2_lrg.jpg"/>
<hr>
%% Cell type:code id:8f2bc7bd-d4a2-4542-8cf7-0632ab6f8852 tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment