小编Mig*_*ork的帖子

切换案例顺序会影响速度吗?

我试过谷歌这个,但没有运气.

我有一个非常大的转变,有些情况显然比其他情况更常见.

因此,我想知道订单是否真的保持不变,"上"案例在"较低"之前进行测试,因此评估得更快.

我想保留我的订单,但如果它伤害速度,那么重新排序分支将是一个好主意.

例如:

switch (mark) {
        case Ion.NULL:
            return null;

        case Ion.BOOLEAN:
            return readBoolean();

        case Ion.BYTE:
            return readByte();

        case Ion.CHAR:
            return readChar();

        case Ion.SHORT:
            return readShort();

        case Ion.INT:
            return readInt();

        case Ion.LONG:
            return readLong();

        case Ion.FLOAT:
            return readFloat();

        case Ion.DOUBLE:
            return readDouble();

        case Ion.STRING:
            return readString();

        case Ion.BOOLEAN_ARRAY:
            return readBooleans();

        case Ion.BYTE_ARRAY:
            return readBytes();

        case Ion.CHAR_ARRAY:
            return readChars();

        case Ion.SHORT_ARRAY:
            return readShorts();

        case Ion.INT_ARRAY:
            return readInts();

        case Ion.LONG_ARRAY:
            return readLongs();

        case Ion.FLOAT_ARRAY:
            return readFloats(); …
Run Code Online (Sandbox Code Playgroud)

java switch-statement

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

为什么私人方法也不能是最终的?

添加privatefinal使用相同的方法是多余的吗?

class SomeClass {

    //--snip--

    private final void doStuff()
    {
        // private work here
    }
}
Run Code Online (Sandbox Code Playgroud)

如果是的话private,任何人都无法覆盖它,对吗?

final如果没有效果,为什么可以添加关键字?(或者我错过了什么?)

java oop methods final

43
推荐指数
2
解决办法
8741
查看次数

Bash打印stderr,而不是stdout

我想lint一个文件,并打印stderr(错误消息),但不打印stdout(说该文件没问题).

php -l "foo/bar.php"
Run Code Online (Sandbox Code Playgroud)
  • 如果没有错误,它会向stdout输出"No errors"消息.
  • 如果有错误,它会向stderr输出详细消息.我只想要这个.

我认为这将是一些魔术>&,但我从来没有理解这些是如何工作的.

我想要的是消耗所有stdout,保持stderr.

(对不起,如果这是假的,但我没有发现任何确切的问题)

linux bash

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

C/C++中的句法糖

我一直在研究Ruby并发现它的关键字"直到"和"除非"非常有趣.所以我认为在C/C++中添加类似关键字的好方法是什么.这就是我想出的:

#define until(x)    while(!(x))
#define unless(x)   if(!(x))
Run Code Online (Sandbox Code Playgroud)

我正在寻找一些建议.谁能提出更好的选择?

这是我编写的一个程序示例,用于说明我打算做什么:

#include <stdio.h>
#include <stdlib.h>

#define until(x)    while(!(x))
#define unless(x)   if(!(x))

unsigned int factorial(unsigned int n) {
    unsigned int fact=1, i;
    until ( n==0 )
        fact *= n--;
    return fact;    
}

int main(int argc, char*argv[]) {
    unless (argc==2)
        puts("Usage: fact <num>");
    else {
        int n = atoi(argv[1]);
        if (n<0)
            puts("please give +ve number");
        else
            printf("factorial(%u) = %u\n",n,factorial(n));
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果你能指出一些可以在C或C++中使用的类似技巧的参考文献,那将是很棒的.

c c++ syntax metaprogramming syntactic-sugar

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

java中的显式类型转换示例

我在http://www.javabeginner.com/learn-java/java-object-typecasting上遇到过这个例子,在谈到显式类型转换的部分中,有一个例子让我感到困惑.

这个例子:

class Vehicle {

    String name;
    Vehicle() {
        name = "Vehicle";
    }
}

class HeavyVehicle extends Vehicle {

    HeavyVehicle() {
        name = "HeavyVehicle";
    }
}

class Truck extends HeavyVehicle {

    Truck() {
        name = "Truck";
    }
}

class LightVehicle extends Vehicle {

    LightVehicle() {
        name = "LightVehicle";
    }
}

public class InstanceOfExample {

    static boolean result;
    static HeavyVehicle hV = new HeavyVehicle();
    static Truck T = new Truck();
    static HeavyVehicle hv2 = null;
    public static void main(String[] …
Run Code Online (Sandbox Code Playgroud)

java casting downcast

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

python中不同参数类型的方法重载

我正在python中编写一个预处理器,其中一部分与AST一起工作.

有一种render()方法可以将各种语句转换为源代码.

现在,我有这样的(缩短):

def render(self, s):
    """ Render a statement by type. """

    # code block (used in structures)
    if isinstance(s, S_Block):
        # delegate to private method that does the work
        return self._render_block(s)

    # empty statement
    if isinstance(s, S_Empty):
        return self._render_empty(s)

    # a function declaration
    if isinstance(s, S_Function):
        return self._render_function(s)

    # ...
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它很乏味,容易出错并且代码很长(我有更多种类的语句).

理想的解决方案是(在Java语法中):

String render(S_Block s)
{
    // render block
}

String render(S_Empty s)
{
    // render empty statement
}

String render(S_Function s)
{
    // render function …
Run Code Online (Sandbox Code Playgroud)

python oop design-patterns

18
推荐指数
3
解决办法
7141
查看次数

Getopt不包括在内?隐式声明函数'getopt'

我想使用getopt,但它不会起作用.

它给了我

gcc -g -Wall -std=c99 -ftrapv -O2 -Werror -Wshadow -Wundef -save-temps -Werror-implicit-function-declaration   -c -o src/main.o src/main.c
src/main.c: In function ‘main’:
src/main.c:13:2: error: implicit declaration of function ‘getopt’ [-Werror=implicit-function-declaration]
src/main.c:23:14: error: ‘optarg’ undeclared (first use in this function)
src/main.c:23:14: note: each undeclared identifier is reported only once for each function it appears in
src/main.c:26:9: error: ‘optopt’ undeclared (first use in this function)
src/main.c:28:5: error: implicit declaration of function ‘isprint’ [-Werror=implicit-function-declaration]
src/main.c:36:5: error: implicit declaration of function ‘abort’ [-Werror=implicit-function-declaration]
src/main.c:36:5: error: …
Run Code Online (Sandbox Code Playgroud)

c linux getopt

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

算术的奇怪问题(?)

背景:我正在开发一个用于创建"低分辨率"显示的库.

现在我想用一个简单的演示来测试它 - 它应该在光标周围绘制一个径向渐变.

我认为我的数学正确并且它正在工作,除非你将鼠标移动到左下角.

我尝试打印数字,改变顺序和所有,但找不到原因.

这里也是一个小提琴:https://jsfiddle.net/to5qfk7o/

var size = 16; // number of pixels

var lo = new Lores('#board', size, size);

// DRAWING FUNCTION
setInterval(function () {
    // Mouse coords
    var m_x = lo.mouse.x;
    var m_y = lo.mouse.y;
    
    // print where is mouse - for debug
    console.log(m_x + "," + m_y);
    
    // for all pixels on screen
    for (var x = 0; x < size; x++) {
        for (var y = 0; y < size; y++) …
Run Code Online (Sandbox Code Playgroud)

html javascript css

12
推荐指数
2
解决办法
804
查看次数

使用Runnable作为回调/子程序的不良做法?

Runnable用作回调被认为是不好的做法吗?

考虑到这Runnable是用于线程(请参阅它的JavaDoc),我想知道这是否可以 - 或者我是否应该为此目的创建自己的接口.

我所说的是这样的:

public class KeyBinding {
    public KeyBinding(KeyStroke stroke, Runnable handler) {
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

java callback runnable

11
推荐指数
2
解决办法
4195
查看次数

为什么TypeScript在匿名函数中包装类?

比方说,我们有一个Dog类:

class Dog {
    static food;
    private static static_var = 123;

    constructor(private name) {}

    speak() {
        console.log(this.name + ', I eat ' + Dog.food + ', ' + Dog.static_var);
    }
}
Run Code Online (Sandbox Code Playgroud)

编译成JS:

var Dog = (function () {
    function Dog(name) {
        this.name = name;
    }

    Dog.prototype.speak = function () {
        console.log(this.name + ', I eat ' + Dog.food + ', ' + Dog.static_var);
    };

    Dog.static_var = 123;
    return Dog;
})();
Run Code Online (Sandbox Code Playgroud)

这同样有效并且不那么复杂:

function Dog(name) {
    this.name = name;
} …
Run Code Online (Sandbox Code Playgroud)

javascript typescript

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