Python之面向对象

  面向过程

  事例:两个人一天干以下几件事:

  1. 张三: 起床---吃饭---工作---吃饭---工作---吃饭---工作---回家---睡觉

  2. 李四: 起床---吃饭---学习---吃饭---学习---回家---玩耍---睡觉

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#cat 面向过程.py
def get_up(name):
    print("{}起床".format(name))
 
def eat(name):
    print("{}吃饭".format(name))
 
def go_to_work(name):
    print("{}工作中".format(name))
 
def go_to_school(name):
    print("{}学习中".format(name))
 
def go_to_play(name):
    print("{}玩耍中".format(name))
 
def go_home(name):
    print("{}回家".format(name))
 
def go_to_bed(name):
    print("{}睡觉".format(name))
 
get_up("zhangsan")
eat("zhangsan")
go_to_work("zhangsan")
go_to_school("zhangsan")
go_to_play("zhangsan")
go_home("zhangsan")
go_to_bed("zhangsan")

  类与对象

  类是总结事物特征的抽象概念,是创建对象的模板。对象是安装类来具体出来的实物

  举例说明

 

   玩具模型就是类

  具体生产出来的玩具就是对象

  类就是一个模板,对象是通过模板创建出来的具体的实物

  类的构成

  类的名称:类名

  类的属性:一组参数数据

  类的方法:操作的方式或行为

  如:为人设计一个类

  名称: People

  属性:name,sex,age,weight,height等

  方法:eat,drink,walk,run

  类的创建

1
2
class People(object): # 创建类,使用驼峰式命名
    pass

  创建对象 给对象附加属性

1
2
3
4
5
6
7
8
p1 = People()
p2 = People()
# 给对象加属性,不是给类加属性
 p1.name = "张三"
p1.sex = "男"
 
p2.name = "李四"
p2.sex = "女"

  以上是直接给对象加属性,非给类加属性

  给类加属性

1
2
3
4
5
6
7
class People(object): # 创建类,使用驼峰式命名
    def __init__(self, name, sex):  # 定义类的属性
        self.name = name
        self.sex = sex
 
p1 = People("张三", "男")   # 通过类创建对象,即类的实例化 p1 = self p1.name="张三" sex="男"
p2 = People("李四", "女")

  给类加方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class People(object): # 创建类,使用驼峰式命名
    def __init__(self, name, sex):  # 定义类的属性
        self.name = name
        self.sex = sex
 
    def info(self): # 定义类的方法
        print(self.name, self.sex)
 
p1 = People("张三", "男")   # 通过类创建对象,即类的实例化 p1 = self p1.name="张三" sex="男"
p2 = People("李四", "女")
 
print(id(p1))   # 两个对象的内存空间不一样,所以是两个不同的对象
print(id(p2))
 
# 给对象加属性,不是给类加属性
# p1.name = "张三"
# p1.sex = "男"
#
# p2.name = "李四"
# p2.sex = "女"
 
p1.info()   # 调用类的方法打印姓名和性别
p2.info()

  类的变量

  示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#cat 类的变量.py
class People(object):
    name = "haha"    # 类变量
 
    def __init__(self, name, sex):
        self.name = name    # 示例变量
        self.sex = sex
 
    def info(self):
        print(self.name, self.sex)
 
p1 = People("zhangsan", "man")
print(People.name)   # 可以打印类变量的值为haha
print(p1.name)       # 类变量与实例变量同名,实例变量优先于类变量 值为zhangsan

  注意:类变量,实例1,实例2都是独立的内存空间,修改互不影响

  示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# cat 类的变量2.py
class People(object):
    abc = "haha"    # 类变量
 
    def __init__(self, name, sex):
        self.name = name    # 实例变量
        self.sex = sex
 
    def info(self):
        print(self.name, self.sex)
 
p1 = People("zhangsan", "man")
p2 = People("lisi", "women")
 
