#定义一个类
class Person():def eat(self):print('我喜欢吃零食')def drink(self):print('我喜欢喝冰红茶')
#类的实例化
p1 = Person()
p1.eat()
p1.drink()
#定义一个类
class Person():def speak(self):print(self)print('很高兴见到你')
#类的实例化
p2 = Person()
print(p2)#打印的地址与调用speak一样
p2.speak()
p3 = Person()
print(p3)
p3.speak()#类的属性
class Geese:neck = '脖子老长'wing = '翅膀贼漂亮'leg = '腿位于身体中心支点'
geese1 = Geese()
print(geese1.neck)
geese1.neck = '脖子短'
print(geese1.neck)
print(Geese.neck)#初始化方法
class Geese:def __init__(self):print('我是大雁')
Geese1 = Geese()
class Geese:def __init__(self,beak,wing,claw):print(beak)print(wing)print(claw)beak = '喙的长度与头部长度几乎相等'
wing = '翅膀长'
wild = '爪子很锋利'
wildgoose = Geese(beak,wing,wild)#定义一个类
class Person():def __init__(self,name,age):self.name = nameself.age = agesun = Person('孙悟空','560')
print(sun.name)
print(sun.age)class Geese():def __init__(self,beak,wing,claw):print('我是大雁类,我有以下特征')self.name = 'uua'print(beak)print(wing)print(claw)def fly(self,state):print(state)
beak = '嘴'
wing = '翅膀'
claw = '爪子'
geese = Geese(beak,wing,claw)
geese.fly('我飞的贼高')
print(geese.name)
geese.name = 'asdf'
print(geese.name)
geese.name1 = 'dayan'
Geese.name1 = 'asdfas'
print(geese.name1)
print(Geese.name1)#在类的内部获取外部定义的属性
class perpon():def speak(self):print(f'我的名字:{self.name},我的年龄:{self.age},我的住址:{self.address}')
p1 = perpon()
p1.name = '白大黑'
p1.age = 18
p1.address = '天街'
p1.speak()
构建的一个类并且调用它
class User(object):def __init__(self):print('构建对象的时候触发了')self.name = '张三'self.age = 18self.gender = '男'self.tel = '17823219231'def study(self):print('我爱学习')if __name__ == '__main__':print('触发')u = User()print('结束')print(u.name)print(u.age)u.age = u.age+1print(u.age)u.study()
构建一个类并调用它,其中有__str__方法
class User():def __init__(self,name,age,gender,tel):self.name = nameself.age = ageself.gender = genderself.tel = teldef study(self):print(self.name+"爱学习")def __str__(self):return f'我的名字:{self.name},我的年龄:{self.age},我的性别:{self.gender},我的电话:{self.tel}'if __name__ == "__main__":u = User('张三',18,'男','19208279872')print(u)u.study()
定义学员信息类,包含姓名、成绩属性,定义成绩打印方法(90分及以上显示优秀,80分及以上显示良好,70分及以上显示中等,60分及以上显示合格,60分以下显示不及格)
class Student():def __init__(self,name,score):self.name = nameself.score = scoredef printScore(self):if self.score >= 90:print(f'{self.name}成绩为{self.score},优秀')elif self.score >=80:print(f'{self.name}成绩为{self.score},良好')elif self.score >= 70:print(f'{self.name}成绩为{self.score},中等')elif self.score >= 60:print(f'{self.name}成绩为{self.score},及格')else:print(f'{self.name}成绩不及格')name = input('请输入你的名字')
score = int(input('请输入你的成绩'))
p = Student(name,score)
p.printScore()
小明体重75.0公斤,小明每次跑步会减掉0.50公斤,小明每次吃东西体重增加1公斤
-
对象:小明
-
属性:姓名、体重
-
方法:跑步、吃东西
class Person():def __init__(self,name,weight):self.name = nameself.weight = weightdef run(self):self.weight = self.weight-0.5def eat(self):self.weight = self.weight+1def __str__(self):return f'{self.name}当前体重为{self.weight}(kg)'p = Person('小明',float(75))
p.run()
p.eat()
print(p)
编写Prime类实现输出大于m且紧随m的k个素
class Prime():def __init__(self,p,number):self.p = pself.number = numberdef primeNumber(self):count = 0i = self.p + 1while count <= self.number:for j in range(2,i):if i % j == 0:breakelse:print(i)count = count + 1i = i + 1
m = int(input('请输入m:'))
k = int(input('请输入k:'))
prime = Prime(m,k)
prime.primeNumber()