day14-类和对象

2018-10-27  本文已影响0人  丿Rem丶

一、面向对象编程

1.编程思想

n = 100
sum1 = 0
for x in range(n+1):
    sum1 += x
def add_student():
    pass
class Student_Manager:
    def add_student(self):
        pass

二、类和对象的声明

1.类和对象定义:

2.类的声明

class 类名(父类列表):
    类的说明文档
    类的内容
# 声明Person类,吃饭和睡觉
class Person:
    """人类"""

    def eat(self):
        print('吃饭!')

    def sleep(self):
        print('睡觉!')

3.对象的声明

对象名 = 类名()
class Person:
    """人类"""

    def eat(self):
        print('吃饭!')

    def sleep(self):
        print('睡觉!')
p1 = Person()
p2 = Person()

三、对象方法

1.定义

2.调用过程

3.self

4.应用

# 声明类
class Person:
    """人类"""

    # 对象方法eat
    def eat(self, name):
        # self = p1, name = '小明'
        print('self:',self)
        print('吃饭!')
        self.sleep()

    def sleep(self):
        print('s_self', self)
        print('睡觉')



# 声明对象
p1 = Person()
print('p1:', p1)
p1.eat('小明')
# p1.sleep()

p2 = Person()
print('p2:', p2)
p2.eat('小红')


四、构造方法和init方法

1.构造方法

2.__init__方法

class Dog:
    """🐶类"""
    def __init__(self):
        print(self)
        print('init方法')


dog1 = Dog()
print(dog1)

dog2 = Dog()

3.带参其他参数的__init__方法

class Person:
    def __init__(self, name='', age=0):
        print(name, age)


p1 = Person('小红', 20)
p2 = Person('小明')
p3 = Person(age=10)

五、类的属性

1.基本知识

2.对象属性

class Person:
    def __init__(self):
        self.name = '张三'
        self.age = 18
        self.sex = '男'



p1 = Person()
print(p1.name)

p2 = Person()
print(p2.name)
class Person2:
    def __init__(self, name1, age1=0, sex1='girl'):
        self.name = name1
        self.age = age1
        self.sex = sex1
        self.id = '0001'


p1 = Person2('小明', 30, '女')

3.对象属性的增删改查

class Student:
    def __init__(self, name1='', age1=0, study_id1='001'):
        self.name = name1
        self.age = age1
        self.study_id = study_id1


stu1 = Student('小明')
stu2 = Student('小红', 18)
# 1.查

print(stu1.name)
# print(stu1.name2)   # AttributeError: 'Student' object has no attribute 'name2'


print(getattr(stu1, 'name'))
print(getattr(stu1, 'name2', '张三'))


print(stu1.__getattribute__('study_id'))
# print(stu1.__getattribute__('study_id2')) # AttributeError: 'Student' object has no attribute 'study_id2'

# 2.增/改

# 添加
stu1.sex = '男'
print(stu1.sex)

# 修改
stu1.name = '李四'
print(stu1.name)

# 修改
setattr(stu1, 'name', '娜美')
print(stu1.name)

# 添加
setattr(stu1, 'name2', '宝儿姐')
print(stu1.name2)

# 3.删

del stu1.age
# print(stu1.age)

print(stu1.sex)
delattr(stu1, 'sex')
# print(stu1.sex)

stu1.__delattr__('name')

4.类的字段

class Dog:
    # num就是类的字段
    num = 10

    __slots__ = ('color', 'name', 'type', 'sex', 'price', 'age')

    def __init__(self, color, name, type):
        self.color = color
        self.name = name
        self.type = type
        self.sex = '公的'
        print(Dog.num)


Dog.num = 100
print(Dog.num)

dog1 = Dog('黄色', '大黄', '土狗')

# dog1.neme = '财财'
# print(dog1.name)
dog1.age = 3

六、内置类属性

用法 作用
类.__name__ 获取当前类的名字
类.__doc__ 获取类的说明文档
对象.__class__ 获取对象的类, 类能做的事情,他都可以做
类.__dict__ 获取当前类的所有类的字段和其对应的值,以字典的形式返回(了解)
对象.__dict__ 获取当前对象所有的属性和其对应的值,以字典的形式返回
类.__module__ 获取当前类所在的模块名
类.__bases__ 获取当前类的父类, 返回的是一个元祖,元祖的元素是类

