我一直试图挑选一个包含对静态类方法的引用的对象.Pickle失败(例如打开module.MyClass.foo)说明它不能被腌制,因为module.foo不存在.
我提出了以下解决方案,使用包装器对象在调用时定位函数,保存容器类和函数名称:
class PicklableStaticMethod(object):
"""Picklable version of a static method.
Typical usage:
class MyClass:
@staticmethod
def doit():
print "done"
# This cannot be pickled:
non_picklable = MyClass.doit
# This can be pickled:
picklable = PicklableStaticMethod(MyClass.doit, MyClass)
"""
def __init__(self, func, parent_class):
self.func_name = func.func_name
self.parent_class = parent_class
def __call__(self, *args, **kwargs):
func = getattr(self.parent_class, self.func_name)
return func(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
我想知道,是否有更好的 - 更标准的方法 - 来腌制这样的物体?我不想对全局pickle进程进行更改(copy_reg例如使用),但以下模式会很棒:class MyClass(object):@ patch_staticmethod def foo():print"done".
我对此的尝试是不成功的,特别是因为我无法从foo函数中提取所有者类.我甚至愿意接受明确的规范(例如@picklable_staticmethod(MyClass)),但我不知道有什么方法可以 …
我有以下课程:
public class DocketType : Enumeration<DocketType, int, string>
{
public static DocketType ChangeOver = new DocketType(1, "Changeover");
public static DocketType Withdrawal = new DocketType(2, "Withdrawal");
public static DocketType Installation = new DocketType(3, "Installation");
private DocketType(int docketTypeId, string description)
: base(docketTypeId, description)
{
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下基类:
public abstract class Enumeration<TEnum, X, Y> : IComparable
where TEnum : Enumeration<TEnum, X, Y>
where X : IComparable
where Y : IComparable
{
protected Enumeration(X value, Y displayName)
{
AddToStaticCache(this);
}
public static TEnum Resolve(X …Run Code Online (Sandbox Code Playgroud) 我有以下简单的代码片段,用于检测给定的IPv4地址确实只有数值(即在剥离点后):
import edu.gcc.processing.exceptions.net.IPAddressNumericException;
//Get the IP address
String address = "239.255.255.255";
//Check to see if this is a number
try {
String IPNumbers = address.replace(".", "");
Integer.parseInt(IPNumbers);
} catch (NumberFormatException e) {
System.out.print(e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,NumberFormatException被解雇了,我得到这个错误:
For input string: "239255255255"
Run Code Online (Sandbox Code Playgroud)
有人可以帮我理解这个吗?该parseInt()方法适用于较小的数字,例如127001.
感谢您的时间.
我有以下类实现必须使用单个全局数组的静态方法.它定义如下:
//Defined in LockTrack.h file
enum LOCK_ID{
LOCKID_0,
LOCKID_1,
LOCKID_2,
LOCKID_COUNT
};
static LOCK_ID __glob_lock_ids[LOCKID_COUNT];
class CLockTrack
{
public:
static void getLockedLocks(/*parameters*/)
{
//__glob_lock_ids = points to 0x015ef558 address in memory
LOCK_ID lockID = __glob_lock_ids[0];
}
static void inline setLock(LOCK_ID lockID)
{
//__glob_lock_ids = points to 0x015ef330 address in memory
__glob_lock_ids[lockID] = LOCK_ON_FLAG;
}
};
Run Code Online (Sandbox Code Playgroud)
但是会发生的是'__glob_lock_ids'指针指向每种方法中的不同内存位置.为什么?以及如何解决这个问题?
我有这种静态方法
public static List<? extends A> myMethod(List<? extends A> a) {
// …
}
Run Code Online (Sandbox Code Playgroud)
我正在使用它
List<A> oldAList;
List<A> newAList = (List<A>) MyClass.myMethod(oldAList);
Run Code Online (Sandbox Code Playgroud)
由于未经检查的强制转换,这会发出警告List<A>.有没有办法避免演员?
我想创建一个类,它有一个静态方法,返回对静态变量的引用(在方法内声明).我想要的是调用方法来获取静态变量的引用.然后当我在类之外修改它并再次调用该方法以获得我之前设置的相同值.
这是我试过的:
#include <iostream>
using namespace std;
class A
{
public:
static int& f()
{
static int i;
return i;
}
};
int main()
{
static int i;
i = A::f();
cout << i << endl;
i = 11;
cout << i << endl;
i = A::f();
cout << i << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是这段代码的输出是:
0
11
0
Press <RETURN> to close this window...
Run Code Online (Sandbox Code Playgroud)
它为什么不返回0, 11, 11,我怎样才能返回0, 11, 11?
注意:我希望静态变量在方法内显式声明,而不是成员.
谢谢!
我需要在静态方法中编写一个cookie(我需要静态,因为我想从其他类调用此方法).我找到了解决方案HttpContex.Current,但它对我不起作用.我收到这个错误
非静态字段,方法或属性'System.Web.Mvc.Controller.HttpContext.get'需要对象引用
我也试过添加using System.Web.HttpContext.Current;,我得到这个错误
'System.Web.HttpContext.Current'是'属性',但用作'类型'
我的方法:
public static void WriteCookie(Guid token)
{
HttpCookie cookie = new HttpCookie("LoginControl");
cookie.Value = token.ToString();
cookie.Expires = DateTime.Now.AddHours(0.5);
HttpContext.Current.Reponse.Cookies.Add(cookie);
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?非常感谢Mathew.
在我的Android应用程序中,我想以静态方法访问我的文本文件,但使用此代码:
InputStream is = Resources.getSystem().openRawResource(R.raw.adv_types);
给我运行时异常:资源未找到异常
虽然我可以使用此代码以非静态方法访问该文件:
InputStream is = getResources().openRawResource(R.raw.adv_types);
虽然它在静态方法中不可用.
你知道为什么第一个代码不起作用吗?在静态方法中访问文本文件(或一般资源)的解决方案是什么?
我只是在试验内部类,并且遇到了这个有本地但是静态内部类的想法......我在静态方法中创建了一个内部类......好吧,它就像那样简单..这是我做的例子
class Outer {
static void m() {
class LocalStatic {
void s() {
System.out.println("static local inner class method");
}
}
}
}
class Demo {
public static void main(String args[]) {
Outer.m();
}
}
Run Code Online (Sandbox Code Playgroud)
这不会给出任何编译错误.
我知道如何访问静态方法m.但我想知道是否有办法 从外部类访问本地类LocalStatic ..那么根据我的理解,我们无法访问方法内的某些内容吗?因此,我不能访问任何LocalStatic或任何方法或类之外的本地类的内部属性外只是想确保..
static-methods ×10
java ×3
c# ×2
c++ ×2
class ×2
android ×1
asp.net ×1
c#-4.0 ×1
f# ×1
generics ×1
httpcontext ×1
inputstream ×1
integer ×1
local-class ×1
oop ×1
parseint ×1
pickle ×1
python ×1
static ×1