• Python编程:从入门到实践—类


    创建类
    #!/usr/bin/env python
    # --*-- encoding:utf-8 --*--

    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)

    your_dog = Dog('lucy',3)
    # print("My dog's name is " + my_dog.name.title() + ".")
    # print("My dog is " + str(my_dog.age) + " years old.")

    my_dog.sit()

    print(" Your dog's name is " + your_dog.name.title() + ".")
    print("Your dog is " + str(your_dog.age) + " years old.")

    your_dog.sit()

    给属性指定默认值

    #!/usr/bin/env python
    # --*-- encoding:utf-8 --*--

    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.")

    my_new_car = Car('audi','a3',2016)

    print(my_new_car.get_descriptive_name())

    my_new_car.read_odometer()

    修改属性的值:
    1:直接修改属性的值
    #!/usr/bin/env python
    # --*-- encoding:utf-8 --*--
    
    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.")
    
    my_new_car = Car('audi','a3',2016)
    
    print(my_new_car.get_descriptive_name())
    my_new_car.odometer_reading = 23
    my_new_car.read_odometer()
    直接修改属性的值

    2:通过方法修改属性的值

    #!/usr/bin/env python
    # --*-- encoding:utf-8 --*--
    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
    
    my_new_car = Car('audi','a3',2016)
    print(my_new_car.get_descriptive_name())
    
    my_new_car.update_odometer(23)
    
    my_new_car.read_odometer()
    通过方法修改属性值
    #!/usr/bin/env python
    # --*-- encoding:utf-8 --*--
    class Car():
        def __init__(self,make,model,year):
            """模拟汽车的的属性"""
            self.make = make
            self.model = model
            self.year = year
            self.odometer_reading = 25
        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):
            """将里程表读数设置为指定的值
            禁止将里程表读数往回调"""
            if mileage >= self.odometer_reading:
                self.odometer_reading = mileage
            else:
                print("You can't roll back an odometer!")
    
    my_new_car = Car('audi','a3',2016)
    print(my_new_car.get_descriptive_name())
    
    my_new_car.update_odometer(23)
    
    my_new_car.read_odometer()
    
    执行结果:
    2016 Audi A3
    You can't roll back an odometer!
    This car has 25 miles on it.
    扩展

    3:通过方法对属性的值进行递增

    继承

    如果编写的类是另一个现成类的特殊版本,可使用继承。一个类集成另一个类时,它将自动获得另一个类的所有属性和方法;原有的类称为父类,而新类称为子类。

    子类继承了其父类的所有属性和方法,同时还可以定义自己的属性和方法。

    
    












  • 相关阅读:
    android之Fragment(官网资料翻译)
    Java获取当前时间的年月日方法
    Android Fragment Base
    Java JNI初探
    PHP 初学之登录查询小case
    PHP 初学
    tomcat:run和tomcat7:run的区别,以及Apache Tomcat Maven Plugin 相关
    Intellij IDEA:maven的本地仓库问题
    Java 构造方法的执行过程(猜测)
    PHP wamp server问题
  • 原文地址:https://www.cnblogs.com/elontian/p/10026858.html
Copyright © 2020-2023  润新知