Python 3.5 (Classes and objects)

Source: Internet
Author: User

Classes and objects

Object-oriented programming is one of the most effective methods in object-oriented programming, where you can write classes that represent things and situations in the real world, and create objects based on those classes. When you write classes, you can also define a whole bunch of class objects that have common behavior, and each object automatically has this common behavior when you create objects based on those classes, and then you can give the object independent properties as needed.

Objects created from classes are called instantiations

Simple use effect of class

#!/use/bin/env Python3#-*-conding:utf-8-*-classDog ():#to create a dog class, the first capitalized name in Python refers to the class    def __init__(self,name,age):#__init__ is a special method that when you create an instance based on the dog class, Python runs automatically and has two underscores at the beginning and end         #three parameter self,name,age are defined in __init__, in Python the parameter self must be in front of other parameters         #when Python calls the __init__ () method to create a dog instance, it automatically passes in the argument self, and each method associated with the class will automatically pass the argument self         """Initialize property name and age"""Self.name=name Self.age= Age#The two variables defined here have a self prefix, and the variable prefixed with self in Python can be used by all methods in the class and can be accessed through any instance of the class         #for example self.name = name Gets the value stored in the parameter name and will be stored in the variable name, and then the variable is associated to the current instance Self.name=name    defSquat (self):"""define a squat method"""        Print(Self.name +": Don't call me two, I'm a dog Daniel Wu") My_dog= Dog ('two ha', 1111)#This creates a name Jiha, age 1111 dog, Python uses arguments to invoke the __init__ () method in the classPrint("Dog Name:", My_dog.name,"Age:", My_dog.age)#The value of the property Naem,age in the method classMy_dog. Squat ()#invoking methods in an instance#Create multiple InstancesMy_dog_2 = Dog ('San ha', 12)Print("Dog Name:", My_dog_2.name,"Age:", My_dog_2.age) My_dog_2.squat ()

Operating conditions are as follows

Python 3.5 (Classes and objects)

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.