我正在考试这StringTokenizer.java
门课,脑海里浮现出几个问题.
我注意到其他类使用的公共方法调用了一些完成所有工作的私有方法.现在,我知道OOD的原则之一就是尽可能多地私有化并隐藏所有实现细节.我不确定我是否完全理解这背后的逻辑.
我理解,将字段设为私有以防止将无效值存储在其中非常重要(这只是众多原因之一).然而,当谈到私人方法时,我不确定它们为何如此重要.
例如,在StringTokenizer
类的情况下,我们不能只将所有实现代码放在公共方法中吗?它如何对使用这些方法的类产生影响,因为这些方法的API(即调用这些公共方法的规则)将保持不变?我能想到为什么私有方法有用的唯一原因是它可以帮助你编写重复的代码.例如,如果所有公共方法都做了同样的事情,那么您可以声明一个执行此任务的私有方法,并且可以由公共方法使用.
其他问题,在私有方法中编写实现而不是公共方法有什么好处?
这是一个小例子:
public class Sum{
private int sum(int a, int b){
return a+b;
}
public int getSum(int a, int b){
return sum(a,b);
}
}
Run Code Online (Sandbox Code Playgroud)
VS ...
public class Sum{
public int getSum(int a, int b){
return a+b;
}
}
Run Code Online (Sandbox Code Playgroud)
第一个样本如何更有益?
java encapsulation private-methods stringtokenizer public-method
我是第一次构建流星应用程序并遇到了一些问题.特别是,我试图让图片浏览器工作,但导航按钮中的右箭头和左箭头似乎都缺失了.当我尝试使用谷歌的开发人员工具加载应该在按钮中的图像时,它将显示为空图像.因此,我可能不会将图像文件放在适当的目录中,因此meteor无法将其加载到css文件中.我不确定是否是这种情况,或者它是别的.它可能不是任何语法错误,因为当我在流星之外运行插件时,我能够看到按钮中的箭头.
这是我的css文件中的行:
background: transparent url('themes.gif') no-repeat left top; margin-top: -45px;
Run Code Online (Sandbox Code Playgroud) 我想将一个已经重命名的文件,比如它现在称为B,恢复到早先提交的文件中,比如它被称为A,我如何在保留历史的同时做到这一点?文件B已被推送.
我可以看到文件B的整个历史记录,包括它命名为A的时间,使用:
git log --follow pathToFileB
这向我展示了这个文件所涉及的提交列表,但我不知道该怎么做.
通常情况下,我会这样做git checkout commitId:pathToFile
,但在这种情况下似乎不起作用.
我正试图让Bootsrap的模态工作,但是当我点击按钮时,我得到的是一个黑屏,没有模态对话框出现,因为我期望它.我正在使用流星.
这是我的代码:
<div class="container">
<h2>Example of creating Modals with Twitter Bootstrap</h2>
<div class="modal hide fade in" id="example" style="display: none; ">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>This is a Modal Heading</h3>
</div>
<div class="modal-body">
<h4>Text in a modal</h4>
<p>You can add some text here.</p>
</div>
<div class="modal-footer">
<a class="btn btn-success" href="#">Call to action</a>
<a class="btn" data-dismiss="modal" href="#">Close</a>
</div>
</div>
<p><a data-toggle="modal" href="#example" class="btn btn-primary btn-large">Launch
demo modal</a></p>
</div>
Run Code Online (Sandbox Code Playgroud)
我基本上从以下代码获得了代码:http://www.w3resource.com/twitter-bootstrap/modals-tutorial.php用于测试目的.
我有一个HashMap
键是类型String
的值,值是类型LinkedList
的类型String
.
所以基本上,这就是我想要做的.
while (contentItr.hasNext()) {
String word = (String) contentItr.next();
if (wordIndex.containsKey(word)) {
LinkedList temp = (LinkedList) w.get(word); //Error occurs here
temp.addLast(currentUrl);
} else {
w.put(word, new LinkedList().add(currentUrl));
}
}
Run Code Online (Sandbox Code Playgroud)
我第一次添加一个键,值对,我没有收到任何错误.但是,当我尝试检索与现有密钥关联的链接列表时,我得到以下异常:
java.lang.Boolean cannot be cast to java.util.LinkedList.
Run Code Online (Sandbox Code Playgroud)
我没有可能的解释为什么会发生此异常.
我已经开始理解一些与柯里化相关的例子,但我仍然对柯里化的概念不满意。我知道 currying 可用于进行部分评估,但我不确定在某些情况下它会如何工作。
我知道它在下面的例子中是如何工作的:
fun funkyPlus x y = x*x+y;
Run Code Online (Sandbox Code Playgroud)
所以假设我们只为 x 传递一个参数,那么它等价于以下内容:
fun funkyPlus 3 = (fn x => fn y => x*x+y)3
Run Code Online (Sandbox Code Playgroud)
最终返回
fn y => 9+y
Run Code Online (Sandbox Code Playgroud)
现在,我正在尝试将这个想法应用到内置函数中foldl
。
我知道它的代码是:
fun foldl f b [] = b
|foldl f b (h::t) = foldl f f(h,b) t.
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果我们不将所有参数传递给foldl
(即我们只传递第一个参数,即函数('a*'b->'b)
)怎么办。在我给出的第一个例子中,当只有一个参数传递给函数时,很容易看出函数是如何工作的。但是,foldl
当只有一个参数传递给它时,我无法看到如何工作。
帮助。
recursion programming-languages functional-programming sml currying
我在我的ubuntu服务器盒上运行了一个apache web服务器.最近,我试图学习JavaScript,我偶然发现了node.js. 我想创建一些Web应用程序多人游戏,我已经了解到node.js可以派上用场.我遇到了一些配置问题.我如何在同一台机器上运行apache server和node.js?如果node.js上的应用程序位于不同的端口上并且必须通过键入websitename:portNumber来访问它,我不介意.我不太关心性能优势/劣势,我只是想借此机会试用JavaScript和node.js. 有没有必须修改的文件?
这是我在服务器上运行的脚本的代码(现在仅用于试用):
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337);
Run Code Online (Sandbox Code Playgroud)
我启动了服务器(节点fileName.js).但是,当我尝试使用网络上的另一台客户端计算机访问它时,它似乎不起作用,因为页面似乎不存在.
当我在1337端口访问服务器时,我可以将Hello World输出到浏览器的程序是什么?
我有以下代码:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define LOOPS 10000
void *run(void *arg)
{
int id = strtol(arg,NULL,0);
int i;
for(i=0; i<LOOPS; i++)
{
printf("In %d.\n",id);
}
}
int main()
{
pthread_t p1,p2;
void *res;
pthread_create(&p1,NULL,run,"1");
pthread_create(&p2,NULL,run,"2");
pthread_join(p1,&res);
pthread_join(p2,&res);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,字符串"In 1"连续显示10000次,然后"In 2"连续显示10000次,反之亦然.字符串不应该是交替的而不是连续显示在这里吗?
在我的单元测试中,我将ScheduledExecutoryService
类的一个模拟实例注入到我尝试测试的类中,以便在scheduleAtFixedRate(...)
调用该方法时,它返回一个模拟的Future
. 但出于某种原因,它总是返回null
。有任何想法吗 ?
应用代码:
Future<?> t =
scheduledExecutorService.scheduleAtFixedRate(this, 10, 10, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
测试代码:
@Mock ScheduledExecutorService scheduledExecutorService;
@Mock ScheduledFuture<?> t;
Mockito.doReturn(t).when(scheduledExecutorService).scheduleWithFixedDelay(
any(Runnable.class), anyLong(), anyLong(), any(TimeUnit.class));
Run Code Online (Sandbox Code Playgroud) java ×3
meteor ×2
apache ×1
c ×1
casting ×1
concurrency ×1
currying ×1
exception ×1
git ×1
hashmap ×1
javascript ×1
linked-list ×1
mockito ×1
modal-dialog ×1
node.js ×1
pthreads ×1
recursion ×1
sml ×1
unit-testing ×1