Python KLASSEN: Unterschied zwischen den Versionen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen
(Die Seite wurde neu angelegt: „ =Links= *https://www.hdm-stuttgart.de/~maucher/Python/html/Klassen.html“)
 
Zeile 1: Zeile 1:
 +
=Klasse=
 +
*cat cl.py
 +
<pre>
 +
#!/usr/bin/python
 +
class mycl(object):
 +
def __init__(self,zahl1,zahl2):
 +
  self.z1 = zahl1
 +
  self.z2 = zahl2
  
 +
def sub(self):
 +
    print self.z1 - self.z2
 +
 +
def add(self):
 +
    print self.z1 + self.z2
 +
</pre>
 +
=Instanz=
 +
*cat inst.py
 +
<pre>
 +
#!/usr/bin/python
 +
from cl import mycl
 +
x = mycl(9,7)
 +
x.sub()
 +
x.add()
 +
</pre>
 
=Links=
 
=Links=
 
*https://www.hdm-stuttgart.de/~maucher/Python/html/Klassen.html
 
*https://www.hdm-stuttgart.de/~maucher/Python/html/Klassen.html

Version vom 4. November 2017, 19:33 Uhr

Klasse

  • cat cl.py
#!/usr/bin/python
class mycl(object):
 def __init__(self,zahl1,zahl2):
   self.z1 = zahl1
   self.z2 = zahl2

 def sub(self):
    print self.z1 - self.z2

 def add(self):
    print self.z1 + self.z2

Instanz

  • cat inst.py
#!/usr/bin/python
from cl import mycl
x = mycl(9,7)
x.sub()
x.add()

Links