클래스 맴버 소개
Ruby
1 2 3 4 5 6 | require 'date' d1 = Date. new ( 2000 , 1 , 1 ) d2 = Date. new ( 2010 , 1 , 1 ) p d1.year() p d2.year() p Date.today() |
클래스 메소드 (Ruby)
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Cs def Cs.class_method() p "Class method" end def instance_method() p "Instance method" end end i = Cs. new () Cs.class_method() i.instance_method() #Cs.instance_method() 오류발생 #i.class_method() 오류발생 |
클래스 메소드 (Python)
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Cs: @staticmethod def static_method(): print ( "Static method" ) @classmethod def class_method( cls ): print ( "Class method" ) def instance_method( self ): print ( "Instance method" ) i = Cs() Cs.static_method() Cs.class_method() i.instance_method() |
클래스 변수 (Ruby)
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Cs @@count = 0 def initialize() @@count = @@count + 1 end def Cs.getCount() return @@count end end i1 = Cs. new () i2 = Cs. new () i3 = Cs. new () i4 = Cs. new () p Cs.getCount() |
클래스 변수 (Python)
Python
1 2 3 4 5 6 7 8 9 10 11 12 | class Cs: count = 0 def __init__( self ): Cs.count = Cs.count + 1 @classmethod def getCount( cls ): return Cs.count i1 = Cs() i2 = Cs() i3 = Cs() i4 = Cs() print (Cs.getCount()) |
클래스 맴버의 활용 (Ruby)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | class Cal attr_reader :v1 , :v2 attr_writer :v1 @@_history = [] def initialize(v1,v2) @v1 = v1 @v2 = v2 end def add() result = @v1 + @v2 @@_history .push( "add : #{@v1}+#{@v2}=#{result}" ) return result end def subtract() result = @v1 - @v2 @@_history .push( "subtract : #{@v1}-#{@v2}=#{result}" ) return result end def setV1(v) if v.is_a?( Integer ) @v1 = v end end def getV1() return @v1 end def Cal.history() for item in @@_history p item end end end class CalMultiply < Cal def multiply() result = @v1 * @v2 @@_history .push( "multipy : #{@v1}*#{@v2}=#{result}" ) return result end end class CalDivide < CalMultiply def divide() result = @v1 / @v2 @@_history .push( "divide : #{@v1}/#{@v2}=#{result}" ) return result end end c1 = CalMultiply. new ( 10 , 10 ) p c1.add() p c1.multiply() c2 = CalDivide. new ( 20 , 10 ) p c2, c2.add() p c2, c2.multiply() p c2, c2.divide() Cal.history() |
클래스 맴버의 활용 (Python)
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | class Cal( object ): _history = [] def __init__( self , v1, v2): if isinstance (v1, int ): self .v1 = v1 if isinstance (v2, int ): self .v2 = v2 def add( self ): result = self .v1 + self .v2 Cal._history.append( "add : %d+%d=%d" % ( self .v1, self .v2, result)) return result def subtract( self ): result = self .v1 - self .v2 Cal._history.append( "subtract : %d-%d=%d" % ( self .v1, self .v2, result)) return result def setV1( self , v): if isinstance (v, int ): self .v1 = v def getV1( self ): return self .v1 @classmethod def history( cls ): for item in Cal._history: print (item) class CalMultiply(Cal): def multiply( self ): result = self .v1 * self .v2 Cal._history.append( "multiply : %d*%d=%d" % ( self .v1, self .v2, result)) return result class CalDivide(CalMultiply): def divide( self ): result = self .v1 / self .v2 Cal._history.append( "divide : %d/%d=%d" % ( self .v1, self .v2, result)) return result c1 = CalMultiply( 10 , 10 ) print (c1.add()) print (c1.multiply()) c2 = CalDivide( 20 , 10 ) print (c2, c2.add()) print (c2, c2.multiply()) print (c2, c2.divide()) Cal.history() |