分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
我们经常要在子类的initialize方法中调用super和super()。
从语法上说super和super()是有微妙区别的。
super不带括号表示调用父类的同名函数,并将本函数的所有参数传入父类的同名函数;
super()带括号则表示调用父类的同名函数,但是不传入任何参数;
演示代码如下:
- class SParent
- def initialize *args
- args.each {|arg| puts arg}
- end
- end
- class SChild < SParent
- def initialize a, b, c
- super
- end
- end
- a, b, c = *%W[a b c]
- SChild.new a, b, c # puts a, b, c if super
- SChild.new a, b, c # puts nothing if super()
可以看出当SChild的initialize中调用super()时,代码是不会打印任何信息的。这是因为super()没有向SParent的initialize方法传任何参数。
给我老师的人工智能教程打call!http://blog.youkuaiyun.com/jiangjunshow
