美文网首页python
python(17):类(1)

python(17):类(1)

作者: Z_bioinfo | 来源:发表于2022-04-10 20:25 被阅读0次

    1.创建和使用类

    ###1.创建dog()类
    class Dog():#定义一个名为dog()的类,类的首字母得大写
        #一次模拟小狗的简单尝试
        def __init__(self, name, age):#类中的函数称为方法,定义了self, name, age三个形参,必须在方法定义中包含形参self,因为python调用 _init__()方法创建Dog()类时,将自动传入实参self
            #每个与类相关联方法调用都自动传递实参self,它是一个指向实例本身的引用,让示例能够访问类中的属性和方法
            #初始化属性name和age
            #只需给后两个形参提供值
            self.name = name
            self.age = age
            
            def sit(self):
                #模拟小狗被命令时蹲下
                print(self.name + 'is now sitting')
                
            def roll_over(self):
                #模拟小狗被命令时打滚
                print(self.name + 'is now rolled over')
    ===========================================
    ###2.根据类创建实例
    class Dog():#定义一个名为dog()的类,类的首字母得大写
        #一次模拟小狗的简单尝试
        def  __init__(self, name, age):#类中的函数称为方法,定义了self, name, age三个形参,必须在方法定义中包含形参self,因为python调用 _init__()方法创建Dog()类时,将自动传入实参self
            #每个与类相关联方法调用都自动传递实参self,它是一个指向实例本身的引用,让示例能够访问类中的属性和方法
            #初始化属性name和age
            #只需给后两个形参提供值
            self.name = name
            self.age = age
            
        def sit(self):
                #模拟小狗被命令时蹲下
                print(self.name + 'is now sitting')
                
        def roll_over(self):
                #模拟小狗被命令时打滚
                print(self.name + 'is now rolled over')
    
    my_dog = Dog('da ming', 3)
    #访问属性
    print('my dog name is ' + my_dog.name)
    print('my dog is ' + str(my_dog.age) +  'years old')
    my dog name is da ming
    my dog is 3years old
    
    #调用方法
    my_dog.sit()
    my_dog.roll_over()
    da mingis now sitting
    da mingis now rolled over
    #创建多个实例
    class Dog():
        #一次模拟小狗的简单尝试
        def __init__(self, name, age)
            self.name = name
            self.age = age
            
        def sit(self):
                
                print(self.name + 'is now sitting')
                
        def roll_over(self):
                
                print(self.name + 'is now rolled over')
    
    my_dog = Dog('da ming', 3)
    your_dog = Dog('ni ni', 3)
    
    print('my dog name is ' + my_dog.name)
    print('my dog is ' + str(my_dog.age) +  'years old')
    my_dog.sit()
    
    print('\nyour dog name is ' + your_dog.name)
    print('your dog is ' + str(your_dog.age) +  'years old')
    your_dog.sit()
    my dog name is da ming
    my dog is 3years old
    da mingis now sitting
    
    your dog name is ni ni
    your dog is 3years old
    ni niis now sitting
    

    2.使用类和实例

    #1.编写一个表示汽车的类
    class Car():
        #一次模拟汽车的简单尝试
        def __init__(self, make, model, year):#初始化描述汽车的属性
            self.make = make
            self.model = model
            self.year = year
        def get_descriptive_name(self):
            #返回整洁的描述信息
            long_name = str(self.year) + ' ' + self.make + ' ' + self.model
            return long_name
    my_new_car = Car('audi', 'a4', 2016)
    print(my_new_car.get_descriptive_name())
    2016 audi a4
    ==========================================
    #2.给属性指定默认值
    class Car():
        
        def __init__(self, make, model, year):
            #初始化描述汽车的属性
            self.make = make
            self.model = model
            self.year = year
            self.odometer_reading = 0 #添加一个名为odometer_reading的属性,初始值为0
        def get_descriptive_name(self):
            
            long_name = str(self.year) + ' ' + self.make + ' ' + self.model
            return long_name
        def read_odometer(self):
            #打印一条指出汽车里程的消息
            print('this car has ' + str(self.odometer_reading) + ' miles on it')
            
    my_new_car = Car('audi', 'a4', 2016)
    print(my_new_car.get_descriptive_name())
    my_new_car.read_odometer()
    2016 audi a4
    this car has 0 miles on it
    ===================================================
    #修改属性的值
    #直接修改
    class Car():
        
        def __init__(self, make, model, year):
            #初始化描述汽车的属性
            self.make = make
            self.model = model
            self.year = year
            self.odometer_reading = 0 #添加一个名为odometer_reading的属性,初始值为0
        def get_descriptive_name(self):
            
            long_name = str(self.year) + ' ' + self.make + ' ' + self.model
            return long_name
        def read_odometer(self):
            #打印一条指出汽车里程的消息
            print('this car has ' + str(self.odometer_reading) + ' miles on it')
            
    my_new_car = Car('audi', 'a4', 2016)
    print(my_new_car.get_descriptive_name())
    
    my_new_car.odometer_reading = 23#直接修改
    my_new_car.read_odometer()
    2016 audi a4
    this car has 23 miles on it
    ================================
    #通过方法修改属性的值
    class Car():
        
        def __init__(self, make, model, year):
            #初始化描述汽车的属性
            self.make = make
            self.model = model
            self.year = year
            self.odometer_reading = 0 #添加一个名为odometer_reading的属性,初始值为0
        def get_descriptive_name(self):
            
            long_name = str(self.year) + ' ' + self.make + ' ' + self.model
            return long_name
        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', 'a4', 2016)
    print(my_new_car.get_descriptive_name())
    
    my_new_car.update_odometer(23)
    my_new_car.read_odometer()
    2016 audi a4
    this car has 23 miles on it
    ===========================
    #进行扩展
    class Car():
        
        def __init__(self, make, model, year):
            #初始化描述汽车的属性
            self.make = make
            self.model = model
            self.year = year
            self.odometer_reading = 0 #添加一个名为odometer_reading的属性,初始值为0
        def get_descriptive_name(self):
            
            long_name = str(self.year) + ' ' + self.make + ' ' + self.model
            return long_name
        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 not roll back an odometer')
    
            
            
    my_new_car = Car('audi', 'a4', 2016)
    print(my_new_car.get_descriptive_name())
    
    my_new_car.update_odometer(23)
    my_new_car.read_odometer()
    ===================================
    #通过方法对属性的值进行递增
    class Car():
        
        def __init__(self, make, model, year):
            #初始化描述汽车的属性
            self.make = make
            self.model = model
            self.year = year
            self.odometer_reading = 0 #添加一个名为odometer_reading的属性,初始值为0
        def get_descriptive_name(self):
            
            long_name = str(self.year) + ' ' + self.make + ' ' + self.model
            return long_name
        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
    my_new_car = Car('audi', 'a4', 2016)
    print(my_new_car.get_descriptive_name())
    
    my_new_car.update_odometer(23500)
    my_new_car.read_odometer()    
    
    my_new_car.increment_odometer(100)
    my_new_car.read_odometer()
    2016 audi a4
    this car has 23500 miles on it
    this car has 23600 miles on it
    

    相关文章

      网友评论

        本文标题:python(17):类(1)

        本文链接:https://www.haomeiwen.com/subject/kpcysrtx.html