1 Star 0 Fork 0

Aurora/python_study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
04-类和对象.py 2.27 KB
一键复制 编辑 原始数据 按行查看 历史
Aurora 提交于 2023-11-17 21:32 . commit
# python与java,javaScript,cpp,kotlin一样都属于OOP语言
# **************** 创建类 ****************
class Person:
# 类的属性
name = None
age = None
addr = None
# 构造方法
def __init__(self, name, age, addr):
self.name = name
self.age = age
self.addr = addr
# 类的方法
def sayHello(self):
print(f"Hi! I am {self.name}.")
# 创建类的实例
p1 = Person("zhangsan", 18, "guilin")
print("name={},age={},addr={}".format(p1.name, p1.age, p1.addr))
p1.sayHello()
# **************** 类的继承和方法重写 ****************
class Animal:
name = None
age = None
def __init__(self, name, age):
self.name = name
self.age = age
# abstract method
def sayHi(self):
pass
class Cat(Animal):
# 重写sayHi方法
def sayHi(self):
print(f"Hi!我是{self.name},我{self.age}岁了")
def todo(self):
print("喵喵喵~~~")
class Dog(Animal):
# 重写sayHi方法
def sayHi(self):
print(f"Hello!我是{self.name},我{self.age}岁了")
def todo(self):
print("汪汪汪。。。")
cat = Cat("大橘", 2)
cat.sayHi()
cat.todo()
dog = Dog("三百万", 5)
dog.sayHi()
dog.todo()
# **************** 多态 ****************
class Car():
def speak(self):
pass
class BMW(Car):
def speak(self):
return "I am BMW."
class LAMB(Car):
def speak(self):
return "I am Lamborghini."
class WUL(Car):
def speak(self):
return "I am Wuling Hongguang."
def car_speak(car):
return car.speak()
bmw = BMW()
lamb = LAMB()
wul = WUL()
print(car_speak(bmw))
print(car_speak(lamb))
print(car_speak(wul))
# **************** 类的魔术方法 ****************
# 魔术方法以双下划线开始,双下划线结束
class my_class:
# init 构造
def __init__(self) -> None:
pass
# toString 一般用于输出对象信息
def __str__(self) -> str:
# return f"Person:{name={self.name}, age={self.age}}"
return " "
# equals 对象比较 (cpp中可使用运算符重载实现)
def __eq__(self, other: object) -> bool:
# if isinstance(self, Cat):
# return self.name == other.name and self.age == other.age
pass
# .....
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kaiz2001/python_study.git
git@gitee.com:kaiz2001/python_study.git
kaiz2001
python_study
python_study
master

搜索帮助