zoukankan      html  css  js  c++  java
  • python入门-类(一)

    1 最简单的一个类

    class Dog():
        """一次模拟小狗的简单尝试"""
        def __init__(self,name,age):
            """初始化属性name和age"""
            self.name = name
            self.age = age
    
        def sit(self):
            print(self.name.title() + " is now sitting.")
    
        def roll_over(self):
            print(self.name.title() + " rolled over")
    
    
    my_dog = Dog('willie',6)
    print("My dog's name is " + my_dog.name.title() + ".")
    print("My dog is " + str(my_dog.age) + "years old.")
    
    my_dog.sit()
    my_dog.roll_over()

    __init__是初始化类

    类的属性和方法的 访问方式  默认的self 是自身,函数可以编写这个形参,但是实际不用传self

    2 一个类 可以创建多个实例

    class Dog():
        """一次模拟小狗的简单尝试"""
        def __init__(self,name,age):
            """初始化属性name和age"""
            self.name = name
            self.age = age
    
        def sit(self):
            print(self.name.title() + " is now sitting.")
    
        def roll_over(self):
            print(self.name.title() + " rolled over")
    
    
    my_dog = Dog('willie',6)
    print("My dog's name is " + my_dog.name.title() + ".")
    print("My dog is " + str(my_dog.age) + "years old.")
    
    my_dog.sit()
    my_dog.roll_over()
    
    your_dog = Dog('lucy',3)
    print("Your dog's name is " + your_dog.name.title() + ".")
    print("Your dog is " + str(your_dog.age) + "years old.")

    3 类的多个用法

    class Car():
        """一次模拟汽车的简单尝试"""
        def __init__(self,make,model,year):
            """初始化属性"""
            self.make = make
            self.model = model
            self.year = year
            self.odometer_reading = 0
    
    
        def get_descriptive_name(self):
            """返回整洁的描述性信息"""
            long_name = str(self.year) + " " + self.make + '' + self.model
            return long_name.title()
    
        def read_odometer(self):
            """打印汽车里程的信息"""
            print("This car has " + str(self.odometer_reading) + "miles on it")
    
        def update_odometer(self,mileage):
            self.odometer_reading = mileage
    
        def increment_odometer(self,miles):
            self.odometer_reading += miles
    
        def fill_gas_tank(self):
            print("This car doesn't need a gas tank")
    
    my_new_car = Car('audi', 'a4' ,'2016')
    print(my_new_car.get_descriptive_name())
    my_new_car.read_odometer()
    
    my_new_car.odometer_reading=23
    my_new_car.read_odometer()
    
    my_new_car.update_odometer(36)
    my_new_car.read_odometer()
    
    my_new_car.increment_odometer(100)
    my_new_car.read_odometer()

    可以给类添加默认值,也可以修改类中的属性的值 多重练习

    4 类的继承

    class ElectricCar(Car):
        """电动汽车的独特之处"""
    
        def __init__(self, make, model,year):
            """初始化父类的属性"""
            super().__init__(make,model,year)
            self.battery_size = 70
    
        def describe_battery(self):
            print("This car has a " + str(self.battery_size) +"-kwh battery.")
    
        def fill_gas_tank(self):
            print("This is OK!")
    
    my_tesla = ElectricCar('tesla','model s',2016)
    print(my_tesla.get_descriptive_name())
    my_tesla.describe_battery()
    my_tesla.fill_gas_tank()

    紧跟着上面,创建一个子类,可以在子类中复写父类的方法

    子类和父类使用关键字super()关联   父类也叫做超类,这是super的由来

    5  把类集中放到 其他的类中,也可以方便的调用

    class Battery():
        def __init__(self,battery_size=70):
            """初始化电瓶的属性"""
            self.battery_size = battery_size
    
        def describe_battery(self):
            """打印一天描述电瓶容量的消息"""
            print("This car has a " +str(self.battery_size) + "-kwh battery.")
    
        def get_range(self):
            if self.battery_size == 70:
                range = 240
            elif self.battery_size == 85:
                range = 270
    
            message = "This car can go approximately " + str(range)
            message +=" miles on a full charge"
            print(message)
    
    class ElectricCar(Car):
        """电动汽车的独特之处"""
    
        def __init__(self, make, model,year):
            """初始化父类的属性"""
            super().__init__(make,model,year)
            self.battery = Battery()
    
    
    
    my_tesla = ElectricCar('tesla','model s',2016)
    print(my_tesla.get_descriptive_name())
    my_tesla.battery.describe_battery()
    my_tesla.battery.get_range()
  • 相关阅读:
    创业日志N,一听到别人说创业我就怕
    《创业维艰分享之五》所有得,有所乐,日事日清,循序渐进。
    创业维艰--分享技术人做产品与做市场的一些经验
    创业维艰--最艰难的时候
    angular js 在ie11 下的get请求缓存问题的解决办法
    《创业维艰-分享创业中遇到的困难之二》----破局
    xwalk_core_library-15.44.384 .13.aar 百度云分享
    linux常用命令
    Merge into 用法
    多线程的并发控制
  • 原文地址:https://www.cnblogs.com/baker95935/p/9290392.html
Copyright © 2011-2022 走看看