在Ruby中将类命名为“Base”似乎很常见。我不知道为什么,也不知道我是怎么想的。
比如说, ActiveRecord ActiveRecord Observer 和 Migration ,以及一个名为 Base . 与拥有一个 观察员
ActiveRecord
Observer
Migration
Base
观察员
class ActiveRecord class Observer [...] end class Migration [...] end end
与
module ActiveRecord class Base [...] end class Observer [...] end class Migration [...] end end
这个 Base
例如, ActiveRecord::Base 是Rails项目中任何活动记录模型的抽象类。模型看起来像
ActiveRecord::Base
class User < ActiveRecord::Base end
同样地, Observer 定义自己的 Observer::Base 动作控制器定义 ActionController::Base ApplicationController::Base .
Observer::Base
ActionController::Base
ApplicationController::Base
Ruby不提供和语言级别的关键字或语法来定义抽象类。从技术上讲, ActiveRecord::基本 它不是一个真正的抽象类,但它是一种惯例 对于这种模式。
ActiveRecord::基本