Basic knowledge of common operations of python-like objects

Basic knowledge of common operations of python-like objects

Basic creation and use of classes

# 基础创建
class clas:
    classVariables = '类变量'
# 基础使用
cal1 = clas()
print(cal1.classVariables)

Advanced usage

class person:
    # 类的构造函数,一般把类的常用属性用构造函数定义,创建有构造函数的类需要先调用构造函数
    # 构造人基本三连 名字 年龄 性别
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex

    # 传入期往身高,生成实际身高
    def measureHeight(self, expectTheHeight):
        # 实际身高=期望身高-10
        actualHeight = expectTheHeight - 10
        return actualHeight

    # 随机生成天赋
    def talent(self):
        #解释下这里(0, 10)就是取头不取尾巴就是0到9
        random1 = random.randint(0, 10)
        if random1 > 3:
            return "不配拥有天赋"
        else:
            talent1 = ["读书", "游戏", "经商", "混子"]
            return talent1[random1]


person1 = person('李', 18, '男')
actualHeight = person1.measureHeight(190)
print(person1.name + '的实际身高为:' + str(actualHeight))
print(person1.name + '的天赋为:' +person1.talent())

Guess you like

Origin blog.csdn.net/aaaaaaaaanjjj/article/details/114630828