"Python" Create and use classes

Source: Internet
Author: User
Tags access properties

Object-oriented programming is one of the most effective methods of software writing

Create a Dog class
classDog ():" "a simple test to simulate a puppy" "    def __init__(self,name,age): Self.name=name Self.age= Agedefsit (self):" "simulated puppy is ordered to squat" "        Print(Self.name.title () +"is now sitting.")    defRoll_over (self):" "simulated puppy is ordered to roll" "        Print(Self.name.title () +"rolled over!")

1. Here we define a dog class, in Python, the name of the first capitalization refers to the class

2. Method __init__ () is a special method that Python automatically runs whenever you create a new instance according to the dog class, in which there are two underscores at the beginning and the end, which is a convention designed to avoid conflicts between the Python default method and the normal method. In this method, again, the parameter self is necessary, and must precede the other parameters, and Python calls this method to create the dog instance, automatically passes in the arguments, self, and each method call that is associated with the class is automatically passed the argument, which is a reference to the instance itself. Enables an instance to access properties and methods in a class. Self is automatically passed, just passing in the next two parameters (name and age)

3.self.name, a variable prefixed with self can be used by all methods in a class, and we can access these variables through any instance of the class, Self.name = name Gets the value stored in the parameter name and stores it in the variable name. The variable is then associated to the currently created instance. Variables such as those that can be accessed through an instance are called properties.

To create an instance from a class
My_dog = Dog ('Lucy', 3)print("" + my_ Dog.name +". " )print(" + str (my_dog.age) +"  years old. ")

We can usually assume that the handwritten letter capitalization refers to the class, the lowercase name refers to the instance created by the class

1. Calling methods

My_dog = Dog ('Lucy', 3) my_dog.sit () my_dog.roll_over ()print(" "+ My_dog.name +".   " )print(" + str (my_dog.age) +"  years old. ")

2. Create multiple instances

My_dog = Dog ('Lucy', 3) Your_dog=dog ('Michile', 4)Print("My Dog ' s name is"+ My_dog.name +".")Print("My Dog is"+ STR (my_dog.age) +"years old.") my_dog.sit ()Print("your dog ' s name is"+ Your_dog.name +".")Print("your dog is"+ STR (your_dog.age) +"years old.") your_dog.sit ()
In this example, we created 2 puppies, each named Lucy and michile. Each puppy is a separate instance with its own attributes.

"Python" Create and use classes

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.