问题列表 - 第16195页

C#riddle:实现接口

更新:

这个问题不是功课.而且不是很防水......我想讨论一下内部表征.当然:add1000应该加1000.

**请回答这个问题的精神......使这个防水会使这个问题更长,没有任何理由..**你可以击败纯十进制 表示在运行时更改内部表示 更新2:看到

创建一个实现此接口的类型:

interface INumber
    {
        void add1000();
        void SetValue(decimal d);
        decimal GetValue();         
    }
Run Code Online (Sandbox Code Playgroud)

所以我在这个for循环中尽可能快地从0到100亿(十亿美元,直到10e9)迭代:

private static void DoSomeAdding(INumber n)
        {
            Debug.Assert(n.GetValue()==0);

            for (long i=0; i<10000000000; i += 1000)
            {
                n.add1000();
            }

            Debug.Assert(n.GetValue() == 10000000000);

        }
Run Code Online (Sandbox Code Playgroud)

所以你可以称之为:

DoSomeAdding(new YourNumberClass());
Run Code Online (Sandbox Code Playgroud)

c#

-1
推荐指数
2
解决办法
801
查看次数

为什么Flash Player会在这种情况下抛出沙箱错误?

连接到Java(1.5)服务器上的Socket后,我收到Flex 3沙箱错误#2048.服务器代码都是我的,即不在Apache下运行.Flash Player 10.0 r32.

顺序如下......

1 Java服务器启动,在端口843上侦听策略文件请求,在端口45455上侦听我的其他请求.

2 Apache服务的Flex客户端(虽然如果我从文件系统运行它会得到相同的结果),在主机上进行套接字连接:45455.

3 Flash Player从端口843请求策略文件.这是使用新安全设置查找主文件的标准行为.无论是否指定了不同的策略文件,都会发生这种情况.

4我通过端口843从Java提供以下XML:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="*" to-ports="*" secure="false"/>
</cross-domain-policy>
Run Code Online (Sandbox Code Playgroud)

5播放器将以下内容写入调试策略日志...

OK: Root-level SWF loaded: http://localhost/bst/BasicSocketTest.swf
OK: Searching for <allow-access-from> in policy files to authorize data loading from resource at xmlsocket://192.168.2.3:45455 by requestor from http://localhost/bst/BasicSocketTest.swf
OK: Policy file accepted: xmlsocket://192.168.2.3:843
OK: Request for resource at xmlsocket://192.168.2.3:45455 by requestor from http://localhost/bst/BasicSocketTest.swf is permitted due to policy file at xmlsocket://192.168.2.3:843
Run Code Online (Sandbox Code Playgroud)

6我使用writeUTFBytes()和从端口45455向客户端发送文本消息flush() …

sockets apache-flex flash sandbox

19
推荐指数
1
解决办法
5244
查看次数

清除.NET的StringBuilder内容的最佳方法

我想问一下你认为最好的方法(持续时间更少/消耗更少的资源)来清除内容以重用StringBuilder.想象一下以下场景:

StringBuilder sb = new StringBuilder();
foreach(var whatever in whateverlist)
{
  sb.Append("{0}", whatever);
}

//Perform some stuff with sb

//Clear stringbuilder here

//Populate stringbuilder again to perform more actions
foreach(var whatever2 in whateverlist2)
{
  sb.Append("{0}", whatever2);
}
Run Code Online (Sandbox Code Playgroud)

在清除StringBuilder时,我可以想到两种可能性:

sb = new StringBuilder();
Run Code Online (Sandbox Code Playgroud)

要么

sb.Length = 0;
Run Code Online (Sandbox Code Playgroud)

清除它的最佳方法是什么?为什么?

谢谢.

编辑:我使用当前的.NET 3.5版本.

.net c# optimization .net-3.5

69
推荐指数
6
解决办法
7万
查看次数

在C++迭代器类中重载*(迭代器+ n)和*(n +迭代器)?

(注意:我正在编写这个项目仅供学习;有关冗余的评论是......呃,多余.;)

我正在尝试实现一个随机访问迭代器,但是我发现关于这个主题的文献很少,所以我将通过试验和错误结合维基多数运算符重载原型列表.到目前为止它运作良好,但我遇到了障碍.

代码如

exscape::string::iterator i = string_instance.begin();
std::cout << *i << std::endl;
Run Code Online (Sandbox Code Playgroud)

工作,并打印字符串的第一个字符.但是,*(i + 1)不起作用,*(1 + i)也不起作用.我的完整实现显然有点太多了,但这是它的要点:

