Python learning notes-class

Source: Internet
Author: User
# Coding = 'utf-8' class user (): def _ init _ (self, name, sex): Self. name = Name self. sex = sex print 'here is the constructor 'def setname (self, name): Self. name = Name def getname (Self): return self. nameuser = user ("Python", 'male') user. setname ("Ruby") print user. getname () print user. nameprint user. sex #============== print result ====================## here is the constructor # Ruby # male

Ruby class definition

 
Require 'jcode' $ kcode = 'U' class user attr_accessor: name,: Sex def initialize (name, sex) @ name = Name @ sex = sex P' this is a constructor 'end def get_name @ name end def set_name (name) @ name = Name enduser = user. new ('Ruby ', '11') user. set_name ('rubyruby ') P user. get_namep user. namep user. sex

Javascript class definition

Function user (name, sex) {This. name = Name; this. sex = sex; alert ("here is the constructor")} user. prototype = {getname: function () {return this. name}, setname: function (name) {This. name = Name} var user = new user ("XXX", 'man') user. setname ("yyy"); alert (user. name) Alert (user. sex)
 
#-*-Coding: UTF-8-*-# This is a simple class used to output a floating point number class roundfloatmanual (object) precise to the second digit after the decimal point ): def _ init _ (self, Val): assert isinstance (Val, float), \ "value must be a float" self. value = round (Val, 2) def _ STR _ (Self): Return '%. 2f '% self. valuerfm = roundfloatmanual (4.288) print RFM
#-*-Coding: UTF-8-*-class time60 (object): 'hour and minute addition and subtraction operations 'def _ init _ (self, HR = 0, min = 0): Self. hR = HR self. min = min def _ STR _ (Self): Return '% d: % d' % (self. HR, self. min) _ repr _ = _ STR _ def _ add _ (self, other): A = divmod (self. min + other. min, 60) return self. _ class _ (self. HR + other. HR + A [0], a [1]) def _ iadd _ (self, other): A = divmod (self. min + other. min, 60) self. HR + = self. HR + other. HR self. min + = A [1] Return selfa = time60 (10, 30) B = time60 (8, 45) print a + B
#-*-Coding: UTF-8 -*-

Class numstr (object ):
Def _ Init (self, num = 0, string = ''):
Self. _ num = num
Self. _ string = string

Def _ STR _ (Self ):
Return '[% d: % R]' % \
(Self. _ num, self. _ string)
_ Repr _ = _ STR __

def _ add _ (self, other):
If (isinstance (Other, numstr):
return self. _ class _ (self. _ num + other. _ num, self. _ string + self. _ string)
else:
raise typeerror, 'Type error'
def _ Mul _ (self, num ):
If (isinstance (Num, INT):
return self. _ class _ (self. _ num * num, self. _ string * num)
else:
raise typeerror, 'Type error'
def _ nonzero _ (Self):
return self. _ num or Len (self. _ string)
def _ norm_cval (self, cmpres):
return CMP (cmpres, 0)
def _ CMP _ (self, other):
return self. _ norm_cval (CMP (self. _ num, other. _ num) + \
self. _ norm_cval (CMP (self. _ string, other. _ string)

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.