Python之类二

作者: ropon 分类: Python 发布时间: 2018-10-24 20:30
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/10/19 15:27
# @Author  : Ropon
# @File    : 23_01.py

# 类的成员,主要有三类
# 变量
#     实例变量(字段)
#         公有实例变量(字段)
#         私有实例变量(字段) 默认都是公开的,在变量前加上两个下划线_,这样函数和变量就变私有的。
#     类变量(静态字段)
#         公有类变量(静态字段)
#         私有类变量(静态字段) 默认都是公开的,在变量前加上两个下划线_,这样函数和变量就变私有的。

# class Lei:
#     __num = '18' # 私有类变量(静态字段)
#
#     def __init__(self, name):
#         self.name = name
#     def func1(self):
#         print('func1')
#
# class Lei_1(Lei):
#
#     def func2(self):
#         print('func2')
#         print(self.name)
#         # print(self.__num)
#
# obj2 = Lei_1('Ropon')
# obj2.func2()

# 方法
#     实例方法
# class Lei_Fang:
#     def __init__(self, name):
#         self.name = name
#
#     # 实例方法
#     def func1(self):
#         print(self.name)
#
# obj = Lei_Fang('Ropon')
# obj.func1()

#     静态方法
# class Lei_Fang1:
#     def __init__(self, name):
#         self.name = name
#     # 静态方法,如果方法不需要使用对象中封装的值,就可以使用静态方法
#     @staticmethod
#     def display(a, b):
#         return a + b
#
# # obj1 = Lei_Fang1('Peng')
# # print(obj1.display(1, 2))
# print(Lei_Fang1.display(1, 2))

#     类方法
# class Lei_Fang2:
#     # 类方法
#     @classmethod
#     def show(cls, a, b):
#         print(a, b)
#
# Lei_Fang2.show(1, 2)

# 静态方法/类方法和实例方法主要区别:

# 静态方法不需要调用类中封装的数据,也不需要类参数,没有访问对象和他内部方法
# 类方法必须有类参数,第一个永远是cls,会访问类cls
# 实例方法可调用类中封装的对象,第一参数是self,访问self
# 需要实例化,就实例方法;不需要类实例化,但是会用到类,就用类方法;如果跟类完全没关系,就用静态方法

# 属性
# 实际是通过方法改造
# class Lei:
#     def __init__(self):
#         pass
#     @property
#     def start(self):
#         return 1
#
#     @property
#     def stop(self):
#         return 2
# obj = Lei()
# print(obj.start)
# print(obj.stop)

# 总结
# 方法上要写@property,方法参数只有一个self
# 调用时不需要加括号,直接使用对象.方法
# 对于简单方法,不需要传参数且有返回值,可以使用属性

# 嵌套
# class Room:
#     def __init__(self, name, address):
#         self.name = name
#         self.address = address
#
#     def create(self):
#         print('create')
#
# obj1 = Room('xxBGP机房', 'xxxx')
# obj2 = Room('xxBGP机房1', 'xxxx1')
# obj3 = Room('xxBGP机房2', 'xxxx2')
#
# class Ebs:
#     def __init__(self, name, os, cpus, mem, ossize):
#         self.name = name
#         self.os = os
#         self.cpus = cpus
#         self.mem = mem
#         self.__ossize = ossize
#         self.room = None
#
#     def test(self):
#         print(self.__ossize)
#
# ebs1 = Ebs('ebs-1', 'centos6.4', '4', '4', '30')
# ebs2 = Ebs('ebs-2', 'centos7.4', '4', '2', '40')
# ebs3 = Ebs('ebs-3', 'centos7.5', '2', '4', '50')
#
# ebs1.room = obj1
# ebs2.room = obj2
# ebs3.room = obj3
#
# print(ebs1.room.name)
# print(ebs2.room.address)
# print(ebs2.name)
# print(ebs1.cpus)
# print(ebs3.os)
# # print(ebs3.__ossize)
# ebs1.room.create()
# ebs1.test()

# class Foo:
#     a1 = 11
#     a2 = 22
#     def __init__(self):
#         self.a1 = 1
# obj = Foo()
# print(obj.a1)
# print(obj.a2)

# class Foo:
#     a1 = 11
#     __a2 = 22
#
#     def __init__(self, num):
#         self.num = num
#         self.__num1 = 6666
#
# obj = Foo(99)
# print(obj.num) #99
# print(obj.a1) #11
# # print(obj.__a2) #报错
# # print(obj.__num1) #报错
#
# print(Foo.a1) #11
# # print(Foo.__a2) #报错
# # print(Foo.__num1) #报错

# class Foo:
#     a1 = 1
#     __a2 = 2
#
#     def __init__(self, num):
#         self.num = num
#         self.__num1 = 6666
#
#     def get_data(self):
#         print(self.num + self.a1)
#
# obj1 = Foo(666)
# obj2 = Foo(999)
# print(obj1.a1)
# print(obj1.num)
#
# obj1.a1 = 99
# obj1.num = 18
# # print(obj1.a1)
# # print(obj1.num)
# # print(Foo.num)
# obj2.a1 = 20
# print(Foo.a1)

# class Foo:
#     @classmethod
#     def func1(cls):
#         print(cls)
# 
#     def func2(self):
#         self.func1()
#         Foo.func1()
# 
# obj = Foo()
# obj.func2()

 

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!