小编Dra*_*eel的帖子

为什么“instanceof”不起作用?

我正在使用 Java instanceof,但它似乎不起作用。

我有三个扩展 Hero 类的 Java 类。
Hero.java类:

public abstract class Hero {

    protected int health;

    public Hero() { 
    }
}
Run Code Online (Sandbox Code Playgroud)

其他三类

public class Archer extends Hero {
    public Archer() {
    }
}

public class Mage extends Hero {
    public Mage() {
    }
}

public class Warrior extends Hero {
    public Warrior() {
    }
}
Run Code Online (Sandbox Code Playgroud)

我有这个主类WelcomeScreen.java

public class WelcomeScreen {

    private Archer archer;
    private Mage mage;
    private Warrior warrior;
    private Hero …
Run Code Online (Sandbox Code Playgroud)

java oop polymorphism instanceof aggregation

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

用于比较数组的所有字符串值的循环

假设你有一个包含3个字符串的字符串数组arr.要比较其值,您只需执行以下操作:

if (arr[0].equals(arr[1]) && arr[0].equals(arr[2] && arr[1].equals(arr[2]) {
    return true;
}
Run Code Online (Sandbox Code Playgroud)

但是如果那个数组有数百个字符串呢?比较所有值的最佳方法是什么?

我想过使用for循环,但Java不允许在条件内循环.有任何想法吗?

java arrays loops

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

扫描程序等待用户输入时GUI冻结

我意识到你需要使用Event Dispatch Thread来避免冻结和死锁,但在这里,我遇到了冻结问题.由于封装的EDT,我怀疑我遇到了这个问题.

这是具有主静态方法的类:

@SuppressWarnings("serial")
public class WelcomeScreen extends JFrame implements ActionListener {

/*
 * initialize variables
 */
private JButton btnSubmit;
private JTextField nameInput;
private JLabel enterName;

public WelcomeScreen() {

    /*
     * build the welcome frame with all components
     */
    setAlwaysOnTop(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setResizable(false);
    setBounds(0, 0, 514, 256);
    getContentPane().setLayout(null);

    // welcome label
    JLabel label_welcome = new JLabel("<html>Welcome to <br/>Game</html>");
    label_welcome.setHorizontalAlignment(SwingConstants.CENTER);
    label_welcome.setBounds(159, 11, 190, 32);
    getContentPane().add(label_welcome);

    // create the warrior button
    btnSubmit = new JButton("Start");
    btnSubmit.setBounds(209, 129, 89, 23);
    btnSubmit.addActionListener(this);
    btnSubmit.setFocusable(false); …
Run Code Online (Sandbox Code Playgroud)

java user-interface swing multithreading

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

使用 pywin32 返回所选资源管理器窗口中所有文件的列表

我目前正在使用Win32 api 使用 Python 3。对于窗口检查,我​​使用 Microsoft Inspect Tool。目前,我有以下代码枚举所有窗口:

def getSelectedFile():

    def callback(handle, hwnds):
        print(str(handle) + " - class name: " + win32gui.GetClassName(handle) + "-- name: " + win32gui.GetWindowText(handle))
        return True

    hwnd = win32gui.GetForegroundWindow()
    if hwnd:
        if win32gui.GetClassName(hwnd) == 'CabinetWClass':  # this is the main explorer window
            win32gui.EnumChildWindows(hwnd, callback, None)
Run Code Online (Sandbox Code Playgroud)

这将输出以下内容:

19269320 - class name: BrowserFrameGripperClass-- name: 
526990 - class name: WorkerW-- name: 
395922 - class name: ReBarWindow32-- name: 
13371224 - class name: TravelBand-- name: 
2559382 - class name: …
Run Code Online (Sandbox Code Playgroud)

winapi pywin32 winforms python-3.x

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

堆栈是否在需要时分配更多空间或是否溢出?

对于x86程序集,假设我们有一个像这样的堆栈 在此输入图像描述

堆栈有2个单词分配给它拥有的2个局部变量.但是如果你强行将第三个局部变量推送到堆栈会怎么样呢.ESP是否向上移动以为变量腾出空间或变量是否覆盖ESP?

c memory assembly stack

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

格式化包含许多字符串和整数的long system.out.print

假设您有以下内容:

String cake = "whatever";
int cakeNumber = 509;
String whyNot = "another string";
double number = 1;

system.out.println("I like "+ cake +" and I eat " + cakeNumber + " blah blah     prolonging this string because " + whyNot + " and so on " + number + ".");
Run Code Online (Sandbox Code Playgroud)

所以,这没有多大意义,也没有必要,因为它只是一个例子.我的问题是这个,给定多个字符串和变量,它们之间带有"加号",看起来就像一团糟.是不是有办法格式化这个println或者通过减少加号来整理它?

java

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