Learning notes for classes in Python (source version)

Source: Internet
Author: User

1.1 First paragraph code

#定义一个类(define a class )class Cat:        #属性(attribution)        #方法(methods)        def eat(self):                print("cat is eating a fish.")        def drink(slef):                print("cat is drinking milk.")        def introduce(self):                print("%s‘s age is %d"%(tom.chinese_name,tom.age))#创建一个对象(creating an object)tom = Cat()#调用一个对象的方法(method to invoke an object)tom.eat()tom.drink()#蠢办法添加属性(stupid method to add attributions)tom.chinese_name = "汤姆"tom.age = 18#获取对象的属性(the first way to get  properties of objects )tom.introduce()#创建多个对象,这里创建第二个对象blue_cat(create multiple objects,and the second creates bule cat)blue_cat = Cat()blue_cat.chinese_name = "蓝猫"blue_cat.age = 8blue_cat.introduce()

1.2 Executing the output of the first piece of code

[[email protected] class]# python test.py cat is eating a fish.cat is drinking milk.汤姆‘s age is 18汤姆‘s age is 18    ‘‘‘那这里我们可以看到两个不同的对象调用方法后输出了相同的结果,这就出现问题了,那怎么解决上面的问题,且看下面的代码。‘‘‘

2.1 2nd Way
Self's addition solves the problem above.

#定义一个类(define a class )class Cat:        #属性(attribution)        #方法(methods)        def eat(self):                print("cat is eating a fish.")        def drink(slef):                print("cat is drinking milk.")        def introduce(self):                print("%s‘s age is %d."%(self.chinese_name,self.age))#创建一个对象(creating an object)tom = Cat()#调用一个对象的方法(method to invoke an object)tom.eat()tom.drink()#蠢办法添加属性(stupid method to add attributions)tom.chinese_name = "汤姆"tom.age = 18#获取对象的属性(the first way to get  properties of objects )tom.introduce()#创建多个对象,这里创建第二个对象blue_cat(create multiple objects,and the second creates bule cat)blue_cat = Cat()blue_cat.chinese_name = "蓝猫"blue_cat.age = 8blue_cat.introduce()

2.2 Output of the second segment of the Code

[[email protected] class]# python test.py cat is eating a fish.cat is drinking milk.汤姆‘s age is 18.蓝猫‘s age is 8.

3 Use of init and STR

class Cat:        """定义了一个Cat类"""        #初始化对象        def __init__(self, new_name, new_age):                self.name = new_name                self.age = new_age        def __str__(self):            return "%s的年龄是:%d"%(self.name,self.age)        #方法        def eat(self):                print("猫在吃鱼....")        def drink(self):                print("猫正在喝牛奶.....")        def introduce(self):                print("%s的年龄是:%d"%(self.name, self.age))#创建一个对象tom = Cat("汤姆", 40)bule_cat = Cat("蓝猫", 10)print(tom)print(bule_cat)[[email protected] class]# python test2.py 汤姆的年龄是:40蓝猫的年龄是:10

Learning notes for classes in Python (source version)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.