class my_class:
num = 0
def func(self):
return 'hello world'
cl = my_class()
print(cl.func(), cl.num)
class my_class1:
def __init__(self, a, b):
self.val1 = a
self.val2 = b
print('--------')
x = my_class1(2, 3)
print(x.val1, x.val2)
'''
--------
2 3
'''
class test:
def print_self(self):
print(self)
t = test()
t.print_self()
class people:
def __init__(self, name, age, weight):
self.name = name
self.age = age
self.weight = weight
def say_hello(self):
print('大家好, 我是{}, 今年{} 岁了, 体重 {} 千克'.format(self.name, self.age, self.weight))
p = people('法外狂徒张三', 23, 67)
p.say_hello()
class stud(people):
grad = ''
def __init__(self, n, a, w, g):
people.__init__(self, n, a, w)
self.grad = g
def say(self):
print('大家好, 我是{}, 今年{} 岁了, 体重 {} 千克, 读{}年级'.format(self.name, self.age, self.weight, self.grad))
s = stud('李四', 12, 45, 6)
s.say()
class parent:
def my_method(self):
print('father')
class child(parent):
def my_method(self):
print('child_own')
c = child()
c.my_method()
super(child, c).my_method()
'''
child_own
father
'''
class counter:
public_count = 0
_privatec_count = 0
def count(self):
self.public_count += 1
self._privatec_count += 1
c = counter()
c.count()
c.count()
print(c.public_count)
class site:
def __init__(self, name, url):
self.name = name
self.url = url
def who(self):
print('name :', self.name)
print('url : ', self.url)
def _private(self):
print('私有方法')
def public(self):
print('公共方法')
print(' ------- ')
self._private()
print('---------')
st = site('taobao', 'hahah.com')
st.who()
st.public()
'''
name : taobao
url : hahah.com
公共方法
-------
私有方法
---------
'''
class my_oper:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def get_vlaue(self):
print('a = {}, b = {}, c = {}'.format(self.a, self.b, self.c))
def __add__(self, other):
new_a = self.a + other.a
new_b = self.b + other.b
new_c = self.c + other.c
return my_oper(new_a, new_b, new_c)
def __sub__(self, other):
new_a = self.a - other.a
new_b = self.b - other.b
new_c = self.c - other.c
return my_oper(new_a, new_b, new_c)
def __mul__(self, other):
new_a = self.a * other.a
new_b = self.b * other.b
new_c = self.c * other.c
return my_oper(new_a, new_b, new_c)
def __divmod__(self, other):
new_a = self.a / other.a
new_b = self.b / other.b
new_c = self.c / other.c
return my_oper(new_a, new_b, new_c)