gpt4 book ai didi

java - 方法内重载构造函数 InnerClass

转载 作者:行者123 更新时间:2023-12-02 01:54:56 24 4
gpt4 key购买 nike

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

我现在不知道我是否理解得很好,但是:这是在方法内部重载内部(本地类)构造函数的方法吗?

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

}提前致谢?

最佳答案

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()形式中使用它来创建新实例。除此之外,还可以使用非默认构造函数。

关于java - 方法内重载构造函数 InnerClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52442687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com