p1.abc = "hehe" # 对p1实例的类变量赋值hehe
print(p1.abc)   # 结果为赋值后的hehe
print(p2.abc)   # 结果仍然为haha说明修改p1不影响p2
print(People.abc)   # 结果仍然为haha,说明修改p1也不影响类变量本身

  __str__和__del__

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# cat 面向对象str和del.py
class Hero(object):
    def __init__(self, name):
        self.name = name
 
    def __str__(self):  # print对象时调用__str__
        return "我叫{},我为自己代言".format(self.name)
 
    def __del__(self):
        print("......我{}还会回来的......".format(self.name))
 
hero1 = Hero("亚瑟")
hero2 = Hero("后羿")
 
print(hero1)    # 执行print调用__str__
# 我叫亚瑟,我为自己代言
print(hero2)
# 我叫后羿,我为自己代言
 
# 结束以后执行__del__输出以下
# ......我亚瑟还会回来的......
# ......我后羿还会回来的......
del hero1
del hero2
 
print("="*30)

  小结

 

   对象销毁包括执行结束了或者是删除了对象

  私有属性和私有方法

  一般情况下私有属性和私有方法是不对外公开的,只能在类的内部调用,往往用来做内部的事情,起到安全的作用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# cat 私有属性和私有方法.py
class People(object):
    __country = "china"    # 前面加上__ 就成了私有类属性,不能被外部直接调用
 
    def __init__(self, name, sex):
        self.name = name
        self.__sex = sex    # 前面加上__就成了私有对象属性,不能被外部调用
 
    def __info(self):   # 前面加上__成了私有方法,不能被外部调用
        print(self.name, self.sex)
 
p1 = People("zhangsan", "man")
 
# print(p1.sex)
# print(p1.__sex)
# print(p1.country)
# print(p1.__country)
# p1.info()
# p1.__info()
# 以上取消注释执行均会报错 类似报错 'People' object has no attribute '__info'

  示例: 如果类的外部需要调用到私有属性的值,可以对私有属性单独定义一个类的方法,让实例通过调用此方法来调用私有属性(私有方法同理)

  示例如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# cat 私有属性和私有方法2.py
class People(object):
    __country = "china"    # 前面加上__ 就成了私有类属性,不能被外部直接调用
 
    def __init__(self, name, sex):
        self.name = name
        self.__sex = sex    # 前面加上__就成了私有对象属性,不能被外部调用
 
    def __info(self):   # 前面加上__成了私有方法,不能被外部调用
        print(self.name, self.__sex)
 
    def show_country(self): # 内部调用私有类属性
        print(self.__country)
 
    def show_sex(self): # 内部调用私有对象属性
        print(self.__sex)
 
    def show_info(self):
        return People.__info(self)
 
p1 = People("zhangsan", "man")
p1.show_country()   # 调用非私有方法即相当于在类内部调用私有属性
p1.show_sex()
p1.show_info()

  继承

  继承的作用: 减少代码的冗余,便于功能的升级(原有的功能进行完善)与扩展(原没有的功能进行添加)

  继承示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# cat 继承.py
