在方法中重载构造函数 InnerClass

overloaded constructor InnerClass inside a method

我正在读 Bruce Eckel 的书 "Thinking in Java"。我在 class 的内部章节中遇到了这个断言,它说:"the only justification for using a local inner class instead of an anonymous inner class is if you need a named constructor and/or an overloaded constructor"

如果我理解得很好,我现在不知道但是: 这是在方法内部重载Inner(local classes)构造函数的方式吗?

abstract class ForInner {
abstract String getName();
abstract void setName(String newName);
abstract int getNumber();
abstract void setNumber(int newNumber);

} class外层{

public ForInner getSomeInner(String name) {
    class LocalInner extends ForInner{
        private String myName;
        private int myNumber;
        public LocalInner(String myName) {
            this.myName = myName;
        }
        public String getName() {
            return myName;
        }
        public void setName(String newName) {
            myName = newName;
        }
        public int getNumber() {
            return myNumber;
        }
        public void setNumber(int newNumber) {
            myNumber = newNumber;
        }
    }
    return new LocalInner(name);
}
public ForInner getSomeInner(int number) {
    class LocalInner extends ForInner{
        private String myName;
        private int myNumber;
        public LocalInner(int myNumber) {
            this.myNumber = myNumber;
        }
        public String getName() {
            return myName;
        }
        public void setName(String newName) {
            myName = newName;
        }
        public int getNumber() {
            return myNumber;
        }
        public void setNumber(int newNumber) {
            myNumber = newNumber;
        }
    }
    return new LocalInner(number);
}

}

我不确定这个断言是否指的是这个。但可能会猜测情况并非如此,因为 以这种方式使用会有多么不同

abstract class ForInner {
abstract String getName();
abstract void setName(String newName);
abstract int getNumber();
abstract void setNumber(int newNumber);

}

lass Outer{
public ForInner inner (String name) {
    return new ForInner() {
        private String myName;
        private int myNumber;
        {
            myName = name;
        }
        public String getName() {
            return myName;
        }
        public void setName(String newName) {
            myName = newName;
        }
        public int getNumber() {
            return myNumber;
        }
        public void setNumber(int newNumber) {
            myNumber = newNumber;
        }
    };
}
public ForInner inner (int number) {
    return new ForInner() {
        private String myName;
        private int myNumber;
        {
            myNumber = number;
        }
        public String getName() {
            return myName;
        }
        public void setName(String newName) {
            myName = newName;
        }
        public int getNumber() {
            return myNumber;
        }
        public void setNumber(int newNumber) {
            myNumber = newNumber;
        }
    };
}

} 提前致谢?

public class OuterClass {
    Runnable printA = new Runnable() {
        @Override
        public void run() {
            System.out.println("Print A");
        }
    };
    Runnable printB = new Runnable() {
        @Override
        public void run() {
            System.out.println("MESSAGE:" + " " + "Print B");
        }
    };

    class PrintMessage implements Runnable {
        private String msg;
        public PrintMessage(String msg) {
            this.msg = msg;
        }

        // overloaded constructor
        public PrintMessage(String prefix, String msg) {
            this.msg = prefix + " " + msg;
        }


        @Override
        public void run() {
            System.out.println(msg);
        }
    }
    Runnable printC = new PrintMessage("Print C");
    Runnable printD = new PrintMessage("Print D");
    Runnable printE = new PrintMessage("MESSAGE:", "Print E");

    public static void main(String[] args) {
        OuterClass sample = new OuterClass();
        sample.printA.run();
        sample.printB.run();
        sample.printC.run();
        sample.printD.run();
        sample.printE.run();
    }
}

有两个 Runnable 实例实现为匿名 classes。在创建 printA 时,您不能使用它来创建 printB。您应该从一开始就创建匿名 class(即覆盖所有抽象方法)。

如果一个内部 class 基于 Runnable 创建,您可以在 new PrintMessage() 形式中使用它来创建新实例。除此之外,还可以使用非默认构造函数。

啊好的所以什么时候有这个代码

class OuterClass {
    public Runnable printA() {
        return new Runnable() {

            @Override
            public void run() {
                System.out.println("Print A");
            }
        };
    }
    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        Runnable printA = outer.printA();
        Runnable printB = outer.printA();   
    }
}

在这种情况下,我不会创建单个匿名内部 class 的多个实例。相反,我正在创建多个使用相同源代码的匿名 classes。那是Rigth吗?! 谢谢