puts "nihao,ruby!" ;
=begin
Object-oriented programming
family first
=end
1.
#so far,Object-oritend programming Class study
=begin
1 task
new create user fzdc
ok
2 task
数字转字符串
2.to_s
字符串转数字
"1".to_i
=end
print "/n" ;
def add(a,b=1)
print a + b ;
end
add(1,5) ;
print "/n" ;
puts "1".to_i + 2 ;
2.
# define a Class Person
class Person
def initialize(name,age=18)
@name = name ;
@age = age ;
@motherland = "China" ;
end #initialize end
def talk
puts "my name is/t" +@name+",age is "+@age.to_s ;
if @motherland == "China" then
puts "i am chinese" ;
else
puts "i am foreigner" ;
end
end # talk end
attr_writer :motherland
end #Person end
p1 = Person.new("samba",24) ;
p1.talk ;
p2 = Person.new("gjhohj",24) ;
p2.motherland = "USA" ;
p2.talk ;
#attr_writer :motherland 相当于 set method
#attr_reader :motherland 相当于 get method
class Student < Person
def talk
puts "i am very happy!" ;
end
end
p = Student.new("nihao",10) ;
p.talk ;
3.
# test extends
class Student
def initialize(a,age=10)
puts a,"/n" ;
end
def talk
puts "nihao extends" ;
end
end
p = Student.new("a") ;
p.talk ;
def sum(a)
if a==2
puts a
return a
end
end
print sum(2)