class People(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
 
    def eat(self):
        print("come to eat,{}".format(self.name))
 
    def drink(self):
        print("come to drink,{}".format(self.name))
 
class Man(People):  # 表示Man类继承父类 People
    def work(self): # 类Man自定义的方法,Woman不能调用此方法
        print("come to work,{}".format(self.name))
 
class Woman(People): # 表示Woman类继承父类 People
    pass
 
m1 = Man("zhansan", 16)
m1.eat()    # 继承了父类,可以调用父类的方法
m1.drink()
m1.work()
 
m2 = Woman("lisi", 18)
m2.eat()
m2.drink()

  方法重写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# cat 在子类重写父类方法.py
class People(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
 
    def eat(self):
        print("come to eat,{}".format(self.name))
 
    def drink(self):
        print("come to drink,{}".format(self.name))
 
class Man(People):  # 表示Man类继承父类 People
    def drink(self):
        if self.age >= 18:
            print("you can drink")
        else:
            print("you can't drink")
 
class Woman(People): # 表示Woman类继承父类 People
    pass
 
m1 = Man("zhansan", 18)
m1.eat()
m1.drink()  # 优先调用子类的方法 所以会根据输入的年龄大小分别输出you can drink或者you can't drink不会调用父类People的方法

  子类重新创建属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# cat 子类重新构造属性.py
class People(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
 
    def eat(self):
        print("come to eat,{}".format(self.name))
 
    def drink(self):
        print("come to drink,{}".format(self.name))
 
 
class Woman(People): # 表示Woman类继承父类 People
    def __init__(self, name, age, bra_size):    # 子类新加一个属性,需要和原父类属性对应
        People.__init__(self, name, age)    # 经典写法;换成super(Woman,self).__init__(name, age)也一样(新式类写法)
        # super(Woman, self).__init__(name, age)
        self.bra_size = bra_size
 
    def info(self):
        print("姓名 {} 年龄 {} 罩杯 {}".format(self.name, self.age, self.bra_size))
 
w1 = Woman("lisi", 18, "D")
w1.eat()
w1.info()

  多层继承

  类可以多层继承,下一级子类拥有上一层父类的属性和方法

  示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# cat 多层继承一.py
class Grandfather():
    def house(self):
        print("a big hose!")
 
class Father(Grandfather):
    def car(self):
        print("a cool car!")
 
class Child(Father):
    pass
 
p1 = Child()    # Child继承了Father Father继承了Grandfather 所以Child也有了Grandfather里的方法
p1.house()

  示例二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# cat 多层继承二.py
class People():
    def __init__(self, name, sex):
        self.name = name
        self.sex = sex
class Love(People):
    def fall_in_love(self,obj):
        if self.sex == "男":
            print("{}向{}求婚".format(self.name,obj.name))
        elif self.sex == "女":
            print("{}要给{}生猴子".format(self.name,obj.name))
        else:
            print("性别输入有误")
class Man(Love):
    pass
class Woman(Love):
    pass
m1=Man("张三","男")
w1=Woman("李四","女")
m1.fall_in_love(w1)
w1.fall_in_love(m1)

  多继承

  一个子类可以继承多个父类

  示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# cat 多继承一.py
class Father(object):
    def sing(self):
        print("can sing")
 
class Mother(object):
    def dance(self):
        print("can dance")
 
class Child(Father, Mother):    # 子类继承了两个父类
    pass
 
p1 = Child()
p1.sing()   # 同时拥有两个父类的方法
p1.dance()

  多态

   多态:一类事物有多种形态。例如水有水蒸气,液态水,冰等多种状态

  python变量本身是没有类型的,变量类型是由赋值觉得的。可以是int,也可以是str,这其实就是一种多态。

   示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# cat 多态1.py
class Animal(object):
    def jiao(self):
       pass
 
class Dog(Animal):
    def jiao(self):
        print("wang wang ...")
 
class Cat(Animal):
    def jiao(self):
        print("miao miao ...")
 
d1 = Dog()
c1 = Cat()
 
d1.jiao()   # 对象接类的方法调用,结果是狗叫
c1.jiao()   # 对象接类的方法调用,结果是猫叫

  统一调用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# cat 多态二.py
class Animal(object):
    def jiao(self):
        pass
 
class Dog(Animal):
    def jiao(self):
        print("wang wang ...")
 
class Cat(Animal):
    def jiao(self):
        print("miao miao ...")
 
def jiao(obj):
    obj.jiao()
 
d1 = Dog()
c1 = Cat()
 
jiao(d1)    # 统一调用方法,不管对象是狗还是猫,调用方法是一致的
jiao(c1)

  

 

 

  

posted @   minseo  阅读(130)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示