class Person:
# name = '刘亦菲'
def __init__(self, name, age): # 做属性的初始化
# print('大家好')
self.name = name
self.age = age
print('这是类中的代码')
def speak(self):
print('大家好,我是%s' % self.name)
# def speak(self):
# print('大家好,我是%s' % self.name)
#
# def speak(self):
# print('大家好,我是%s' % self.name)
#
# def speak(self):
# print('大家好,我是%s' % self.name)
p1 = Person('刘亦菲', 18)
# p1.__init__()
# p1.name = '刘亦菲'
p2 = Person('周慧敏', 20)
# p2.name = '周慧敏'
p1.speak()
p2.speak()
# p3 = Person()
# p3.speak()
封装的引入:
class Car:
def __init__(self, name, high, color):
self.name = name
self.color = color
self.high = high
def run(self):
print('汽车在马路上疯狂飞驰')
def didi(self):
print('滴滴滴滴滴')
def stop(self):
print('淦,碰到石头了')
c1 = Car('奔驰', 1.8, '黑色')
# c1.name = '奥迪'
# c1.name = '泰迪'
# c1.name = '中华田园犬'
print(c1.name, c1.color, c1.high)
c1.run()
c1.didi()
c1.stop()
类的使用:
class Myclass:
name = ''
def sing(s)
print('唱歌真好听')
c1 = Myclass()
c1.name = '刘亦菲'
print(c1.name)
c1.sing()