重载关系
一组函数要重载,必须处在同一个作用域中 ,而且函数名字相同,参数列表不同
代码1中的Base中的 show() 和show(int) 属于重载
代码2中的Base中的 show() 和Derive中的show()不属于重载不在同一个作用域下面
隐藏/重定义的关系(主要是指作用域隐藏)
在继承结构当中,当子类和父类中有同名成员时,子类成员会隐藏父类成员.子类成员和父类成员构成隐藏关系,也叫重定义。
只要函数名相同,就构成隐藏关系。想要调用父类的成员就要指定作用域,显式的调用。
子类把基类的同名成员全部都给隐藏掉了,只要名字相同就会发生隐藏,无所谓子类函数的返回值,参数列表是否与父类一致.
例如代码2中的 Derive中的show() 和Base()中的show() ,show(int) 是隐藏关系
隐藏发生的主要原因,就是当子类有父类的同名成员时,子类对象访问该成员时,会发生冲突。所以编译器的处理方式是,优先考虑子类域中的自身成员。
即,子类对象访问某成员时,如ch.m_m 或者ch.f(),成员变量和成员函数都一样。编译器首先在子类域中检索,如果在子类域中找到该成员,则检索结束,返回该成员进行访问。
如果在子类域中找不到该成员,则去父类域中检索。如果父类域中存在,则返回该成员进行访问,如果父类域中也不存在,则编译错误,该成员无效。
当父子类域都存在同一成员时,编译器优先在子类中检索,就算父类域中也存在该同名成员,也不会被检索到。
因此,父类域中的该成员被子类域中的该同名成员隐藏,即访问时完全以为该成员不存在,如果想访问父类域中的该成员,只能通过显示调用的方式,即:ch.Father::m_m;
代码1
class Base
{
public:
Base(int data=10):ma(data){
cout<<"Base"<<endl;
}
void show(){cout<<"Base Show()"<<endl;}
void show(int){cout<<"Base Show(int)"<<endl;}
~Base(){cout<<"~Base()"<<endl;}
protected:
int ma;
};
class Derive : public Base
{
public:
Derive(int data=20):Base(data),mb(data){
cout<<"Derive"<<endl;
}
~Derive(){cout<<"~Derive()"<<endl;}
private:
int mb;
};
int main(){
Derive d(20);
d.show(); //正常调用基类show()
d.show(100); //正常调用基类show(int)
return 0;
}
代码2
class Base
{
public:
Base(int data=10):ma(data){
cout<<"Base"<<endl;
}
void show(){cout<<"Base Show()"<<endl;}
void show(int){cout<<"Base Show(int)"<<endl;}
~Base(){cout<<"~Base()"<<endl;}
protected:
int ma;
};
class Derive : public Base
{
public:
Derive(int data=20):Base(data),mb(data){
cout<<"Derive"<<endl;
}
void show(){cout<<"Derive Show()"<<endl;}
~Derive(){cout<<"~Derive()"<<endl;}
private:
int mb;
};
int main(){
Derive d(20);
d.show(); //调用子类show()
d.show(100);//调用报错 报错信息 "Derive::show()函数不接受1个参数"
// 即 Derive中的show()方法把Base中的show()和show(int)都给隐藏掉了
// 所以d.show()没问题,调用的是派生类的show(),但是d.show(100)报错了,因为
// 父类的show()和show(int)都被隐藏了,而派生类Derive中没有 show(int)方法所以报错了
// 如果想调用父类的show(int) 要这样写 d.Base.show(100);
return 0;
}
基类对象 -> 派生类对象 类型由上向下转 NOT OK
Base b(10);
Derive d(20);
d=b;// NOT OK
派生类对象 ->基类对象 类型由下向上转 OK
Base b(10);
Derive d(20);
b=d;//OK

派生类指针(引用) ->基类指针 类型由下向上转 OK
Base b(10);
Derive d(20);
Base *pb =&d;// OK 如下图, 基类指针只能访问到基类那一部分的成员,所以是安全的

