最近遇到了不可变对象的概念,我想知道控制状态访问的最佳实践.即使我的大脑的面向对象部分让我想要在公众成员的视线中畏缩,但我认为没有类似这样的技术问题:
public class Foo {
public final int x;
public final int y;
public Foo( int x, int y) {
this.x = x;
this.y = y;
}
}
Run Code Online (Sandbox Code Playgroud)
我觉得更适合声明字段private并为每个字段提供getter方法,但是当状态明确只读时,这似乎过于复杂.
提供对不可变对象状态的访问的最佳实践是什么?
我们目前正在将项目从一个项目迁移QtWebkit到QWebEngine.但是,处理下载会引起一些麻烦.以前我们使用QWebPage::unsupportedContent信号处理这个,如下所示:
QWebPage* webPage = new QWebPage(this);
QObject::connect(webPage, &QWebPage::unsupportedContent, [] (QNetworkReply* reply) {
// do stuff with the reply
reply->readAll();
});
Run Code Online (Sandbox Code Playgroud)
使用时QtWebEngine,我唯一能想到的是使用QWebEngineView::urlChanged信号向服务器发出请求,我甚至不确定这是否有效.
QNetworkAccessManager* accessManager = new QNetworkAccessManager(this);
QWebEngineView* webView = new QWebEngineView(this);
QObject::connect(webView, &QWebEngineView::urlChanged, [=] (const QUrl& url) {
if (url.path().endsWith("some_endpoint_which_results_in_a_download") {
QNetworkReply* reply = accessManager->get(url);
// do the same stuff to the reply
reply->readAll();
}
})
Run Code Online (Sandbox Code Playgroud)
显然,这种方法非常有限,因为导致下载的端点必须硬编码到应用程序中.但是,我看不出更好的解决方案.谁有人想出更好的东西?
- 更新 -
Qt的5.5发布计划中的文档概述了开发人员对Web缓存和cookie的控制的其他改进,以下功能.
添加了用于管理文件下载的API
5.5 beta计划于2015年4月9日发布,最终发布于2015年5月26日.
为了防止任何进一步的头部创伤,可能值得等待这些改进.
话虽如此,如果有人的话,我仍然会对我的清洁解决方案感兴趣.
在最近读一本关于物理引擎开发的书时,我遇到了一个我以前从未考虑过的设计决策.这与CPU处理内存中的原始字节的方式有关.
考虑以下课程:
class Foo
{
public:
float x;
float y;
float z;
/* Constructors and Methods */
private:
float padding;
}
Run Code Online (Sandbox Code Playgroud)
作者声称填充,在x86架构中将对象的大小增加到四字,可以显着提高性能.这是因为4个单词在内存中比3 更干净,这是什么意思?用冗余数据填充对象以提高性能对我来说似乎很矛盾.
这也引出了另一个问题,那些大小为1或2个单词的对象呢?如果我的班级是这样的:
class Bar
{
public:
float x;
float y;
/* Constructors and Methods */
private:
/* padding ?? */
}
Run Code Online (Sandbox Code Playgroud)
我应该在这个类中添加填充,以便它在内存中更干净地放置吗?
在一个集合中,我存储了一些数据,映射到在其他地方生成的移动设备 UID。例如:
{
devices: {
'b2e4fe52d4ab57fd55fa': { model: 'aPhone', number: 1111111 },
'b2e4fe52d4ab57fd55fb': { model: 'bPhone', number: 2222222 },
'b2e4fe52d4ab57fd55fc': { model: 'cPhone', number: 3333333 }
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在不使用 UID 的情况下检索代表一个设备的对象?
对于仅在模型或数字上使用 $elemMatch 的数组来说,这很容易,但似乎它的使用仅限于数组,文档是否有模拟运算符?
考虑以下:
foo打算获取参数对象并重新排列顺序,将arg1移动到arg2的位置
function foo (args) {
args[2] = args[1];
args[1] = undefined;
}
Run Code Online (Sandbox Code Playgroud)
bar 用它的参数调用foo
function bar (a, b, c) {
foo(arguments);
console.log(arguments);
}
Run Code Online (Sandbox Code Playgroud)
我希望下面的结果是这样的 { 0: 'hello', 1: undefined, 2: 'world' }
bar('hello', 'world');
Run Code Online (Sandbox Code Playgroud)
但是,我得到:
{
0: 'hello',
1: undefined,
2: 'world',
3: undefined,
4: undefined,
5: undefined,
6: undefined,
7: undefined,
8: undefined,
9: undefined,
10: undefined,
11: undefined,
12: undefined,
13: undefined,
14: undefined,
15: undefined,
16: undefined,
17: undefined,
18: undefined,
19: undefined
}
Run Code Online (Sandbox Code Playgroud)
我完全不知道为什么会这样.有人有主意吗?
我在node.js环境中运行它
我有两个表格,包括以下字段
emp_table: emp_id, emp_name
salary_increase: emp_id, inc_date, inc_amount
Run Code Online (Sandbox Code Playgroud)
我需要写一个查询,该查询给出了员工的详细信息,员工获得加薪的次数,最大增加金额的值以及增加的日期.这是我到目前为止:
SELECT e.*, count(i.inc_amount), max(i.inc_amount)
FROM salary_increase AS i
RIGHT JOIN emp_table AS e
ON i.emp_id=e.emp_id
GROUP BY e.emp_id;
Run Code Online (Sandbox Code Playgroud)
这正确地给出了除了授予最大增加的日期之外的所有要求.我试过以下但没有成功:
SELECT e.*, count(i.inc_amount), max(inc_amount), t.inc_date
FROM salary_increase AS i
RIGHT JOIN emp_table AS e
ON i.emp_id=e.emp_id
RIGHT JOIN
(
SELECT emp_id, inc_date FROM salary_increase
WHERE inc_amount=max(inc_amount) GROUP BY emp_id
) AS t
ON e.emp_id=t.emp_id
GROUP BY e.emp_id;
Run Code Online (Sandbox Code Playgroud)
这会给出错误"组功能的无效使用".有谁知道我做错了什么?
在使用mocha和chai进行测试时,我经常需要测试数组中的所有元素是否满足条件.
目前我正在使用以下内容:
var predicate = function (el) {
return el instanceof Number;
};
it('Should be an array of numbers', function () {
var success,
a = [1, 2, 3];
success = a.every(predicate);
expect(success).to.equal(true);
});
Run Code Online (Sandbox Code Playgroud)
通过文档查看,我无法立即看到任何提供此类行为的内容.我错过了什么或者我是否必须编写插件来扩展chai?
我只是一个初学的Java程序员,我只是想知道如果用户输入为false,是否有办法结束程序.这是一个例子:
if (age > 13) {
System.out.println("You are eligible for this site, you may proceed. ");
} else {
// End program here if statement = false
}
Run Code Online (Sandbox Code Playgroud)
(如果你不介意,请回答代码.)
c++ ×2
java ×2
javascript ×2
arguments ×1
chai ×1
group-by ×1
if-statement ×1
immutability ×1
join ×1
mocha.js ×1
mongodb ×1
mysql ×1
performance ×1
qt ×1
qt5.4 ×1
qtwebengine ×1
sql ×1