python3学习笔记-面向对象

Python面向对象也是通过类的机制实现。
Python类的语法如下。

class Example:
	# 基本属性
	property1 = 0
	property2 = ''
	# 私有属性,类外不能被访问
	__property3 = ''
	# 属性p4在上面没有显示声明。可通过构造函数直接声明并初始化。
	def __init__(self, p1, p2, p3, p4):
		self.property1 = p1
		self.property2 = p2
		self.__property3 = p3
		self.property4 = p4
	def __print(self):
		print('AAAA')	
	def speak(self):
		print('property1 = %d,  property2 = %s, property3 = %s' % (self.property1, self.property2, self.__property3))

下面仔细介绍一下python的类。

类的属性

私有属性:__private_attrs:两个下划线开头,声明该属性为私有,不能在类地外部被使用或直接访问。
在类内部的方法中使用时,用self.__private_attrs。
在类外部访问时,可以使用 object._className__attrName 在外部访问私有属性。
公有属性。不以两个下划线开头。
公有属性和私有属性都必须显示初始化。

类属性初始化方式有两种。

  1. 直接在类中声明并初始化。
  2. 通过构造函数声明并初始化。

稍后再给出例子。

类的方法

在类地内部,使用def关键字可以为类定义一个方法,与一般函数定义不同,类方法必须包含参数self,且为第一个参数。

类的私有方法

__private_method:两个下划线开头,声明该方法为私有方法,不能在类地外部调用。
只能在类的内部调用 self.__private_methods

说明示例

"""
测试python类
"""


class Person:
    """ This is a comment."""
    # 定义属性
    person_id = 0
    person_name = ''
    person_age = 0
    # 私有属性,外部不能访问
    __person_sex = ''

    # 属性example没有在上面出现
    def __init__(self, id, name, age, sex, location):
        self.person_id = id
        self.person_name = name
        self.person_age = age
        self.__person_sex = sex
        self.location = location

    def speak(self):
        print('my id is %d, name is %s, age is %d, sex is %s, i live in %s' % (
            self.person_id, self.person_name, self.person_age, self.__person_sex, self.location))


person1 = Person(20132805, 'samuel king', 25, 'male', 'an hui')
person1.speak()
# 访问类的私有属性
print('访问类的私有属性:', person1._Person__person_sex)
print(person1.__class__)
print(person1.__dict__)
print(person1.__dir__())
print(person1.__sizeof__())
print(person1.__doc__)
print(person1.__module__)
"D:\Program Files\anaconda3\envs\practice\python.exe" D:/workspace/pycharm-workspace/Practice/jinyuxin0323/Demo1.py
my id is 20132805, name is samuel king, age is 25, sex is male, i live in an hui
访问类的私有属性: male
<class '__main__.Person'>
{'person_id': 20132805, 'person_name': 'samuel king', 'person_age': 25, '_Person__person_sex': 'male', 'location': 'an hui'}
['person_id', 'person_name', 'person_age', '_Person__person_sex', 'location', '__module__', '__doc__', '__init__', 'speak', '__dict__', '__weakref__', '__repr__', '__hash__', '__str__', '__getattribute__', '__setattr__', '__delattr__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__new__', '__reduce_ex__', '__reduce__', '__subclasshook__', '__init_subclass__', '__format__', '__sizeof__', '__dir__', '__class__']
32
 This is a comment.
__main__

Process finished with exit code 0

上图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值