・ I ・ Experienced in Java and C #
・ The amount of information is tremendous. If you think that the amount of information is too large and you are doing it in order from the top (while doing business), it will not end. ・ Although there is already a similar article, I decided to make a dictionary-like thing in my own words, "This is Java."
・ * Strictly speaking, it may not be equal, but I feel that it is posted with the feeling that it is this if you force it. ・ ※ I would like you to point out if there is something that "the understanding is wrong" because you are learning. -Since the volume is small at this stage, the concept unique to ruby is also included.
description
method in ** test.rb **"
・ ** This ** in java
・ See ** test.rb ** </ font>test.rb
class Car
#Object variable (value)
# type(Car type),weight(weight),user(User)
attr_accessor :type,:weight,:user
#Special method for initialization
def initialize
self.type = 'SUV'
self.weight = 2500
end
#Object method (processing)
def description(user)
puts "Hello#{user},This car type is#{type},Weight is#{power}is."
end
end
#Create an instance and assign it to a variable
Car_A = Car.new
Car_A.description('I')
p Car_A
・ The one attached to the instance variable ・ Agree with java instance variable * 1)
test2.rb
class Car
#Object variable (value)
# type(Car type),weight(weight),user(User)
attr_accessor :type,:weight,:user
#Special method for initialization
def initialize
@type = 'SUV'
@weight = 2500
end
#Object method (processing)
def description(user)
puts "Hello#{user},This car type is#{@type},Weight is#{@power}is."
end
end
#Create an instance and assign it to a variable
Car_A = Car.new
Car_A.description('I')
p Car_A
・ The one attached to the class variable -Can be used in classes and instances ・ Values are shared ・ See ** test3.rb ** </ font>
test3.rb
class Test
@@class_var = 0
def foo
@@class_var += 1
p @@class_var
end
def self.hoge
@@class_var += 1
p @@class_var
end
end
test = Test.new
#Values are shared within the same instance
test.foo # => 1
test.foo # => 2
#Values are shared even if the instances are different
test2 = Foo.new
test2.foo # => 3
#It can be accessed from class methods and the values are shared.
Test.hoge # => 4
・ I wanted to write import ... in java, but in reality, require seems to have a higher degree of freedom (see the link below). ・ [Ruby] Ruby require is different from Java
・ I couldn't find this in Java. .. ..
-** Difference from class ** --Unable to instantiate. --Cannot be inherited.
-** Usage ** --Namespace (Ensuring method uniqueness ... To prevent duplication of method names when importing libraries, etc.) --Mix-in (Use by including all methods in a module)
Recommended Posts