我正在寻求帮助,看看是否有更好的方法来编写代码。我需要基于某些值实例化不同的实现,但不知何故,我觉得我可以用更通用的方式编写xyz属性
以下是我的代码的简单表示
public class A
{
string p1;
IEnumerable<B> b;
}
public interface Ix
{
...
}
public class x1 : Ix
{
...
}
public class x2 : Ix
{
...
}
public class C
{
public C()
{
_a = GetA();
}
public List<Ix> xyz
{
get
{
if (_a.b == "sometype")
{
return _a.b.ToList().Select(r => new x1(r) as Ix).ToList();
}
else
{
return _a.b.ToList().Select(r => new x2(r) as Ix).ToList();
}
}
}
}
如果我能以更好的方式写xyz属性,请告诉我吗?
提前致谢。
答案 0 :(得分:0)
你为此制作了多个清单,我建议这样做:
PnLDrawdown <- function(x) {
ts = as.vector(x[,1])
cumsum = cumsum(c(0, ts))
cmaxx = cumsum - cummax(cumsum)
cmaxx = cmaxx[-1]
cmaxx = as.matrix(cmaxx)
row.names(cmaxx) = row.names(x)
cmaxx = timeSeries(cmaxx)
cmaxx
}
findPnLDrawdown <- function(R) {
drawdowns = PnLDrawdown(R)
draw = c()
begin = c()
end = c()
length = c(0)
trough = c(0)
index = 1
if (drawdowns[1] >= 0) {
priorSign = 1
} else {
priorSign = 0
}
from = 1
sofar = as.numeric(drawdowns[1])
to = 1
dmin = 1
for (i in 1:length(drawdowns)) {
thisSign =ifelse(drawdowns[i] < 0, 0, 1)
if (thisSign == priorSign) {
if (as.numeric(drawdowns[i]) < as.numeric(sofar)) {
sofar = drawdowns[i]
dmin = i
}
to = i+ 1
}
else {
draw[index] = sofar
begin[index] = from
trough[index] = dmin
end[index] = to
from = i
sofar = drawdowns[i]
to = i + 1
dmin = i
index = index + 1
priorSign = thisSign
}
}
draw[index] = sofar
begin[index] = from
trough[index] = dmin
end[index] = to
list(pnl = draw, from = begin, trough = trough, to = end,
length = (end - begin + 1),
peaktotrough = (trough - begin + 1),
recovery = (end - trough))
}
但理想情况下,你会做这样的事情:
return _a.b.Select(r => new x1(r) as Ix).ToList();