python dog类基本写法

本文介绍了Python中创建dog类的基本方法,包括静态属性和行为属性的设定。类的实例化过程以及init方法的使用被详细阐述,同时探讨了类变量与实例变量的区别,展示了如何通过方法定义变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

  1. 一个抽象的dog类包含静态属性和行为属性
  2. 对象是类的具体化。
  3. 一个类可以有n个对象。
  4. class 可以通过dot: . 直接引用函数或类变量。
# Python program to 
# demonstrate instantiating 
# a class 
  
class Dog:  
      
    # A simple class 
    # attribute 
    attr1 = "mamal"
    attr2 = "dog"
  
    # A sample method   
    def fun(self):  
        print("I'm a", self.attr1) 
        print("I'm a", self.attr2) 
  
# Driver code 
# Object instantiation 
dog1 = Dog() 
  
# Accessing class attributes 
# and method through objects 
print(dog1.attr1) 
dog1.fun() 

mamal
I’m a mamal
I’m a dog

  1. init method直接执行, 一旦建立对象, 直接赋值。
# A Sample class with init method  
class Person:  
    
    # init method or constructor   
    def __init__(self, name):  
        self.name = name  
    
    # Sample Method   
    def say_hi(self):  
        print('Hello, my name is', self.name)  
    
p = Person('Li Lei')  
p.say_hi()  

Hello, my name is Li Lei

p.name
'Li Lei'
  1. 类变量和实例变量
# Python program to show that the variables with a value   
# assigned in the class declaration, are class variables and  
# variables inside methods and constructors are instance  
# variables.  
     
# Class for Dog  
class Dog:  
    
    # Class Variable  
    animal = 'dog'             
    
    # The init method or constructor  
    def __init__(self, breed, color):  
      
        # Instance Variable      
        self.breed = breed 
        self.color = color         
     
# Objects of Dog class  
Rodger = Dog("Pug", "brown")  
Buzo = Dog("Bulldog", "black")  
  
print('Rodger details:')    
print('Rodger is a', Rodger.animal)  
print('Breed: ', Rodger.breed) 
print('Color: ', Rodger.color) 
  
print('\nBuzo details:')    
print('Buzo is a', Buzo.animal)  
print('Breed: ', Buzo.breed) 
print('Color: ', Buzo.color) 
  
# Class variables can be accessed using class  
# name also  
print("\nAccessing class variable using class name") 
print(Dog.animal)         
Rodger details:
Rodger is a dog
Breed:  Pug
Color:  brown

Buzo details:
Buzo is a dog
Breed:  Bulldog
Color:  black

Accessing class variable using class name
dog
  1. 通过方法定义变量
# Python program to show that we can create   
# instance variables inside methods  
     
# Class for Dog  
class Dog:  
        
    # Class Variable  
    animal = 'dog'      
        
    # The init method or constructor  
    def __init__(self, breed):  
            
        # Instance Variable  
        self.breed = breed              
    
    # Adds an instance variable   
    def setColor(self, color):  
        self.color = color  
        
    # Retrieves instance variable      
    def getColor(self):      
        return self.color     
    
# Driver Code  
Rodger = Dog("pug")  
Rodger.setColor("brown")  
print(Rodger.getColor())   

brown

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值