#计算正方形的周长和面积 class Square: def __init__(self,side_len): self.side_len = side_len def perimeter(self): return self.side_len*4 def area(self): return self.side_len **2 s = Square(5) print(s.perimeter()) print(s.area())
命名空间
class Course: language = 'Chinese' def __init__(self,teacher,course_name,period,price): self.teacher = teacher self.name = course_name self.period = period self.price = price python = Course('egon','python','6 months',20000) Course.language = 'English' #对类里的静态属性进行修改 # Course.__dict__['language'] = 'Chinese' # print(Course.language) print(python.language)
#类中的静态变量,可以被对象和类调用
#对于不可变数据类型,类变量最好用类操作,而不应该用对象名操作
#对于可变数据类型,修改是共享的,重新赋值是独立的
class Course: language = 'Chinese' def __init__(self,teacher,course_name,period,price): self.teacher = teacher self.name = course_name self.period = period self.price = price python = Course('egon','python','6 months',20000) linux = Course('oldboy','python','6 months',20000) Course.language = 'English' #对类里的静态属性进行修改 # Course.__dict__['language'] = 'Chinese' # print(Course.language) # print(python.language) # print(linux.language) python.language = 'go-lang'
#类中的静态变量,可以被对象和类调用
#对于不可变数据类型,类变量最好用类操作,而不应该用对象名操作
#对于可变数据类型,修改是共享的,重新赋值是独立的
print(python.language) print(Course.language) print(linux.language)
go-lang
English
English
#类中的静态变量,可以被对象和类调用
#对于不可变数据类型,类变量最好用类操作,而不应该用对象名操作
python.language = 'Chinese' del python.language
class Course: language = ['English'] def __init__(self,teacher,course_name,period,price): self.teacher = teacher self.name = course_name self.period = period self.price = price python = Course('egon','python','6 months',20000) linux = Course('oldboy','python','6 months',20000) # Course.language = 'Chinese' #对类里的静态属性进行修改 # Course.__dict__['language'] = 'Chinese' # print(Course.language) # print(python.language) # print(linux.language) python.language[0] = 'Chinese' # del python.language
#类中的静态变量,可以被对象和类调用
#对于不可变数据类型,类变量最好用类操作,而不应该用对象名操作
#对于可变数据类型,修改是共享的,重新赋值是独立的
print(python.language) print(Course.language) print(linux.language) ['Chinese'] ['Chinese'] ['Chinese']
class Course: language = ['English'] def __init__(self,teacher,course_name,period,price): self.teacher = teacher self.name = course_name self.period = period self.price = price python = Course('egon','python','6 months',20000) linux = Course('oldboy','python','6 months',20000) # Course.language = 'Chinese' #对类里的静态属性进行修改 # Course.__dict__['language'] = 'Chinese' # print(Course.language) # print(python.language) # print(linux.language) python.language = ['Chinese'] # del python.language
#类中的静态变量,可以被对象和类调用
#对于不可变数据类型,类变量最好用类操作,而不应该用对象名操作
#对于可变数据类型,修改是共享的,重新赋值是独立的
print(python.language) print(Course.language) print(linux.language) ['Chinese'] ['English'] ['English']
#面向对象的三大特性:继承,多态和封装
#组合
class Dog: def __init__(self,name,aggr,hp,kind): self.name =name self.aggr = aggr self.hp =hp self.kind =kind def bite(self,persion): persion.hp -= self.aggr class Person: def __init__(self,name,aggr,hp,sex): self.name =name self.aggr = aggr self.hp =hp self.sex =sex self.money = 0 def attack(self,dog): dog.hp -= self.aggr def get_weapon(self,weapon): if self.money >= weapon.price: self.money -= weapon.price self.weapon = weapon self.aggr += weapon.aggr else: print('余额不足,请先充值') class Weapon: def __init__(self,name,aggr,njd,price): self.name = name self.aggr = aggr self.njd = njd self.price = price def hand18(self,person): if self.njd >0: person.hp -= self.aggr * 2 self.njd -= 1 w = Weapon('打狗棒',100,3,998) alex = Person('alex',0.5,100,'不详') jin = Dog('金老板',100,500,'不详') alex.money += 1000 alex.get_weapon(w) print(alex.weapon) print(alex.aggr) alex.attack(jin) print(jin.hp) alex.weapon.hand18(jin) print(jin.hp) # 组合:一个对象的属性值是另一个类的对象 # alex.weapon是 Weapon类的一个对象