namespace exscape {
    class string {
        friend class iterator;
    ...
    public:
        class iterator : public std::iterator<std::random_access_iterator_tag, char> {
            ...
            char &operator*(void) {
                return *p; // After some bounds checking
            }
            char *operator->(void) {
                return p;
            }

            char &operator[](const int offset) {
                return *(p + offset); // After some bounds checking
            }

            iterator &operator+=(const int offset) {
                p += offset;
                return *this;
            }

            const …
Run Code Online (Sandbox Code Playgroud)

c++ iterator operator-overloading operator-keyword

0
推荐指数
1
解决办法
3373
查看次数

针对调用 Web 服务的方法签名编写 JUnit 测试的最佳方法是什么?

我想要测试的方法有一个局部变量,该变量引用从 Web 服务调用返回的对象。此 Web 服务根据特定用户在网页上的输入返回特定于该用户的信息。它就像一个问题/答案,表单文本字段中给出的答案必须与用户之前提供的答案相匹配,并且现在与他们的帐户绑定。根据他们是否得到正确答案,会发生某些事情。我应该将服务模拟为类上的一个字段,我可以将其设置为返回虚拟服务响应,还是有更好的方法来测试它?谢谢!

java service junit

2
推荐指数
1
解决办法
304
查看次数

PyQt:翻译标准按钮

如何从QMessageBox轻松翻译标准按钮(是,否)?我不能在这些参数上使用self.tr,所以我想以其他简单的方式实现它.我是否必须使用整个翻译系统?

python qt translation pyqt button

4
推荐指数
2
解决办法
3974
查看次数

PHP:如何从多重分裂数组中删除元素?

我有这个代码来添加新元素到多维数组:

$this->shopcart[] = array(productID => $productID, items => $items);
Run Code Online (Sandbox Code Playgroud)

那么我如何从这个数组中删除一个元素?我尝试了这段代码,但它没有用:

public function RemoveItem($item)
{
    foreach($this->shopcart as $key)
    {
        if($key['productID'] == $item)
        {
            unset($this->shopcart[$key]);               
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

  • 警告:第50行的C:\ xampplite\htdocs\katrinelund\classes\TillRepository.php中未设置非法偏移类型

php

1
推荐指数
1
解决办法
856
查看次数

为什么我的Winforms应用程序中的SynchronizationContext.Current为null?

我刚写了这段代码:

System.Threading.SynchronizationContext.Current.Post(
    state => DoUpdateInUIThread((Abc)state), 
    abc);
Run Code Online (Sandbox Code Playgroud)

但System.Threading.SynchronizationContext.Current为null

c# multithreading winforms synchronizationcontext

20
推荐指数
2
解决办法
2万
查看次数

在sql squirrel中执行SQL Server存储过程

我在ubuntu 9.04并使用sql squirrel作为我的sql客户端.我连接到远程SQL Server.db中有一些存储过程.我不知道如何执行它们.没有明确的gui.早些时候我在Windows,我可以使用管理工作室.我可以右键单击存储过程并给出Execute.你们有什么想法吗?让我知道.这对我有帮助.:)

sql-server squirrel-sql

4
推荐指数
1
解决办法
1万
查看次数

编码标准/编码C++中的最佳实践

考虑下面的两个代码段.哪一个更好,为什么?如果您有任何其他想法,请提及.我在哪里可以找到像这样的编码实践的答案?如果您知道任何书籍/文章,请分享.

//代码1

bool MyApplication::ReportGenerator::GenerateReport(){
    bool retval = false;
    do{
        if (!isAdmin()){
            break;
        }
        if (!isConditionOne()){
            break;
        }
        if (!isConditionTwo()){
            break;
        }
        if (!isConditionThree()){
            break;
        }

        retval = generateReport();
    } while(0);
    return retval;
}
Run Code Online (Sandbox Code Playgroud)

//代码2

bool MyApplication::ReportGenerator::GenerateReport(){
    if (!isAdmin()  || !isConditionOne() || !isConditionTwo() || !isConditionThree()){
        return false;
    }
    return generateReport();
}
Run Code Online (Sandbox Code Playgroud)

罗伯特C.马丁的清洁代码是一本很好的书来处理这个问题.但是,我想这本书倾向于Java.

更新:

  1. 我故意使用do {} while(0); 因为我不想使用goto循环.

  2. 我想摆脱这么多if和break语句,所以我提出了Code 2.

我从回复中看到的是Code1和Code2的一组混合响应.与Code 1(我认为更好)相比,有些人更喜欢goto.

c++ coding-style

9
推荐指数
6
解决办法
1万
查看次数