封装+面向对象内置函数大杂烩

# 广义上面向对象的封装:代码的保护,面向对象的思想本身就是一种
# 只让自己的对象能调用自己类中的方法

# 狭义上的封装——面向对象的三大特征之一
# 属性 和 方法都藏起来,不让你看见
# class Person:
#     def __init__(self,name,passwd):
#         self.name = name
#         self.__passwd = passwd      # 私有属性
#     def __get_pwd(self):            # 私有方法
#         return self.__passwd        # 只要在类的内部使用私有属性,就会自动的带上_类名
# feng = Person('feng','feng1234')
# print(feng.__dict__)

# 所有的私有,都是在属性的左边加上双下划线
    # 对象的私有属性
    # 类中的私有方法
    # 类中的静态私有属性
# 所有的私有的,都不能在类外使用

# class Room:
#     def __init__(self,name,length,width):
#         self.__name = name
#         self.__length = length
#         self.__width = width
#     def get_name(self):
#         return self.__name
#     def set_name(self,newName):
#         if type(newName) is str and newName.isdigit() == False:
#             self.__name = newName
#         else:
#             print('error')
#     def area(self):
#         return self.__length*self.__width
#
# zsir = Room('zsin',10,10)
# print(zsir.area())
# zsir.set_name('aaa')
# print(zsir.get_name())

# 用到私有概念的场景
# 1.隐藏起一个属性,不详让类的外部调用
# 2.我想保护这个属性,不想让属性随意被改变
# 3.我想保护这个属性,不被子类继承



# 关于类的内置函数
# property      将方法伪装成属性
# 内置装饰器函数  只在面向对象中使用
# from math import pi
# class Circle:
#     def __init__(self,r):
#         self.r = r
#     @property
#     def perimeter(self):
#         return 2*pi*self.r
#     @property
#     def area(self):
#         return pi*self.r**2
# c1 = Circle(5)
# print(c1.area)

from abc import abstractclassmethod,ABCMeta
# class Person:
#     def __init__(self,name,high,weight):
#         self.name = name
#         self.high = high
#         self.weight = weight
#     @property
#     def bmi(self):
#         return self.weight/(self.high**2)
# feng = Person('Mr.Feng',1.784,94.1)
# print(feng.bmi)

from abc import abstractclassmethod,ABCMeta
# class Person:
#     def __init__(self,name):
#         self.__name = name
#     @property
#     def name(self):
#         return self.__name + 'sb'
#     @name.setter
#     def name(self,new_name):
#         self.__name = new_name
#
# tiger = Person('泰森')
# print(tiger.name)
# tiger.name = '全班'
# print(tiger.name)

# class Goods:
#     discount = 0.5
#     def __init__(self,name,price):
#         self.name = name
#         self.__price = price
#     @property
#     def price(self):
#         return self.__price*Goods.discount
# apple = Goods('apple',5)
# print(apple.price)

# 属性  查看 修改  删除
# class Person:
#     def __init__(self,name):
#         self.__name = name
#     @property
#     def name(self):
#         return self.__name
#     @name.deleter
#     def name(self):
#         del self.__name
#     @name.setter
#     def name(self,newname):
#         self.__name = newname
# bob = Person('老铁')
# print(bob.name)
# del bob.name
# print(bob.name)

# method   方法
# staticmethod  静态的方法  ***
# classmethod   类方法      ****

# class Goods:
#     __discount = 0.5
#     def __init__(self,name,price):
#         self.name = name
#         self.__price = price
#     @property
#     def price(self):
#         return self.__price*Goods.__discount
#     @classmethod   # 把一个方法  变成一个类中的方法,这个方法就可以直接被类调用,不需要依托任何对象
#     def change_discount(cls,new_discount):
#         cls.__discount = new_discount
# apple = Goods('apple',5)
# print(apple.price)
# Goods.change_discount(0.8)
# print(apple.price)

class Login:
    def __init__(self,name,pwd):
        self.name = name
        self.pwd = pwd
    def login(self):pass
    @staticmethod
    def get_usr_pwd():
        user = input('usr>>>>')
        pwd = input('pwd>>>>')
        Login(user,pwd)
Login.get_usr_pwd()
# 在完全面向对象的程序中,
# 如果一个函数,既和对象没有关系,也和类没有关系,那么就用staticmethod将这个函数变成一个静态方法


# 类方法和静态方法,都是类调用的
# 对象可以调用类方法和静态方法吗?  可以      一般情况下 推荐类名调用
# 类方法  有一个默认参数 cls  代表这个类
# 静态方法  没有默认的参数,就像函数一样

 

转载于:https://www.cnblogs.com/Mr-Feng/p/10852607.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值