我刚刚在一些代码中发现了这个:
class Foo {
[...]
private:
virtual void Bar() = 0;
[...]
}
Run Code Online (Sandbox Code Playgroud)
这有什么用途吗?
(我试图将一些代码从VS移植到G ++,这引起了我的注意)
我花了一段时间才了解私有方法在Ruby中是如何工作的,这让我觉得非常尴尬.有谁知道私人方法是否有充分的理由按照它们的方式处理?这只是历史原因吗?还是实施原因?还是有很好的合理逻辑(即语义)?
例如:
class Person
private
attr_reader :weight
end
class Spy < Person
private
attr_accessor :code
public
def test
code #(1) OK: you can call a private method in self
Spy.new.code #(2) ERROR: cannot call a private method on any other object
self.code #(3) ERROR!!! cannot call a private method explicitly on 'self'
code="xyz" #(4) Ok, it runs, but it actually creates a local variable!!!
self.code="z" #(5) OK! This is the only case where explicit 'self' is ok
weight …Run Code Online (Sandbox Code Playgroud) 来自C风格语法的悠久历史,现在正在尝试学习Ruby(在Rails上),我一直在用它的成语等问题分享我的问题,但今天我遇到了一个我没想到会出现问题的问题.我无法看到任何必须在我面前的东西.
我有一个二进制类,其中包含一个私有方法,用于从路径值派生URI值(uri和路径是类的属性).我打电话self.get_uri_from_path()从内部Binary.upload(),但我得到:
Attempt to call private method
Run Code Online (Sandbox Code Playgroud)
该模型的片段如下所示:
class Binary < ActiveRecord::Base
has_one :image
def upload( uploaded_file, save = false )
save_as = File.join( self.get_bin_root(), '_tmp', uploaded_file.original_path )
# write the file to a temporary directory
# set a few object properties
self.path = save_as.sub( Rails.root.to_s + '/', '' )
self.uri = self.get_uri_from_path()
end
private
def get_uri_from_path
return self.path.sub( 'public', '' )
end
end
Run Code Online (Sandbox Code Playgroud)
我打电话不正确吗?我错过了一些更基本的东西吗?目前唯一Binary.get_uri_from_path()被调用的地方是 - Binary.upload().我希望能够在同一个类中调用私有方法,除非Ruby做了与我使用的其他语言明显不同的东西.
谢谢.
我试图学习java,当我通过访问说明符时,我有一个疑问,如果没有指定默认值和受保护的访问说明符,有什么区别?
我有一个程序,我需要在dll和一些应用程序代码之间共享一个基类.然后我有两个不同的派生类,一个在主应用程序的dll中.其中每个都有一些静态成员函数,它们对nase类中的数据进行操作.(它们需要是静态的,因为在其他地方用作函数指针).最简单的形式我的问题如下所示.
class Base {
protected:
int var ;
};
class Derived : public Base {
static bool Process( Base *pBase ) {
pBase->var = 2;
return true;
}
};
Run Code Online (Sandbox Code Playgroud)
我的编译器抱怨我无法访问pBase的受保护成员,即使Derived已经保护对Base的访问权限.有什么方法可以解决这个问题,还是我误解了什么?我可以将Base变量公开,但这会很糟糕,因为在我的实例中,这些是分配的内存块和信号量来保护它以进行多线程处理.
救命?
我private在C++中尝试访问说明符的有效性.开始:
接口:
// class_A.h
class A
{
public:
void printX();
private:
void actualPrintX();
int x;
};
Run Code Online (Sandbox Code Playgroud)
执行:
// class_A.cpp
void A::printX()
{
actualPrintX();
}
void A::actualPrintX()
{
std::cout << x:
}
Run Code Online (Sandbox Code Playgroud)
我将其构建到静态库(.a/.lib)中.我们现在有一个class_A.h和classA.a(或classA.lib)对.我编辑了class_A.h并从中删除了private:它.
现在在另一个classTester.cpp中:
#include "class_A.h" // the newly edited header
int main()
{
A a;
a.x = 12; // both G++ and VC++ allowed this!
a.printX(); // allowed, as expected
a.actualPrintX(); // allowed by G++, VC++ gave a unresolved linker error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道在篡改了一个库的标题之后所有的赌注都没有了(我的意思是,系统完整性等等)虽然方法很黑,但这真的允许吗?有办法阻止这个吗?或者我在这里做错了什么?
所以我在DLL和该类的子类中有一个抽象基类.我希望孩子们是公共的,但是要成为私人的基础,以便它不能在dll之外访问.
我怎么做?
self.class.send :method, args...当然不是.我想在类和实例级别上创建一个相当复杂的方法,而不需要复制代码.
更新:
@Jonathan Branam:这是我的假设,但我想确保没有其他人找到方法.Ruby中的可见性与Java中的可见性非常不同.你也很正确,private它不适用于类方法,虽然这将声明一个私有类方法:
class Foo
class <<self
private
def bar
puts 'bar'
end
end
end
Foo.bar
# => NoMethodError: private method 'bar' called for Foo:Class
Run Code Online (Sandbox Code Playgroud) 我可以创建一个可以通过类方法调用的私有实例方法吗?
class Foo
def initialize(n)
@n = n
end
private # or protected?
def plus(n)
@n += n
end
end
class Foo
def Foo.bar(my_instance, n)
my_instance.plus(n)
end
end
a = Foo.new(5)
a.plus(3) # This should not be allowed, but
Foo.bar(a, 3) # I want to allow this
Run Code Online (Sandbox Code Playgroud)
抱歉,如果这是一个非常基本的问题,但我无法以谷歌的方式找到解决方案.
response_to可能不是那么明显吗?在红宝石中工作.考虑一下:
class A
def public_method
end
protected
def protected_method
end
private
def private_method
end
end
obj = A.new
obj.respond_to?(:public_method)
# true - that's pretty obvious
obj.respond_to?(:private_method)
# false - as expected
obj.respond_to?(:protected_method)
# true - WTF?
Run Code Online (Sandbox Code Playgroud)
因此,如果'obj'响应protected_method,我们应该期待
obj.protected_method
Run Code Online (Sandbox Code Playgroud)
我不应该提出异常吗?
......但它明显提升了
调用respond_to的文档点?第二个参数设置为true也检查私有方法
obj.respond_to?(:private_method, true)
# true
Run Code Online (Sandbox Code Playgroud)
这更合理
所以问题是如何检查对象是否只响应公共方法?有没有比这更好的解决方案?
obj.methods.include?(:public_method)
# true
obj.methods.include?(:protected_method)
# false
Run Code Online (Sandbox Code Playgroud) access-specifier ×10
ruby ×5
c++ ×3
private ×3
c# ×1
class-method ×1
derived ×1
dll ×1
java ×1
oop ×1
overriding ×1
scope ×1
static ×1
visibility ×1