Python语言中类的创建和使用非常重要。小仙女又来写博客了~~ 【厚颜无耻脸】
p1: 类的创建
类的定义:用关键字class
类的属性:有基本属性和私有属,私有属性在类外麦不可以直接访问
构造函数:def __init__(self):
类的方法:def set_data(data) / get_data()
举个栗子:
class people:
# define basic attributes, must initialize
name = ''
age = 1
gender = 'm'
# define private attribute
__height = 100
__weight = 20
# define constructor
def __init__(self, name, age, gender, h, w):
self.name = name
self.age = age
self.gender = gender
self.__height = h
self.__weight = w
# define method
def speak(self, words):
print("%s 说: 我 %d 岁