3个月前

编程语言
607
class student:
    name = None
    sex = "man"
    age = 1
    __privatemsg = 1980 #私有属性

    def __privatefunc(self): #私有方法
        print("private func")

    def callprivate(self): #公共方法可以调用
        self.__privatefunc()
        print(self.__privatemsg)

    def classfunc(self): #类方法, 必须加入参数 self
        print(f"show my name {self.name}")

    def __init__(self, name, sex, age): #魔术方法 操控原来的构造方法
        self.name = name
        self.sex = sex
        self.age = age

    def __str__(self): #魔术方法 操控原来的str()
        return (f"{self.name}, {self.sex}")
    
    def __it__(self, other): #魔术方法, 操控原来的 <
        return self.age < other.age
    
    def __le__(self, other): #操控原来的 <=
        return self.age <= other.age
    
    def __gt__(self, other): # >
        return self.age > other.age
    
    def __ge__(self, other): # >=
        return self.age >= other.age
    
    def __eq__(self, other): # ==
        return self.age == other.age
        


student2 = student("tail", "manb", 18) # 构造方法
student3 = student("tail", "woman", 17)
print(student2.__it__(student3)) #False
print(student2 < student3) #False
print(student2 <= student3) #False
print(student2 > student3) #True
print(student2 >= student3) #True
print(student2 == student3) #False
print(str(student2)) #替换原本的str方法 tail, manb

类的继承

class phone:
    name = "honor"
    def ptname(self):
        print("im honor")

class phone2022:
    name = "huawei"
    def ptname(self):
       print("im huawei")

class phone2024(phone, phone2022): #同名方法或成员, 左边优先级最高
    age = 12
    def ptage(self):
        print("12 year")
    
    def ptname(self): #同名直接复写
        print("im xiaomi")
        super().ptname() #默认查找最左边的父类
        phone2022.ptname(self) #传参

a = phone2024()
a.ptname()
#im xiaomi
#im honor
#im huawei

多态的简单实现

class animal:
    def speak(self):
        pass
class dog:
    def speak(self):
        print("bark")
class cat:
    def speak(self):
        print("mi")

def voice(a : animal):
    a.speak()

dog = dog()
cat = cat()
voice(dog)
voice(cat)
class airconditioner: #定义抽象类
    def make_cool(self):
        pass
    def make_hot(self): #抽象方法 子类需要复写抽象方法
        pass
    def swing(self):
        print("??? me")
        pass
 
class geli(airconditioner):
    def make_cool(self):
        print("geli make cool")
    def make_hot(self):
        print("geli make warm")
    #def swing(self):
    #    print("geli wave")

class meidi(airconditioner):
    def make_cool(self):
        print("meidi make cool")
    def make_hot(self):
        print("meidi make warm")
    def swing(self):
        print("meidi wave")

def makecool(object : airconditioner):
    object.make_cool()
def makewarm(object : airconditioner):
    object.make_hot()
def swing(object : airconditioner):
    object.swing()


airmeidi = meidi()
airgeli = geli()
makecool(airgeli)
makecool(airmeidi)

makewarm(airgeli)
makewarm(airmeidi)

swing(airgeli) # ??? me
swing(airmeidi)
image
EchoEcho官方
无论前方如何,请不要后悔与我相遇。
1377
发布数
439
关注者
2222635
累计阅读

热门教程文档

C
14小节
爬虫
6小节
Objective-C
29小节
Typescript
31小节
Redis
14小节