小编Jac*_*aub的帖子

如何为包含双字段的类实现 equals/hashCode 方法

我在包含双字段的类中覆盖了 equals 和 hashCode。我的第一种方法是在 equals 方法中使用 epsilon 测试,在 hashCode 中使用 Double.hashCode( double ),但这会导致相等的对象具有不同的哈希码;这是一个简化的示例:

public class DoubleHashTest2
{
    public static void main(String[] args)
    {
        double  base1   = .9;
        double  base2   = .7;
        Test    test1   = new Test( base1 - .1 );
        Test    test2   = new Test( base2 + .1 );

        System.out.println( test1.equals( test2 ) );
        System.out.println( test1.hashCode() );
        System.out.println( test2.hashCode() );
    }

    private static class Test
    {
        private double  dnum1;

        public Test( double dnum1 )
        {
            this.dnum1 = dnum1;
        }

        public …
Run Code Online (Sandbox Code Playgroud)

java

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

Swing 对话框布局:运行方法还是构造函数?

当我启动 Swing 对话框布局时,是否在类的 run 方法中进行布局会有所不同:

    公共无效运行()
    {
        框架 = 新 JFrame();
        ...
        frame.setVisible(true);
    }

或类构造函数?

    公开课
    {
        框架 = 新 JFrame();
        ...
        frame.setVisible(true);
    }
    公共无效运行()
    {
    }

谢谢

java swing multithreading

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

必须在 EDT 上执行 JTextField.getText 和 JButton.addActionListener

我是否应该假设以下程序是错误的,因为 x 和 y 行(在 main 方法的最后)没有在 EDT 上执行?

public class Temp
{
    private static JButton     button;
    private static JTextField  text;
    
    private static void showGUI()
    {
        JFrame  frame       = new JFrame();
        JPanel  contentPane = new JPanel();
        button = new JButton( "Push Me" );
        text = new JTextField( "I am the walrus" );
        contentPane.add( button );
        contentPane.add( text );
        frame.setContentPane( contentPane );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
        throws InterruptedException, InvocationTargetException
    {
        SwingUtilities.invokeAndWait( () …
Run Code Online (Sandbox Code Playgroud)

java swing multithreading

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

为什么在调用 Constructor.newInstance() 时不能使用 lambda 表达式?

我正在尝试使用 实例化一个对象Constructor.newInstance。构造函数需要一个类型为 的参数DoubleSupplier。如果我首先创建一个DoubleSupplier对象,然后将该对象传递给该newInstance方法,则这是成功的:

DoubleSupplier  supplier    = () -> 3.0;
obj = ctor.newInstance( supplier );
Run Code Online (Sandbox Code Playgroud)

但如果我尝试直接在调用中使用 lambda newInstance

obj = ctor.newInstance( () -> 3.0 );
Run Code Online (Sandbox Code Playgroud)

它无法编译,并显示“此表达式的目标类型必须是函数式接口”。这两种方法有什么区别?

顺便说一句,我可以在使用 实例化对象时使用 lambda new

obj2 = new SubTypeA( () -> 3.0 );
Run Code Online (Sandbox Code Playgroud)

示例程序如下。

public class CtorDemo
{
    public static void main(String[] args)
    {
        SuperType   obj = getSubType( SubTypeA.class );
        System.out.println( obj.getClass().getName() );
    }
    
    private static SuperType 
    getSubType( Class<? extends SuperType> clazz )
    {
        SuperType   obj …
Run Code Online (Sandbox Code Playgroud)

java reflection lambda

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

标签 统计

java ×4

multithreading ×2

swing ×2

lambda ×1

reflection ×1