设为首页 加入收藏

TOP

Python中类的定义与使用(一)
2018-10-06 21:34:59 】 浏览:177
Tags:Python 定义 使用

一、类的简述


类时面向对象编程的核心内容。通常把具有相同特征(数据元素)与行为(功能)的事物描述定义为一个类,类是一个抽象的概念,把类实例化既可以得到一个对象。


因此,对象的抽象是类,类的具体化就是对象,也可以说类的实例是对象,类实际上就是一种数据类型。


类具有属性,它是对象的状态的抽象,用数据结构来描述类的属性。类具有操作,它是对象的行为的抽象,用操作名和实现该操作的方法来描述。


对象具有状态,一个对象用数据值来描述它的状态。对象还有操作,用于改变对象的状态,对象及其操作就是对象的行为。


比如:把人类即为一个抽象的类,“老王”则为一个的人即对象。每个对象都有一些相同的特征,但具体的数值却不一定相同。如:每个人都有“姓名”,“国籍”,“年龄”等特征。还具有一些相同的行为,如:“吃饭”,“睡觉”,“工作”等


可以简洁的描述为:Person ({"name", "country", "age"}, {"eat", "sleep", "work"})。


在这里可以看到,类有两种属性:数据属性,行为属性。在类中行为属性一般称为“方法”。


二、数据属性


属性有两种,类属性,实例属性(对象的属性),通常把所有对象共同拥有的属性定义为类属性,如:每个人都只有两只眼等,实例属性则时根据不同的对象赋值不一样的值,如:姓名等


下面来看一个简单的代码实现:


class Person(object):


    eyes = 2


    def __init__(self, name, age):
        self.name = name
        self.age = age


    def eat(self, food):
        print("%s 吃:%s" % (self.name, food))


    def eye(self):
        print("%s" % (self.eyes))



p1 = Person("张三", 18)
p2 = Person("李四", 19)


print("类的属性:", Person.__dict__)
print("对象p1的属性:", p1.__dict__)
print("对象p2的属性:", p2.__dict__)


p1.eat("番茄炒鸡蛋")
p1.eye()



#输出结果
类的属性: {'__module__': '__main__', 'eyes': 2, '__init__': <function Person.__init__ at 0x000002059BABB6A8>, 'eat': <function Person.eat at 0x000002059BABB730>, 'eye': <function Person.eye at 0x000002059BABBAE8>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
对象p1的属性: {'name': '张三', 'age': 18}
对象p2的属性: {'name': '李四', 'age': 19}
张三 吃:番茄炒鸡蛋
2


先输出,类, p1, p2 的属性,得出结论(1)实例化的对象只具备自己的数据属性(2)类的属性包含:类的数据属性、函数属性。


这里要注意几点:


2)对象访问属性的过程,查找属性__dict__字典,找到就访问这个属性,当对象的属性字典不存在该属性时,则会去类属性里边找,类里边也不存在时则会报错


3)类属性所有通过该类创建的对象都可以访问


1、类属性的增删该查


class Person(object):
    # 类属性添加的第一种方式
    eyes = 2


    def __init__(self, name):
        self.name = name


    def say(self, w):
        print("%s说了%s" % (self.name, w))



print("1--类的属性:", Person.__dict__)


# 类属性添加的第二种方式
Person.arm = 2
print("2--添加类的属性:", Person.__dict__)


# 类属性修改
Person.arm = 1
print("3--修改类的属性:", Person.__dict__)


# 类属性删除
del Person.eyes
print("4--删除类的属性:", Person.__dict__


#输出结果
1--类的属性: {'__module__': '__main__', 'eyes': 2, '__init__': <function Person.__init__ at 0x000001A8F769B6A8>, 'say': <function Person.say at 0x000001A8F769B730>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
2--添加类的属性: {'__module__': '__main__', 'eyes': 2, '__init__': <function Person.__init__ at 0x000001A8F769B6A8>, 'say': <function Person.say at 0x000001A8F769B730>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None, 'arm': 2}
3--修改类的属性: {'__module__': '__main__', 'eyes': 2, '__init__': <function Person.__init__ at 0x000001A8F769B6A8>, 'say': <function Person.say at 0x000001A8F769B730>, '__dict__': <attribute '__dict__

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇5分钟学会 Python 装饰器 下一篇OpenCL:图像处理基础note

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目