1、初始对象
1.1、类的定义和使用
1.2、类的私有成员和方法
class Clock:
id =None
price =None
def ring(self):
import winsound
winsound.Beep(2000,3000)
clock = Clock()
clock.id ="12345"
clock.price=12.2
print(f"闹钟的ID为:{clock.id},闹钟的价格为:{clock.price}")
clock.ring()
1.3、类的构造方法
2、继承的实现和属性的使用
class Student(object):
count = 0
def __init__(self,sno,name,age,pwd='123456'):
self.sno = sno
self.name = name
self.age = age
self.__pwd =pwd
Student.count += 1
def __del__(self):
print("执行析构函数")
def work(self):
print("基类 work")
@staticmethod
def print_stu_list():
print("总人数 :\t", Student.count)
@property
def pwd(self):
return self.__pwd
@pwd.setter
def pwd(self,newpwd):
self.__pwd =newpwd
def say_hello(self):
print("my name is {},i am {} years old".format(self.name,self.age))
def get_pwd(self):
return self.__pwd
def set_pwd(self,newpwd):
self.__pwd =newpwd
class ActorStu(Student):
def __init__(self,sno,name,age,pwd='123456'):
super().__init__(sno, name, age, pwd)
self.major = 'movie'
def work(self):
super().work()
print("演戏")
pass
def make_movie(self,file_name):
print("{}参演过{}影片".format(self.name,file_name))
if __name__ == '__main__':
stu1 = Student(1001, 'lily', 20)
stu1.work()
stu2 = ActorStu(1002, 'sam', 18)
stu2.work()
print("stu2.name = ",stu2.name)
print("stu2.id = ",stu2.sno)
stu2.make_movie("sss")