小编sda*_*das的帖子

为什么我的组件不包装在Swing的FlowLayout中?

为什么我的组件不使用FlowLayout包装在这个JPanel中?它们只是在屏幕上运行,只是部分可见.

JPanel panel = new JPanel(new FlowLayout());
panel.add(new JLabel("TEST"));
// ... repeat adding JLabels until they go off screen when they SHOULD wrap
// to the next line...
Run Code Online (Sandbox Code Playgroud)

这是我的全部代码(除了添加和打包框架).我误解了FlowLayout吗?我是否必须在标签或面板上设置某种尺寸?

java swing flowlayout

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

为什么setter不改变C++中的值?

以下代码打印:

2
1
Run Code Online (Sandbox Code Playgroud)

代替

2
2
Run Code Online (Sandbox Code Playgroud)

为什么我的二传手没有调整价值?

主要

Vector location = camera.get_location();
camera.get_location().set_y(location.get_y() + 1);
std::cout << location.get_y() + 1 << std::endl;
std::cout << camera.get_location().get_y() << std::endl;
Run Code Online (Sandbox Code Playgroud)

camera.h

#ifndef CAMERA_H
#define CAMERA_H

#include "vector.h"

class Camera {
 private:
  Vector location;
 public:
  Vector get_location();
  void set_location(Vector);
};

#endif
Run Code Online (Sandbox Code Playgroud)

camera.cpp

#include "camera.h"

Vector Camera::get_location() { return location; }
void Camera::set_location(Vector l) { location = l; }
Run Code Online (Sandbox Code Playgroud)

c++

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

在Java方法中使用大括号会降低性能吗?

似乎双括号初始化增加了开销.

在方法中使用大括号是否也会降低性能?

例如.

public class DoIReducePerformanceToo {

    public void aMethod() {

        {
           // Is it a bad idea to use these?
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

我看了一下Java的语法,似乎这被归类为一个块:

Block: 
    { BlockStatements }

BlockStatements: 
    { BlockStatement }

BlockStatement:
    LocalVariableDeclarationStatement
    ClassOrInterfaceDeclaration
    [Identifier :] Statement
Run Code Online (Sandbox Code Playgroud)

但我不确定语法双括号初始化的位置在哪里.

我的问题:在方法中使用块语句会降低Java中的性能吗?这些块与双支撑初始化具有相同的性质吗?

编辑:

内课即时:

ClassCreatorRest: Arguments [ClassBody]

ClassBody: 
    { { ClassBodyDeclaration } }
Run Code Online (Sandbox Code Playgroud)

java performance block

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

一个类的公共成员如何在java中造成破坏?

一个类的公共成员如何在java中造成破坏?有人可以用例子解释一下吗?我试图创造这种情况,但不能成功.我只是发现它们等同于'protected'访问修饰符.

java class public

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

我可以在Java类中减少这种重复出现的模式吗?

我有以下界面:

public interface Gravy {

     public List<Giblet> getGiblets();
     public Giblet getGiblet(String id);
     public int getNumGiblets();
     public void addGiblet();
     public void removeGiblet(Giblet giblet);

     public List<Carrot> getCarrots();
     public Carrot getCarrot(String id);
     public int getNumCarrots();
     public void addCarrot();
     public void removeCarrot(Carrot carrot);

     public List<Gravy> getGravies();
     public Gravy getGravy(String id);
     public int getNumGravies();
     public void addGravy();
     public void removeGravy(Gravy gravy);

}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我的作品中有一个重复出现的模式Gravy.甲Gravy对象可以包含内脏,胡萝卜,和其他(较小)肉汁.所有这些都可以添加,删除或查询.

有两点需要注意:

  1. Carrots和Giblets彼此有点共同之处,但两者都与Gravys 有很大不同.

  2. 我可能需要稍后添加更多项目(因此需要重构)...

是否可以减少上面的代码,以便"动词"只写一次?

java refactoring design-patterns interface

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

如何根据参数的类型使用函数?

我正在使用Haskell和OpenGL.OpenGL有一种使用一组统一函数加载变量的方法:

glUniform1i location intValue
glUniform1f location floatValue
glUniform2i location intValue1 intValue2
... etc.
Run Code Online (Sandbox Code Playgroud)

我试图将其翻译成更惯用的Haskell.我想写一个函数:

uniform :: String -> a -> IO ()
uniform location value = ...
Run Code Online (Sandbox Code Playgroud)

我的问题是我调用的函数依赖于的值a.有没有办法做到这一点,而无需编写ň不同的功能?

opengl haskell

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

如何判断一个方法是否修改了类?

我正在编写 C++ 代码,并且我有一个包含许多成员方法的类。例如,print()act();前者不会修改类的内部结构(即变量i),而后者会。

class Example {
    int i;

    void act() { i++; };
    void print() { };
Run Code Online (Sandbox Code Playgroud)

在这种情况下这是显而易见的,但是对于较长的方法,或者定义和声明已经分开的方法,哪些方法可以改变对象并不明显。有没有一种方法可以明确表示方法是不可修改的?

(作为另一种语言的例子,在 CI 中可能会传递一个指向对象的指针。如果一个函数是非修改的,我会传递一个常量指针。)

c++

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

如何找到具有特定值的所有元组?

我正在使用以下形式的元组填充的列表:

tups = [(1, 2, 4.56), (2, 1, 1.23), (1, 3, 2.776), ...]
Run Code Online (Sandbox Code Playgroud)

我想执行两个操作.

第一个是查找以数字n开头的所有元组,例如:

def starting_with(n, tups):
    '''Find all tuples with tups that are of the form (n, _, _).'''
    # ...
Run Code Online (Sandbox Code Playgroud)

第二个是相反的,找到第二个值为n的所有元组:

def middle_with(n, tups):
    '''Find all tuples with tups that are of the form (_, n, _).'''
    # ...
Run Code Online (Sandbox Code Playgroud)

从某种意义上说,在元组列表上进行模式匹配.我如何在Python中执行此操作?

python tuples list pattern-matching

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