方法内重载的构造函数InnerClass

时间:2018-09-21 11:23:40

标签: java overloading local inner-classes anonymous-inner-class

我正在阅读Bruce Eckel的书《 Thinking in Java》。我在内部类一章中遇到了这个断言,它说:“使用本地内部类而不是匿名内部类的唯一理由是是否需要命名的构造函数和/或重载的构造函数”

我现在不太了解,但是: 这是在方法内部重载Inner(本地类)的构造函数的方法吗?

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

} 外层课程

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;
        }
    };
}

} 预先感谢?

2 个答案:

答案 0 :(得分:1)

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实例被实现为匿名类。创建printA时,不能使用它来创建printB。您应该从头开始创建匿名类(即,覆盖所有抽象方法)。

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

答案 1 :(得分:1)

好的,什么时候有此代码

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();   
    }
}

在这种情况下,我不会创建单个匿名内部类的乘法实例。相反,我正在创建使用相同源代码的多个匿名类。那是里格斯吗? 谢谢