-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1-classes.py
More file actions
157 lines (100 loc) · 3.86 KB
/
1-classes.py
File metadata and controls
157 lines (100 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Classes, Methods, Constructors
# Der Klassenrumpf
class foo(object):
pass
# Methoden
# Keine Umlaute oder Operatoren im Namen
# nur Methoden etc verwenden ....
class foo_methods(object):
print("Hurz")
def print_Foo(self):
print("Foo")
# --!! wie man sehen kann, wird print("Hurz") ausgegeben obwohl,
# die Classe noch nicht aufgerufen / Instanziiert wurde
# deshalb nur methoden definitionen in classen verwenden nicht direkten code...
# richtig wäre:
# --------- von Hand ausbauen ----------
class foo_methods(object):
def hurz(self):
print("Hurz")
def print_Foo(self):
print("Foo")
# Instanziierung der Klasse
instanz_von_foo = foo_methods()
# aufruf der Methode print_Foo
instanz_von_foo.print_Foo()
instanz_von_foo.hurz()
# einfache Parameterübergabe in Methoden
class foo_methods(object):
def print_Foo(self, text):
print(text)
instanz_von_foo = foo_methods()
instanz_von_foo.print_Foo("Hallo Pythonfoo")
# mehrere fest definierte Parameter
# ---- von Hand ausbauen--------
class foo_methods(object):
def print_Foo(self, text, multiplikator):
print multiplikator*text
instanz_von_foo = foo_methods()
instanz_von_foo.print_Foo("Hallo Pythonfoo",5)
# variable Anzahl von Parametern
class foo_methods(object):
def print_Foo(self, *foo_parameter):
print "It is at Tuple: " , foo_parameter
for param in foo_parameter:
print param
instanz_von_foo = foo_methods()
instanz_von_foo.print_Foo("Hallo Pythonfoo","Python ist cool")
# variable Anzahl von Parametern in Dictionary
class foo_methods(object):
def print_Foo(self, **foo_parameter):
print "just printed: " , foo_parameter
if "gruss" in foo_parameter:
print "Die Grußformel lautet: " + foo_parameter["gruss"]
else:
print "Keine Grüße enthalten"
instanz_von_foo = foo_methods()
instanz_von_foo.print_Foo(gruss = "Hallo Pythonfoo",sagt = "Python ist cool")
# Construktor __init__
# Der Konstruktor einer Classe ist die Methode, die als erstes
# bei der initialisierung der Classe aufgerufen wird.
# In python wird der Construktor __init__ genannt
class foo(object):
def __init__(self):
print "Ich bin der __init__ Konstruktor und komme bei der Instantiierung"
# Variablendeklaration in einer Klasse während der Initialisierung
self.magic = "Variablen einer Classe werden mit self. begonnen"
# Es gibt nur die Konvention private Variablen und Methoden in
# der Namensgebung mit Unterstrich zu beginnen.
self._privat = "Private Variablen in Klassen unter Python gibt es nicht"
fooclass = foo()
print fooclass.magic
print fooclass._privat
# Instantiierung mit Parameterübergabe
# Der Konstruktor kann bei der Instantiierung
# der Klasse vergleichbar zur Methode Parameter entgegennehmen
class foo(object):
def __init__(self, text):
print text
fooclass = foo("Hallo Pythonfoo")
# was tut self oder Variablen in Klassen
# ------- die Prints nacheinander von Hand testen -----------
# ------- immer ein geht nicht dahinter ---------------------
class foo(object):
def __init__(self):
var1 = "Ich bin var1"
self.var2 = "Ich bin var2"
def printvar1(self):
print var1
foobar = foo()
print var1
# print foobar.var1
# print foobar.printvar1()
# print foobar.var2
# wie man sehen kann, ist eine einfache Variable innerhalb
# einer Methode nur in der Methode selbs zu benutzen
# für den Zugriff aus anderen Methoden oder von außerhalb in die Klasse
# ist self erforderlich