赞
踩
class Student: name = None gender = None nationality = None native_place = None age = None stu_1 = Student() stu_1.name = '小明' stu_1.gender = '男' stu_1.nationality = '中国' stu_1.native_place = '河南省' stu_1.age =18 print(stu_1.name) print(stu_1.gender) print(stu_1.nationality) print(stu_1.native_place) print(stu_1.age)
class Student: name = None def say_hi(self): print("Hello,{}".format(self.name)) def say_hello(self,msg): print(f"Hello,我是:{self.name},{msg}") stu = Student() stu.name = '小明' stu.say_hello("你好") stu2 = Student() stu2.name = '小白' stu2.say_hi()
class Clock:
id = None
price = None
def ring(self):
import winsound
winsound.Beep(2000,3000) #请关小音量,声音很大
clock1 = Clock()
clock1.id = "003032"
clock1.price = 18.8
print(clock1.id,clock1.price)
clock1.ring()
class Student:
name = None
age = None
tel = None
def __init__(self, name, age, tel):
self.name = name
self.age = age
self.tel = tel
print("Student object")
s1 = Student("xiaoming", 20, "555555")
print(s1.name)
print(s1.age)
print(s1.tel)
class Student:
def __init__(self,name,age):
self.name = name
self.age = age
def __str__(self):
return f'{self.name} is {self.age} years'
s = Student('小明',66)
print(s)
print(str(s))
class Student: def __init__(self,name,age): self.name = name self.age = age # __str__魔术方法 def __str__(self): return f'{self.name} is {self.age} years' # __lt__魔术方法 def __lt__(self, other): return self.age < other.age s1 = Student('小明',66) s2 = Student('小白',55) print(s1 < s2) print(s1 > s2)
class Student: def __init__(self,name,age): self.name = name self.age = age # __str__魔术方法 def __str__(self): return f'{self.name} is {self.age} years' # __lt__魔术方法 def __lt__(self, other): return self.age < other.age # __le__魔术方法 def __le__(self, other): return self.age < other.age s1 = Student('小明',66) s2 = Student('小白',55) print(s1 <= s2) print(s1 >= s2)
class Student: def __init__(self,name,age): self.name = name self.age = age # __str__魔术方法 def __str__(self): return f'{self.name} is {self.age} years' # __lt__魔术方法 def __lt__(self, other): return self.age < other.age # __le__魔术方法 def __le__(self, other): return self.age < other.age # __eq__魔术方法 def __eq__(self, other): return self.age == other.age s1 = Student('小明',66) s2 = Student('小白',66) print(s1 == s2)
class Phone:
__current_voltage = 1
def __keep_single_core(self):
print("cpu单核运行")
def call(self):
if self.__current_voltage >= 1:
print("yes")
else:
self.__keep_single_core()
print("no")
phone = Phone()
phone.call()
class Phone:
__is_5g_enable = False
def __check_5g_enable(self):
if self.__is_5g_enable:
print("yes")
else:
print("no")
def call_by_5g(self):
self.__check_5g_enable()
print("正在通话中")
phone = Phone()
phone.call_by_5g()
class Phone: IMET = None producer = "HH" def call_by_4G(self): print("4g通话") class Phone2(Phone): face_id = '10001' def call_by_5G(self): print("5g通话") phone = Phone2() phone.call_by_4G() phone.call_by_5G() print(phone.producer)
class Phone: IMET = None producer = "HH" def call_by_4G(self): print("4g通话") class NFCReader: nfc_type = '第五代' producer = 'HH' def read_card(self): print("NFC读卡") def write_card(self): print("NFC写卡") class RemoteControl: rc_type = '红外遥控' def control(self): print("红外") class MyPhone(Phone, RemoteControl, NFCReader): pass my_phone = MyPhone() my_phone.call_by_4G() my_phone.write_card() my_phone.control() my_phone.read_card()
class Phone: IMET = None producer = "itcast" def call_by_5G(self): print("5g通话") class Phone2(Phone): producer = "HH" def call_by_5G(self): print("省电模式") phone = Phone2() print(phone.producer) phone.call_by_5G()
def add(x: int, y: int):
return x + y
add1 = add(1, 2)
print(add1)
def func(date:list) -> list:
return date
class Animal: def speak(self): pass class Dog(Animal): def speak(self): print("汪汪汪") class Cat(Animal): def speak(self): print("喵喵喵") def make_noise(animal:Animal): animal.speak() dog = Dog() make_noise(dog) cat = Cat() make_noise(cat)
class AC: def cool_wind(self): pass def hot_wind(self): pass def swing_l_r(self): pass class Midea_AC(AC): def hot_wind(self): print("Midea制热") def swing_l_r(self): print("Midea左右摆风") def cool_wind(self): print("Midea制冷") class Gre(AC): def hot_wind(self): print("Gre制热") def swing_l_r(self): print("Gre左右摆风") def cool_wind(self): print("Gre制冷") def make_cool(ac:AC): ac.hot_wind() midea = Midea_AC() make_cool(midea) gre = Gre() make_cool(gre)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。