我有这个问题,因为单例/命名构造函数.在这两种情况下,真正的构造函数都是受保护的或私有的,两者都不能从外部访问.
例如,一个简短的命名构造函数是这样的:
class A
{
public:
static A createA() { return A(0); } // named constructor
private:
A (int x);
};
int main(void)
{
A a = A::createA();
}
Run Code Online (Sandbox Code Playgroud)
我认为静态方法只能访问静态数据成员,或通过现有对象访问私有数据/方法.但是,在上面的代码中,私有构造函数A()不是静态的,并且在调用它时,也不存在任何对象.所以我能想到的唯一解释是静态方法可以访问同一个类的非静态私有方法.任何人都可以肯定或否定我的想法,可能有一些解释?
我很抱歉,如果这太微不足道,但关键词太常见了,我无法在几十个谷歌页面找到答案.提前致谢.
我有一个充满静态方法的Python类.将这些包装在一个类而不是原始函数中有哪些优点和缺点?
我有一个静态成员函数,它只是我的语法糖,我希望它的主体出现在通过参数传递给它的运动的位置.将
inline static foo(int a) {return a & 0x00000040;}
Run Code Online (Sandbox Code Playgroud)
如果inline不存在的话就像内联一样内联static?
像我之前的许多人一样,我正在努力让我的派生类型自动注册到我的工厂.我读了很多问题,并试着把注意力集中在我没有找到的地方.
除了自动注册外,我的一切运行都很顺利.
我的目标:
是)我有的:
template <class T>
class abstract_factory
{
public:
template < typename Tsub > static void register_class();
static T* create( const std::string& name );
private:
// allocator<T> is a helper class to create a pointer of correct type
static std::map<std::string, boost::shared_ptr<allocator<T> > > s_map;
}; …Run Code Online (Sandbox Code Playgroud) 我认为这是完全不可能的,但如果.在任何版本的C++中,是否有可能以某种方式在静态成员函数中获取封闭类的类型?
class Impossible {
public:
static void Fun()
{
typedef Impossible EnclosingClass;
// now do something with EnclosingClass ...
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法获得封闭类的类型(Impossible在这种情况下)而不在函数中写入类的名称?
我之所以这样做,是为了避免在函数中重复类名.如果发生这样的事情,很容易导致难以找到的复制粘贴错误:
class SomeOther { // another class, with the same interface as Impossible
public:
static void Fun()
{
typedef Impossible EnclosingClass;
// whoops, copy-pasted, forgot to change "Impossible" to "SomeOther"
// now do something with EnclosingClass ...
}
}
Run Code Online (Sandbox Code Playgroud)
有没有一种好方法可以防止这种事情发生?我可以想象触摸在封闭类中声明为私有的东西,但这会强迫我编写额外的代码(因为我当前的设计不包含任何固有的私有成员,都是公共的).
给定一个带有初始化方法的静态类:
public static class Foo
{
// Class members...
internal static init()
{
// Do some initialization...
}
}
Run Code Online (Sandbox Code Playgroud)
如何确保之前运行初始化程序Main()?
我能想到的最好的是将其添加到Foo:
private class Initializer
{
private static bool isDone = false;
public Initializer()
{
if (!isDone)
{
init();
isDone = true;
}
}
}
private static readonly Initializer initializer = new Initializer();
Run Code Online (Sandbox Code Playgroud)
这会有用还是有一些不可预见的警告?还有更好的方法吗?
我想在我的班级中有一个函数,我将只在这个类的方法中使用它.我不会在这些方法的实现之外调用它.在C++中,我将使用在类的私有部分中声明的方法.在python中实现这样一个函数的最佳方法是什么?我想在这种情况下使用静态装饰器.我可以使用没有任何装饰器和self单词的函数
首先,我理解一般来说装饰工作是如何工作的.而且我知道@staticmethod在签名中剥离实例参数
class C(object):
@staticmethod
def foo():
print 'foo'
C.foo //<function foo at 0x10efd4050>
C().foo //<function foo at 0x10efd4050>
Run Code Online (Sandbox Code Playgroud)
有效.
但是,我不明白如何staticmethod实现这一点的源代码.
在我看来,该包装方法时foo的staticmethod,实例staticmethod被实例化,那么一些神奇的发生,使C.foo()合法的.
那么......那些魔法会发生什么?做了staticmethod什么?
我知道关于SO的巨大话题,staticmethods但没有一个能解决我的疑虑.但也许我没有点击魔术关键词.如果是的话,请告诉我.
对于寻找staticmethod源代码的人,请参阅https://hg.python.org/cpython/file/c6880edaf6f3/Objects/funcobject.c
python static-methods decorator python-internals python-decorators
我想做这样的事情:
interface Serializable<FromType, ToType> {
fun serialize(): ToType
companion object {
abstract fun deserialize(serialized: ToType): FromType
}
}
Run Code Online (Sandbox Code Playgroud)
甚至这对我有用:
interface Serializable<ToType> {
fun serialize(): ToType
constructor(serialized: ToType)
}
Run Code Online (Sandbox Code Playgroud)
但都没有编译.是否有这样的语法,或者我将被迫使用make this作为工厂的接口? 或者还有另一个答案吗?那很整洁!
为什么Java 8支持静态方法?下面代码中main方法中两行的区别是什么?
package sample;
public class A {
public static void doSomething()
{
System.out.println("Make A do something!");
}
}
public interface I {
public static void doSomething()
{
System.out.println("Make I do something!");
}
}
public class B {
public static void main(String[] args) {
A.doSomething(); //difference between this
I.doSomething(); //and this
}
}
Run Code Online (Sandbox Code Playgroud)
正如我们在上面看到的那样,我甚至没有在B中实现.当我们可以在另一个类中编写相同的静态方法并调用它时,在接口中使用静态方法的目的是什么?是否为模块化以外的任何其他目的而引入.通过模块化,我的意思是:
public interface Singable {
public void sing();
public static String getDefaultScale()
{
return "A minor";
}
}
Run Code Online (Sandbox Code Playgroud)
只是把类似的方法放在一起.