作业

1. 声明一个电脑类

1. 声明一个电脑类
class Computer:
    def __init__(self, brand, colour, memory_size):
        self.brand = brand
        self.colour = colour
        self.memory_size = memory_size
    def play_games(self):
        print('打游戏')
    def write_code(self):
        print('写代码')
    def watch_videos(self):
        print('看视频')


computer1 = Computer('联想', 'black', '8G')
# 1.查
print(computer1.brand)
print(getattr(computer1, 'colour'))
# 2.改
computer1.brand = '华为'
print(computer1.brand)
setattr(computer1, 'brand', '苹果')
print(computer1.brand)
# 3.增
computer1.price = 10000
print(computer1.price)
setattr(computer1, 'system', 'windows')
print(computer1.system)
# 4.删
del computer1.price
# print(computer1.price)  AttributeError: 'Computer' object has no attribute 'price'
delattr(computer1, 'system')
# print(computer1.system)  AttributeError: 'Computer' object has no attribute 'system'

#联想
#black
#华为
#苹果
#10000
#windows

2.声明一个人的类和狗的类

class Dog:
    def __init__(self, name, age, colour):
        self.name = name
        self.age = age
        self.colour = colour
    def cry(self):
        print('汪汪汪')


dog1 = Dog('小黄', 5, '黄色')


class Person:
    def __init__(self, name, age, dog):
        self.name = name
        self.age = age
        self.dog = dog
    def walk_the_dog(self, dog_name):
        print('我去遛%s了'%(dog_name))


person1 = Person('小明', 18, dog1)
person1.walk_the_dog(person1.dog.name)
#我去遛小黄了

3.声明一个矩形类

class Rectangle:
    def __init__(self, long, width):
        self.long = long
        self.width = width
    def show_perimeter(self):
        return 2*(int(self.long)+int(self.width))
    def show_area(self):
        return int(self.long)*int(self.width)


rectangle1 = Rectangle(2, 3)
print(rectangle1.show_area())
print(rectangle1.show_perimeter())
#6
#10

4.创建一个学生类和班级类

class Student:
    def __init__(self, name, age, study_id, score):
        self.name = name
        self.age = age
        self.study_id = study_id
        self.score = score
    def reply(self):
        print('到')
    def show_student_info(self):
        return 'name: '+self.name+' age: '+self.age+' study_id: '+self.study_id


student1 = Student('小明', '18', '001', '90')
student2 = Student('小绿', '18', '002', '65')
student3 = Student('小花', '17', '003', '70')
student4 = Student('小红', '19', '004', '85')
students = [student1, student2, student3]


class My_class:
    def __init__(self, stu, stu_class):
        self.stu = stu
        self.stu_class = stu_class
    def add_student(self, stu):
        students.append(stu)
    def del_student(self, stu):
        students.remove(stu)
    def call_the_roll(self,stu_name):
        print('来了吗%s'%(stu_name))

    def my_max(self):
        score_max = self.stu[0].score
        the_name = self.stu[0].name
        for stu in self.stu:
            if stu.score > score_max:
                score_max = stu.score
                name = the_name
        print(the_name, score_max)

    def get_average_value(self):
        score_sum = 0
        for stu in self.stu:
            score_sum += int(stu.score)
        stu_number = int(len(self.stu))
        score_average_value = score_sum/stu_number
        return score_average_value


my_class1 = My_class(students, 'class01')
my_class1.add_student(student4)
for student in students:
    print(student.show_student_info())
print('===================')
my_class1.del_student(student4)
for student in students:
    print(student.show_student_info())
name1 = input('请输入要点名的人: ')
my_class1.call_the_roll(name1)
for student in students:
    if student.name == name1:
        student.reply()
my_class1.my_max()
print(my_class1.get_average_value())

#name: 小明 age: 18 study_id: 001
#name: 小绿 age: 18 study_id: 002
#name: 小花 age: 17 study_id: 003
#name: 小红 age: 19 study_id: 004
#===================
#name: 小明 age: 18 study_id: 001
#name: 小绿 age: 18 study_id: 002
#name: 小花 age: 17 study_id: 003
#请输入要点名的人: 小明
#来了吗小明
#到
#小明 90
#75.0
上一篇下一篇

猜你喜欢

热点阅读