我无法理解抽象类背后的概念.我正在阅读的定义是它们至少有一个声明但缺乏实现的方法,因此无法实例化类.java日历类是抽象的,无法使用New运算符进行实例化,但是有一个名为getInstance()的方法返回一个日历对象.这是如何运作的?
Calendar cal = new Calendar(); //doesn't work
Calendar cal = Calendar.getInstance(); //works
Run Code Online (Sandbox Code Playgroud) 在做类似的事情时:
int value;
if (dict.TryGetValue(key, out value))
{
if (condition)
{
//value = 0; this copies by value so it doesn't change the existing value
dict[key] = 0;
}
}
else
{
dict[key] = 0;
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以避免索引查找来替换现有值吗?我已经使用TryGetValue验证密钥存在,因此再次通过索引检索值似乎是浪费.
另外,在我的代码的else {}部分中,通常认为在添加新值或替换旧值时使用索引器是一种好习惯,并添加以明确您是在添加而不是替换?或者我应该每次只使用索引器?我学习使用字典的方式,我总是做一个TryGetValue查找,而在else部分我处理没有键存在的情况.
我不太确定如何在一个句子中说出这个问题,所以我很难找到以前的帖子.这经常出现在我身上,我想就如何处理它达成共识.
假设你有两个课程,ExampleClass并且ExampleClassManager.ExampleClass有一个Update(Data data)从中调用的方法ExampleClassManager.但是,ExampleClass可以处于两种状态之一,并且在Enabled状态中它想要处理data传递给它Update的状态,并且在禁用状态下它根本不执行任何操作data.
我是否应该检查状态,如果禁用ExampleClassManager则不通过,或者我是否应该通过而忽略它?datadataExampleClass
这是一个代码示例,以防我没有明确表达清楚.
public class ExampleClass {
public bool Enabled {
get;
set;
}
public void Update(Data data) {
if(Enabled) {
//do stuff with data
}
}
}
public class ExampleClassManager {
private List<ExampleClass> exampleClassList=new List<ExampleClass>();
public void UpdateList() {
foreach(ExampleClass exampleClass in exampleClassList) {
exampleClass.Update(data);
}
}
}
Run Code Online (Sandbox Code Playgroud)
要么 …
这是输出:
library(tseries) # for adf.test function
adf.test(data)
Augmented Dickey-Fuller Test
data: data
Dickey-Fuller = 11.1451, Lag order = 16, p-value = 0.99
alternative hypothesis: stationary
Warning message:
In adf.test(spread.princomp) : p-value greater than printed p-value
adf.test(coredata(data))
Augmented Dickey-Fuller Test
data: coredata(data)
Dickey-Fuller = -4.031, Lag order = 16, p-value = 0.01
alternative hypothesis: stationary
Warning message:
In adf.test(coredata(spread.princomp)) :
p-value smaller than printed p-value
Run Code Online (Sandbox Code Playgroud)
基础数据是数字向量.人们似乎成功地使用xts应用adf.test,所以我不确定我做错了什么.请告诉我我能提供的其他信息.
t1 <- strptime("2015-02-17 12:20:00", format = "%Y-%m-%d %H:%M:%S", tz = "America/Chicago")
t2 <- strptime("2015-03-13 15:00:00", format = "%Y-%m-%d %H:%M:%S", tz = "America/Chicago")
as.numeric(abs(difftime(t1, t2, units = "mins", tz = "America/Chicago")))
Run Code Online (Sandbox Code Playgroud)
以上返回34,660,而Excel和http://www.timeanddate.com/date/duration.html均返回34,720.由于2015年3月8日的夏令时,R将t1识别为CST,t2识别为CDT.我想确认R是正确的,而其他两个来源则没有.
我一直在使用TryGetValue在我的词典中添加/替换数据.为了区分添加new和替换旧,我同时使用[]和.Add().这导致像这样的代码,如果我实际上没有对检索到的值做任何事情:
private Dictionary<Foo, Bar> dictionary = new Dictionary<Foo, Bar>();
public void Update(Foo foo)
{
Bar bar;
if (dictionary.TryGetValue(foo, out bar)
{
dictionary [foo] = bar;
}
else
{
dictionary .Add(foo, bar);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我实际上没有对检索到的值做任何事情,是否有理由不用这个代替上面的代码?:
public void Update(Foo foo)
{
dictionary[foo] = bar;
}
Run Code Online (Sandbox Code Playgroud)
先感谢您.