python--for those of you who don't know how to use Python object-oriented programming.

Source: Internet
Author: User

Object-oriented, seemingly not difficult. Some students after learning, still do not know how to use it. Sometimes you write code that says you have a hole in it, such as writing and writing, even if you're wondering if it's object-oriented programming.

Therefore, I think of a relatively simple example, to use an object-oriented way to think about it, to encode. Well, I will not do too much to explain, I think my code should be the most easy to read!

#coding:utf-8class os:     #描述操作系统对象     def __init__ (self,  os_name):        self.os_name = os_name     def start_install (self):         print ("start  INSTALL OS:{0} ". Format (self.os_name)) class host:     #描述主机对象      def __init__ (Self, host_id, brand, cpu, ram, disk):         self.host_id = host_id         self.brand = brand        self.cpu = cpu         self.ram = ram         self.disk = disk        self.os_type = none     def pOwer_off (self):         print ("Host id:{0} shutdown."). Format (self.host_id))     def power_on (self):         print ("Host id:{0} boot."). Format (self.host_id))     def install_os (self, os_name):          #服务器 (host) can install operating system         os = os (OS _name)         os.start_install ()          self.os_type = os.os_nameclass DataCenter:     #描述数据中心对象      def __init__ (self):         self.hosts_list  = []    def add_host (Self, host_id, brand, cpu, ram,  disk):         #数据中心里可以添加服务器 (host)   &NBSp;     host = host (Host_id, brand, cpu, ram, disk)         self.hosts_list.append (host)     def  Search_host (self, host_id):        for host in  self.hosts_list:            if host.host_id  == host_id:                 return host        return falseif __name__  ==  ' __main__ ':     dc = datacenter ()     dc.add_ Host (201,  "IBM",  "16",  "128GB",  "9TB")     host = dc.hosts_ List[0]    print (host.host_id, host.brand, host.cpu, host.ram,  host.disk )    &NBsp;print (Host.os_type)     host.install_os ("ubuntu14.04")     print ( Host.os_type)     host.power_on ()     host.power_off ()      search_host_ret = dc.search_host (201)     print (Search_host_ret.disk)


You can actually think like this:

1, a data center room, can have a server, then the data center can add the server, therefore, the class that describes the data center object can have a method to add the server

2, then, a server can have brand, CPU, memory and so on, then describe the server object class, should have these properties, and the server can also install the operating system, you should also design a method to install the operating system

3, since the server can install the operating system, in my design, I have the operating system also as an object, describe the operating system object class has the operating system name, and a specific installation method


Finally, for those who are still more vacant students read this article, will it be a little inspiration? My example here just plays a role, the level is limited, but also look at the vast number of Python enthusiasts criticized and pointed out. Thank you so much!


python--for those of you who don't know how to use Python object-oriented programming.

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.