给定一个具有一些受保护成员的类,并通过一个公共接口对其进行修改,通常什么时候可以直接访问受保护成员?我想到了一些具体的例子:
我不想公开这些属性,因为我不想公开地触摸它们。我的语法IDE语法高亮显示,我在访问受保护的成员时错了-谁在这里?
编辑 -在下面添加一个简单的示例:
class Complex:
def __init__(self, imaginary, base):
self._imaginary = imaginary
self._base = base
def __str__(self):
return "%fi + %f" % self._base, self._imaginary
def __add__(self, other):
return Complex(self._imaginary + other._imaginary, self._base + other._base)
Run Code Online (Sandbox Code Playgroud)
Pycharm 使用以下内容突出显示other._imaginary和other._base:
访问类的受保护成员_imaginary
我的代码从每个正在运行的进程中提取加载模块的所有名称,我的方法就像这个答案。
这是我的代码:
Process[] procs = Process.GetProcesses();
foreach (Process p in procs)
{
foreach (ProcessModule item in p.Modules)
{
Console.WriteLine(item.FileName);
}
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,这种方法的性能非常低:(
是否有另一种方法或不同的方法来获取所有这些模块的名称?
任何其他运行速度比这更快的解决方案都很棒
TIA
因此,我们尝试按颜色汇总所有文档,其中每种颜色的最大时间戳记为max_timestamp。然后,仅过滤max_timestamp低于的存储桶now-5m
。此处的想法是检查最近5分钟内是否没有报告报告的任何颜色。
这是我们现在得到的:
{
"size": 0,
"aggs": {
"colors_aggs": {
"terms": {
"field": "color",
"size": 10
},
"aggs": {
"max_timestamp": {
"max": {
"field": "timestamp"
}
},
"aggs": {
"filter": {
"range": {
"timestamp": {
"lt": "now-5m"
}
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
似乎忽略了第三种聚合。时间戳大于now-5m
显示的存储桶。
有什么帮助吗?
我正在尝试从中检索数据MSNdis_CurrentPacketFilter
,我的代码如下所示:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI",
"SELECT NdisCurrentPacketFilter FROM MSNdis_CurrentPacketFilter");
foreach (ManagementObject queryObj in searcher.Get())
{
uint obj = (uint)queryObj["NdisCurrentPacketFilter"];
Int32 i32 = (Int32)obj;
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我正在从NdisCurrentPacketFilter
两次投射接收的物体,这引出了一个问题:为什么?
如果我尝试将其直接投射到int
,例如:
Int32 i32 = (Int32)queryObj["NdisCurrentPacketFilter"];
Run Code Online (Sandbox Code Playgroud)
它抛出一个InvalidCastException
.这是为什么?
我的代码是这样的:
public T ReadState<T>(string file_path)
{
string file_content = ...read file..from file_path...
T state = ...xml deserialize...
state.SetSomething(); // <--the problem is here <--
return state;
}
Run Code Online (Sandbox Code Playgroud)
我想在各种对象类型上使用这个泛型方法.当然,他们都实施了这个SetSomething()
方法
目前,编译器抱怨:
'T'不包含SetSomething()的定义......
TIA!
c# ×3
aggregation ×1
attributes ×1
casting ×1
filter ×1
generics ×1
module ×1
oop ×1
performance ×1
process ×1
protected ×1
python ×1
timestamp ×1