Think before you speak, read before you think.

python面向对象实例

by

in

最最简单基础的结构了

#!/usr/bin/env python

class Server():
    def __init__(self, ip, hostname):
        self.ip = ip
        self.hostname = hostname
    def set_ip(self, ip):
        self.ip = ip
    def set_hostname(self, hostname):
        self.hostname = hostname
    def ping(self, ip_addr):
        print "Pinging %s from %s (%s)" % (ip_addr, self.ip, self.hostname)
if __name__ == '__main__':
    server = Server('192.168.1.244', 'ibm')
    server.ping('192.168.1.105')

解释

定义一个Server类

在类中有初始化函数,set_ip,set_hostname,ping函数

引用实例server,执行server.ping函数


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *