zoukankan      html  css  js  c++  java
  • Python3.x基础学习-类--面向对象

    面向对象和面向过程区别

    1.面向过程:按照业务逻辑从上到下的设计模式,代码紧凑、耦合性强
    2.面向对象:将数据和业务抽象为对象,耦合性低,有利于代码重构

    类和对象概念

    类:是对一群具有相同特征或者行为的事物的统称,是抽象的,不能直接使用。
    对象:由类创建的,具体到某一事物的就是对象
    属性:记录对象有关特征的数据 例如:人的身高、体重、年龄
    方法:用于对象的相关操作和行为 例如:人吃饭,睡觉

    类使用方法

    定义类: 格式:class 类名:
    def 方法名 (self):
    命名规范:大驼峰命名

    创建对象:对象名(变量名)=类名()

    调用方法:对象名.方法名()

    class Person:
        def eat(self):
            print("吃饭")
    
    x = Person()
    print(id(x))
    #2226825524392
    x.eat()
    # 吃饭 y
    =Person() print(id(y))
    2226825524952
    y.eat()
    吃饭

    在类外部添加属性 变量名.属性名=属性值
    调用: 变量名.属性名
    self 哪个对象调用方法或者属性,self就是哪个变量,self约定俗成的写法
    class Person:
        def eat(self):
            print("%s吃火锅!"%self.name)
            print(id(self))
    
    perl = Person()
    print('--perl--',id(perl))
    #--perl-- 2226825525008
    perl.name
    ='johnson' perl.eat() # eat(perl)
    #johnson吃火锅!
    per2 = Person()
    per2.name = 'Mary'
    #Mary吃火锅!
    per2.eat()
     

     __init___()方法

    在创建对象时调用,进行初始化操作

    对象的属性都可以放在__init__()方法中

    class Dog:
        def __init__(self,name):
            self.name = name
        def introduce(self):
            print("我叫%s,请多多关照!"%self.name)
    
    dog1 = Dog("pet")
    dog1.introduce()
    #我叫pet,请多多关照!
    dog2
    = Dog("may") dog2.introduce()
    #我叫may,请多多关照!

    __str__()方法


    打印对象名称时默认调用的是__str__()方法
    此方法默认返回的是对象的内存地址
    我们可以重写__str__()方法打印对象保存信息
    class Dog:
        def __init__(self,name,age,weight):
            self.name = name
            self.age = age
            self.weight = weight
    
        def __str__(self):
            print("__str__")
            return '我叫{},今年{}岁,体重{}公斤'.format(self.name,self.age,self.weight)
    
    dog = Dog("jey",22,122)
    print(dog)
    print(dog.name,dog.age,dog.weight)
    # 我叫jey,今年22岁,体重122公斤
    定义一个学生类,有姓名和年龄属性,打印对象时
    输出,我叫xxx,今年xx岁
    class Student:
        def __init__(self,name,age):
            self.name = name
            self.age = age
    
        def __str__(self):
            return '我叫{},今年{}'.format(self.name,self.age)
    
    may = Student('may',22)
    print(may)
    1.计算圆的周长和面积
    定义圆类,属性:半径r,pi:3.14
    方法:求周长,面积
    class CalculateCicle:
        def __init__(self,r):
            self.r =r
            self.PI = 3.14
    
        def yuanZhou(self):
            return 2*self.PI*self.r
    
        def mianJi(self):
            return self.PI*self.r**2
    
    yuanZhou = CalculateCicle(4)
    print(yuanZhou.yuanZhou())
    # 25.12 mianJi
    = CalculateCicle(5) print(mianJi.mianJi())
    # 78.5
    搬家具规则:
    1.家具分不同的类型,并占用不同的面积
    2.输出家具信息时,显示家具的类型和家具占用的面积
    3.房子有自己的地址和占用的面积
    4.房子可以添加家具,如果房子的剩余面积可以容纳家具,则提示家具添加成功;否则提示添加失败
    5.输出房子信息时,可以显示房子的地址、占地面积、剩余面积
    class Furniture:
        def __init__(self,fur_type,area):
            self.fur_type = fur_type
            self.area = area
    
        def __str__(self):
            return "家具的类型:{},占用面积{}平米".format(self.fur_type,self.area)
    
    class Home:
        def __init__(self,address,area):
            self.address = address
            self.area = area
            self.free_area = area
    
        def add_fur(self,fur):
            if self.free_area>=fur.area:
                self.free_area -= fur.area
            else:
                print('剩余空间不足,无法添加家具')
    
        def __str__(self):
            return '房子的地址:{},占用面积{}平米,剩余面积{}平米'.format(self.address,self.area,self.free_area)
    
    # 创建一个家具对象
    
    fur1 = Furniture('',5)
    print(fur1)
    
    # 创建一个房子对象
    
    home1 = Home('深圳',30)
    print(home1)
    
    # 添加一个家具
    home1.add_fur(fur1)
    print(home1)
    
    # 再添加一个家具
    # 创建一个家具对象
    
    fur2 = Furniture('',25)
    print(fur2)
    home1.add_fur(fur2)
    print(home1)

    #家具的类型:床,占用面积5平米
    #房子的地址:深圳,占用面积30平米,剩余面积30平米
    #房子的地址:深圳,占用面积30平米,剩余面积25平米
    #家具的类型:床,占用面积25平米
    #房子的地址:深圳,占用面积30平米,剩余面积0平米

    人类:
    属性:姓名,攻击力10,生命值100
    方法:打猩猩
    猩猩类:
    属性#:姓名,攻击力20,生命值50
    方法:打人类
    猩猩的攻击力大于人类
    输出对象时,显示对象信息:xx的攻击力是xx,剩余生命力是xx

    class Person:
        def __init__(self,name):
            self.name = name
            self.attack = 10
            self.life = 100
    
        def attack_monky(self,monkey):
            if monkey.life>self.attack:
                monkey.life-=self.attack
            else:
                print("猩猩挂了")
    
        def __str__(self):
            return "{}的攻击力是{},剩余生命力是{}".format(self.name,self.attack,self.life)
    
    
    class Monkey:
        def __init__(self, name):
            self.name = name
            self.attack = 20
            self.life = 100
    
        def attack_person(self, person):
            if person.life >= self.attack:
                person.life -= self.attack
            else:
                print("人类挂了")
    
        def __str__(self):
            return "{}的攻击力是{},剩余生命力是{}".format(self.name,self.attack,self.life)
    
    
    # 创建一个人
    
    person1 = Person("Jack")
    print(person1)
    
    # 创建一个猩猩
    monkey1 = Monkey("Ma")
    print(monkey1)
    
    # 人攻击猩猩
    person1.attack_monky(monkey1)
    print(monkey1)
    
    monkey1.attack_person(person1)
    print(person1)
    
    monkey1.attack_person(person1)
    print(person1)
    
    monkey1.attack_person(person1)
    print(person1)
    
    monkey1.attack_person(person1)
    print(person1)
    
    monkey1.attack_person(person1)
    print(person1)
    
    monkey1.attack_person(person1)
    print(person1)
    
    monkey1.attack_person(person1)
    print(person1)

    # Jack的攻击力是10,剩余生命力是100
    # Ma的攻击力是20,剩余生命力是100
    # Ma的攻击力是20,剩余生命力是90
    # Jack的攻击力是10,剩余生命力是80
    # Jack的攻击力是10,剩余生命力是60
    # Jack的攻击力是10,剩余生命力是40
    # Jack的攻击力是10,剩余生命力是20
    # Jack的攻击力是10,剩余生命力是0
    # 人类挂了
    # Jack的攻击力是10,剩余生命力是0
    # 人类挂了
    # Jack的攻击力是10,剩余生命力是0
    1.编写一个餐馆类,并按要求编写程序:
    (1)创建一个名为 Restaurant 的类,其方法init()设置两个属性:
    restaurant_name 和 cuisine_type。
    (2)创建一个名为 describe_restaurant()的方法和一个名为 open_restaurant()的方法,
    其中前者打印前述两项信息,而后者打印一条消息, 指出餐馆正在营业。
    (3)根据这个类创建一个名为 restaurant 的实例,分别打印其两个属性,再调用前述
    两个方法。
    class Restaurant:
        def __init__(self,restaurant_name,cuisine_type):
            self.restaurant_name = restaurant_name
            self.cuisine_type = cuisine_type
    
        def describe_restaurant(self):
            print(self.restaurant_name)
            print(self.cuisine_type)
    
        def open_restaurant(self):
            print('餐馆正在营业')
    
    
    
    chifan = Restaurant('海底捞','火锅')
    print(chifan.restaurant_name)
    print(chifan.cuisine_type)
    chifan.describe_restaurant()
    chifan.open_restaurant()
    #1烤羊肉串
    """初始羊肉串是生的,烧烤时间是0
    0-3分钟 生的
    3-6分钟 半生不熟
    6-9分钟 熟了
    大于9分钟 烤焦了
    打印羊肉串对象,输出烤的时间是xxx,状态是xxx"""
    
    class Yangrouchuan:
        def __init__(self):
            self.shudu = '生的'
            self.time =0
    
        def kaorou(self,x_time):
    
            self.time+=x_time
            if 0<self.time<=3:
                self.shudu='生的'
                print("羊肉串还没熟呢!")
            elif 3<self.time<=6:
                self.shudu = '半生不熟'
                print("羊肉串半生不熟")
            elif 6<self.time<=9:
                self.shudu = '熟了'
                print("羊肉串已经熟了")
            else:
                self.shudu = '烤焦了'
                print("完了,烤焦了")
        def __str__(self):
            return '烤的的时间是{},状态是{}'.format(self.time,self.shudu)
    
    
    yangrouchuan = Yangrouchuan()
    yangrouchuan.kaorou(3)
    yangrouchuan.kaorou(5)
    print(yangrouchuan)
    
    # 羊肉串还没熟呢!
    # 羊肉串已经熟了
    # 烤的的时间是8,状态是熟了
    
    
    
    
    #2. 面向对象中 属性的练习
    """
    邓超 体重 75.0 公斤
    邓超每次 奔跑 会减肥 0.1 公斤
    邓超每次 吃东西 体重增加 0.5 公斤
    打印对象会显示姓名体重
    调用奔跑方法,会减肥 0.1 公斤
    调用吃东西方法,体重增加 0.5 公斤
    """
    
    class Person:
        def __init__(self,weight,name):
            self.weight = weight
            self.name = name
            self.loss_weight = 0.1
            self.add_weight = 0.5
    
        def run(self):
            self.weight-=self.loss_weight
    
        def eat(self):
            self.weight+=self.add_weight
    
        def __str__(self):
            return "我叫{},我现在体重还有{}公斤".format(self.name,self.weight)
    
    dengchao = Person(75,'邓超')
    dengchao.run()
    dengchao.eat()
    print(dengchao)
    # 我叫邓超,我现在体重还有75.4公斤













  • 相关阅读:
    char*,const char*和string的相互转换[转]
    1.0到2.0 的变化
    Hello项目分析
    VS上编译Lua5.1.4生成静态库
    cocos2dx 自带动画
    asp.net2.0安全性(Login系列控件)
    用js实现的对css的一些操作方法
    mapxtreme开发小结3(c#)
    asp.net页面事件执行顺序
    access与SqlServer 之时间与日期及其它SQL语句比较
  • 原文地址:https://www.cnblogs.com/johnsonbug/p/12702669.html
Copyright © 2011-2022 走看看