Skip to content

Commit d5cfeb9

Browse files
committed
method
1 parent 151bf59 commit d5cfeb9

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

‎newcodes/about_self.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
class Person:
5+
def __init__(self, name):
6+
self.name = name
7+
8+
def get_name(self):
9+
return self.name
10+
11+
def breast(self, n):
12+
self.breast = n
13+
14+
def color(self, color):
15+
print("{0} is {1}".format(self.name, color))
16+
17+
def how(self):
18+
print("{0} breast is {1}".format(self.name, self.breast))
19+
20+
girl = Person('canglaoshi')
21+
girl.breast(90)
22+
23+
girl.color("white")
24+
25+
girl.how()
26+

‎newcodes/class_method.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
class Foo:
5+
lang = "Java"
6+
def __init__(self):
7+
self.lang = "python"
8+
9+
@classmethod
10+
def get_class_attr(cls):
11+
return cls.lang
12+
13+
if __name__ == "__main__":
14+
print("Foo.lang:", Foo.lang)
15+
r = Foo.get_class_attr()
16+
print("get class attribute:", r)
17+
18+
f = Foo()
19+
print("instance attribute:", f.lang)
20+
print("instance get_class_attr:", f.get_class_attr())

‎newcodes/static_method.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
import random
5+
6+
7+
class Foo:
8+
def __init__(self, name):
9+
self.name = name
10+
11+
def get_name(self, age):
12+
if self.select(age):
13+
return self.name
14+
else:
15+
return "the name is secret."
16+
17+
@staticmethod
18+
def select(n):
19+
a = random.randint(1, 100)
20+
return a - n > 0
21+
22+
23+
if __name__ == "__main__":
24+
f = Foo("luolaoshi")
25+
name = f.get_name(22)
26+
print(name)

0 commit comments

Comments
 (0)