• python学习笔记10-方法


    # -*- coding: utf-8 -*-
    # author:wyatt
    # @time:2021/9/23 20:08
    
    
    """
    类和对象
    成员有的特性:属性
    行为:方法
    
    在类的作用域里面定义的函数称为方法。特殊的函数。
    """
    
    
    class Mobile:
        # 类属性
        can_call = True
    
        def __init__(self, brand, color):
            self.brand = brand
            self.color = color
    
        def sell(self, price, discount=1):
            print(f'手机被卖了{price*discount}元')
            return price
    
        def call(self):
            print('正在打电话')
            self.take_photo()
    
        def take_photo(self):
            print('拍照')
    
    
    mobile = Mobile('apple', '粉色')
    
    # 实例方法的调用,只能有实例调用,类不能调用实例方法
    # 对象.方法()
    # 不能使用Mobile.sell()
    # 当方法有参数时,遵循和普通函数一样的规则,要传参数就传
    # __init__没有返回值,但其他函数该有返回值还是要有
    result = mobile.sell(100, 1)
    print(f'价格是{result}')
    
    # 在类里面,一个实例方法想要调用另外一个方法,可以使用self.方法
    result2 = mobile.call()

    """
    类方法:类可以调用的方法,实例也可以调用
    实例方法:实例可以调用,类不能调用
    静态方法:在本质上只是一个普通的函数,刚好放在了类里面管理
    """
    
    
    class Mobile:
        # 类属性
        can_call = True
    
        def __init__(self, brand, color):
            self.brand = brand
            self.color = color
    
        def sell(self, price, discount=1):
            print(f'手机被卖了{price*discount}元')
            return price
    
        def call(self):
            print('正在打电话')
            self.take_photo()
    
        def take_photo(self):
            print('拍照')
    
        # 类方法的定义  声明类方法
        @classmethod
        def abc(cls):
            print(f'这个类{cls}正在使用abc')
    
        @staticmethod
        def baozhaung():
            """
            没有固定参数,与类和对象没有直接关联
            无法使用self.属性和其他实例方法
            无法使用cls.属性 和其他类方法
            """
            print('这是一个静态方法')
    
    
    mobile = Mobile('apple', '粉色')
    
    # 调用类方法
    Mobile.abc()
    
    # 调用静态方法 无需实例化
    Mobile.baozhaung()
    mobile.baozhaung()
    class Mobile:
        # 类属性
        can_call = True
    
        def __init__(self, brand, color):
            self.brand = brand
            self.color = color
    
        def sell(self, price, discount=1):
            print(f'手机被卖了{price*discount}元')
            return price
    
        def call(self):
            print('正在打电话')
            self.take_photo()
    
        def take_photo(self):
            print('拍照')
    
        # 类方法的定义  声明类方法
        @classmethod
        def abc(cls):
            print(f'这个类{cls}正在使用abc')
    
        @staticmethod
        def baozhaung():
            """
            没有固定参数,与类和对象没有直接关联
            无法使用self.属性和其他实例方法
            无法使用cls.属性 和其他类方法
            """
            print('这是一个静态方法')
    
    
    class SmartPhone(Mobile):
        pass
    
    
    # 父类的所有的属性和方法,子类都可以用
    xiaomi = SmartPhone('xiaomi', '粉色')
    xiaomi.call()
  • 相关阅读:
    C# WinForm dataGridView 技巧小结
    Win7设置局域网共享
    vs2010快捷键大全
    C# WebBrowser.DocumentCompleted 多次调用解决方法
    为应用程序池 'DefaultAppPool' 提供服务的进程关闭时间超过了限制
    VB高清图标制作方法
    sqlite 中文排序
    一个vbs文件将指定文件夹下的文件名输出到指定文件夹下
    用DOS命令获取文件列表
    文件搜索神器 Everything
  • 原文地址:https://www.cnblogs.com/wsfsd/p/15328010.html
Copyright © 2020-2023  润新知