代码3
class Base
{
public:
Base(int data=10):ma(data){
cout<<"Base"<<endl;
}
void show(){cout<<"Base Show()"<<endl;}
void show(int){cout<<"Base Show(int)"<<endl;}
~Base(){cout<<"~Base()"<<endl;}
protected:
int ma;
};
class Derive : public Base
{
public:
Derive(int data=20):Base(data),mb(data){
cout<<"Derive"<<endl;
}
void show(){cout<<"Derive Show()"<<endl;}
~Derive(){cout<<"~Derive()"<<endl;}
private:
int mb;
};
int main(){
Base b(10);
Derive d(20);
Base *pb =&d;
pb->show(); //调用的是基类的 show
pb->show(100);//调用的是基类的 show(int)
((Derive *)pb)->show(); //强转后 调用的是派生类的 show
}
基类指针(引用) -> 派生类对象 类型由上向下转 NOT OK
Base b(10);
Derive d(20);
Derive *pb =&b;// NOT OK pb指针能够访问的区域超过了实际对象b的内存块 ,危险访问
代码4
#include <iostream>
using namespace std;
class Base{
public:
Base(){
cout<<"Base()"<<endl;
}
void show(){
cout<<"Base show()"<<endl;
}
void show(int x){
cout<<"Base show(int x)"<<endl;
}
~Base(){
cout<<"~Base()"<<endl;
}
private:
int ma;
};
class Derive :public Base{
public:
Derive(){
cout<<"Derive()"<<endl;
}
void show(){
cout<<"Derive show()"<<endl;
}
~Derive(){
cout<<"~Derive()"<<endl;
}
private:
int ma;
};
int main(){
Derive d;
Derive *pd=&d;
d.show();
d.show(100); //编译报错, Derive 的void show()方法把Base 的void show() void show(int x) 都隐藏了
pd->show(100);//编译报错 Derive 的void show()方法把Base 的void show() void show(int x) 都隐藏了
return 0;
}
代码5
#include <iostream>
using namespace std;
class Base{
public:
Base(){
cout<<"Base()"<<endl;
}
virtual void show(){
cout<<"Base show()"<<endl;
}
void show(int x){
cout<<"Base show(int x)"<<endl;
}
~Base(){
cout<<"~Base()"<<endl;
}
private:
int ma;
};
class Derive :public Base{
public:
Derive(){
cout<<"Derive()"<<endl;
}
virtual void show(){
cout<<"Derive show()"<<endl;
}
~Derive(){
cout<<"~Derive()"<<endl;
}
private:
int ma;
};
int main(){
Derive *pd=new Derive();
pd->show(100); //编译报错,Derive 中的show() 函数,只要名字与Base中有相同名字的函数的,就会隐藏掉Base中所有的show方法(不管加不加virtual),包括void show() void show(int x)
return 0;
}
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
在Ruby类中,我重写了三个方法,并且在每个方法中,我基本上做同样的事情:classExampleClassdefconfirmation_required?is_allowed&&superenddefpostpone_email_change?is_allowed&&superenddefreconfirmation_required?is_allowed&&superendend有更简洁的语法吗?如何缩短代码? 最佳答案 如何使用别名?classExampleClassdefconfirmation_required?is_a
我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll
我在一个我想在formtasticGem中覆盖的方法中找到了这个。该方法如下所示:defto_htmlinput_wrappingdohidden_field_html是什么意思?在第三行做什么?我知道它对数组有什么作用,但在这里我不知道。 最佳答案 你可以这样读:hidden_field_htmllabel_with_nested_checkbox是连接到hidden_field_html末尾的参数-为了“清晰”,他们将其分成两行 关于ruby-on-rails-没有参数的`
我已经看到了一些其他的问题,尝试了他们的建议,但没有一个对我有用。我已经使用Rails大约一年了,刚刚开始一个新的Rails项目,突然遇到了问题。我卸载并尝试重新安装所有Ruby和Rails。Ruby很好,但Rails不行。当我输入railss时,我得到了can'tfindgemrailties。我当前的Ruby版本是ruby2.2.2p95(2015-04-13修订版50295)[x86_64-darwin15],尽管我一直在尝试通过rbenv设置ruby2.3.0。如果我尝试rails-v查看我正在运行的版本,我会得到同样的错误。我使用的是MacOSXElCapitan版本10
假设您编写了一个类Sup,我决定将其扩展为SubSup。我不仅需要了解你发布的接口(interface),还需要了解你的私有(private)字段。见证这次失败:classSupdefinitialize@privateField="fromsup"enddefgetXreturn@privateFieldendendclassSub问题是,解决这个问题的正确方法是什么?看起来子类应该能够使用它想要的任何字段而不会弄乱父类(superclass)。编辑:equivalentexampleinJava返回"fromSup",这也是它应该产生的答案。 最佳答案