zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    [Android开发]cocos2dx工程中接入支付宝sdk
    cocos2dx android SDK接入总结
    Cocos2d-vs避免过长编译的小技巧
    math.h里的数学计算公式介绍
    Cocos Code IDE入门指南
    kbengine引擎的安装
    游戏服务端pomelo完整安装配置过程
    cocos2d-x 两个场景间进行传参数
    cocos2d-x 设置全局可变变量
    如何将cocos2d-x项目打包成一个.exe
  • 原文地址:https://www.cnblogs.com/wsfsd/p/15328010.html
Copyright © 2011-2022 走看看