Python self-Learning 1--class object-oriented into

Source: Internet
Author: User

Before the object-oriented programming has been not very understanding, today carefully analyzed, I understand the following: In Python, everything is classified, for example, people, animals, plants are belong to a certain category, which is called, class: Class and then to a certain of the classes, such as people, divided into white, black, yellow people, This is called, object: Objeck These objects are composed of people of this kind of white, white skin, blue eyes, blond hair, this is called this object, properties: Property These properties make up the way we look at properties, called methods to see the following example:[[email  protected] python]# cat human.py#!/usr/bin/python# -*- coding: utf8 -*-#*** # file name: human.py#  Author: huxianyong# Mail: [email protected]# Created Time:  September 05, 2017   Tuesday  16 45 minutes 50 seconds #************************************************************************# Man classclass human:def __init__ (self, race, hair, eyes, colour): self.race =  raceself.hair = hairself.eyes = eyesself.colour = colourdef people ( Self):p rint  "%s,%s,%s,%s"  % (self.race, self.hair, self.eyes, self.colour) man1 =  human (' White ',  ' blond ', &nbSP; ' Blue eye ',  ' white Skin ') man1.people () Man2 = human (' Black ',  ' dark hair ',  ' black eyes ',  ' black Skin ') man2.people () man3  = human (' Yellow ',  ' dark hair ',  ' black eye ',  ' yellow Skin ') man3.people () [[email protected] python]#  python human.py white, blonde, blue eyed, white black, dark hair, black eyed, black skin yellow, dark hair, black eyed, yellow skin we just need to call Man1 = human (' white ',  ' blond ',   ' Blue eye ',  ' white skin ' you can get some results by adding attributes to the object, which is called encapsulation, which is actually encapsulating the content into the object using a construction method. The following example illustrates that the class is a test object with the Hxy forand property of   name,age,sex#!/usr/bin/python# -*- coding: utf8 -*-#************************************** # file name: eg.py# author: huxianyong# mail:  [email protected]# created time: 2017 September 05   Tuesday  15 41 minutes, 02 seconds #*************** Class test:def __init__ (Self, name,  age, sex): Self.name = nameself.age = ageself.sex = sexdef action (self):p Rint&nbsP;self.name, self.age, "old", self.sex,  "go home to Dinner" print self.name, self.age, "old",  self.sex,  "Drive to Beijing" Print self.name, self.age, "old", self.sex,  "favorite Big Health Care" print  "%s,%s years old,% S, go home and get married! "  % (self.name, self.age, self.sex) print  "%s,%s-year-old,%s, drive to Tohoku!"  % (self.name, self.age, self.sex) print  "%s,%s-year-old,%s, love the big health!"  % (self.name, self.age, self.sex) obj = test (' Hxy ', 18,  ' man ') obj.action () Obj1 = test (' Forand ', 17,  ' man ') obj1.action () [[email protected] python]#  python eg.pyhxy 18   man  go home to dinner hxy 18  old  man  Drive to Beijing hxy 18  Old  man  most love big health care hxy,18 years old, man, go home to marry!hxy,18 years old, man, drive to northeast!hxy,18 years old, man, love big health! forand 17   man  go home to dinner forand 17  old  man  Drive to Beijing forand 17  year old   man  most love Big health care forand,17 years old, man, go home to get married! forand,17-year-old, man, drive to the Northeast! forand,17 years old, man, love the big health care!

These are my own understanding, there is the wrong place to hope that the great God help spend, thank you!

This article is from the "Forand" blog, make sure to keep this source http://853056088.blog.51cto.com/12966870/1962899

Python self-Learning 1--class object-oriented into

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.