python--the creation and application of a simple class

Source: Internet
Author: User
Tags access properties

1. Create the class, set the property and set the default value for the property, set the method and access the class's properties;

2, the use of classes to create multiple instances, as well as the method of invoking the class two ways;

3. Set the function that updates the properties, and update the properties of the instance.

1 classDog (object):2     """Create puppy Dog class"""3     4     def __init__(self, Name, age):5         """initialization parameters, when Python creates an instance, automatically passes in the argument self, pointing to the instance itself to access properties and methods"""6Self.name =name7Self.age = Age8         " "Assigning a default value to a property" "9Self.color =' White'Ten      One     defsit (self): A         " "accessing the properties of a class" " -         Print(Self.name.title () +'is now siting.') -      the     defRoll (self): -         Print(Self.name.title () +'is now rolling') -      -     " "Modifying the value of a property by method" " +     defUpdateColor (self,color): -Self.color =str (color) +         Print(Self.name.title () +"' s color is"+str (color)) A          at " "method One creates an instance by assigning a value to the parameter before it is brought into the class parameter, and then calls the methods of the class" " -Name = input ("Input your dog ' s name:\n") -Age = Input ("Input your dog ' s age:\n")   -Jacksdog =Dog (name,age) - " "This invokes the method of the class, bringing the instance into the class as a parameter" " - dog.sit (Jacksdog) in Dog.roll (Jacksdog) -  " "Method one Output: to ************************ + Input your dog ' s name: - Jack the Input your dog ' s age: * Ten $ Jack is now siting.Panax Notoginseng Jack is now rolling - ************************" " the  + " "the method has the same principle, but the variable is directly assigned by the input." " ATomsdog = Dog (Input ("Input your dog ' s name:\n"), Input ("Input your dog ' s age:\n")) the " "the method of invoking the instance directly here" " + " "multiple instances have been created" " - tomsdog.sit () $ Tomsdog.roll () $Tomsdog.updatecolor (Input ("What ' s your dog ' s color:\n")) - " "Method, Output: - ************************ the Input your dog ' s name: - TomWuyi Input your dog ' s age: the 9 - Tom is now siting. Wu Tom is now rolling - What ' s your dog ' s color: About Yellow $ Tom ' s color is yellow - ************************" "

python--the creation and application of a simple class

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.