我能够非常快地学习和掌握东西,但这仍然让我感到困惑:
这是在主类(DCFlags)中:
private WGCustomFlagsPlugin pluginWGCustomFlags;
private WorldGuardPlugin pluginWorldGuard;
private DCPvPToggle pluginDCPvPToggle;
private RegionListener listener;
public WGCustomFlagsPlugin getWGCFP(){
return this.pluginWGCustomFlags;
}
public WorldGuardPlugin getWGP() {
return this.pluginWorldGuard;
}
public DCPvPToggle getPPT(){
return this.pluginDCPvPToggle;
}
public void onEnable(){
this.pluginWorldGuard = Utils.getWorldGuard(this);
this.pluginWGCustomFlags = Utils.getWGCustomFlags(this);
this.pluginDCPvPToggle = Utils.getDCPvPToggle(this);
this.listener = new RegionListener(this);
}这是一个不同的类(Utils):
public static WGCustomFlagsPlugin getWGCustomFlags(DCFlags plugin){
Plugin wgcf = plugin.getServer().getPluginManager().getPlugin("WGCustomFlags");
if ((wgcf == null) || (!(wgcf instanceof WGCustomFlagsPlugin))) {
return null;
}
return (WGCustomFlagsPlugin)wgcf;
}
public static WorldGuardPlugin getWorldGuard(DCFlags plugin){
Plugin wg = plugin.getServer().getPluginManager().getPlugin("WorldGuard");
if ((wg == null) || (!(wg instanceof WorldGuardPlugin))) {
return null;
}
return (WorldGuardPlugin)wg;
}
public static DCPvPToggle getDCPvPToggle(DCFlags plugin){
Plugin ppt = plugin.getServer().getPluginManager().getPlugin("DCPvPToggle");
if ((ppt == null) || (!(ppt instanceof DCPvPToggle))) {
return null;
}
return (DCPvPToggle)ppt;
}我知道这是为了能够使用其他插件的方法,但什么是“这”。它的用途以及为什么需要它?
发布于 2013-06-09 23:40:54
this始终是对当前对象的引用。
在这些示例中,它不是必需的。但是,请考虑以下几点:
class C {
private String name;
public void setName(String name) {
this.name = name;
}
}在本例中,this关键字用于区分传递给setName方法的局部变量name和在类中声明的字段this.name。
还要考虑以下几点:
class C {
private String name;
public void doSomething(final String name) {
// here, `this` is an instance of C
new Runnable() {
@Override
public void run() {
// here, `this` is an instance of Runnable
System.out.println(name);
// prints the name passed to the method
System.out.println(this.name);
// error: Runnable has no field name
System.out.println(C.this.name);
// prints the enclosing class's name
}
}.run();
}
}在其他一些语言中,比如Python语言,总是需要使用self. (this.的大致语义等价物)来引用字段。在Java中,情况并非如此。
https://stackoverflow.com/questions/17011212
复制相似问题