![](/img/trans.png)
[英]How and Where was the my repository interface instanced by SimpleJdbcRespository?
[英]How to shadow an interface variable with an instanced variable
我在使用instanced变量遮蔽接口变量时遇到了一些麻烦。 我知道如何通过重写方法并在方法中手动分配变量来做到这一点,但无法弄清楚如何使用任何对象的实例变量来调用方法。
public interface ShakesHands {
static final String name = "Gabe";
public void shakeHands (ShakesHands other);
}
class Student implements ShakesHands{
String name;
@Override
public void shakeHands(ShakesHands other) {
String othersName = other.name;
System.out.println(name + " shook " + othersName + "'s hand.");
}
}
class Parent implements ShakesHands{
String name;
@Override
public void shakeHands(ShakesHands other) {
String othersName = other.name;
System.out.println(name + " shook " + othersName + "'s hand.");
}
}
public class App {
public static void main(String[] args) {
Student student1 = new Student();
student1.name = "Bob";
Parent parent1 = new Parent();
parent1.name = "Sally";
student1.shakeHands(parent1);
}
}
这段代码将输出“Bob摇摇Gabe的手”。 有什么方法可以阻止它引用接口名称“Gabe”,而是引用实例名称“Sally”,以便我得到“Bob震惊Sally的手”?
抛开编码风格和清晰代码的问题,这就是为什么你的代码总是打印“......摇晃Gabe的手。”:
握手的方法实现引用了ShakesHands实例中的“name”,而不是其中一个实现类。 由于ShakesHands中唯一的“名称”在这里“在范围内”,因此您总是最终使用值为“Gabe”的静态变量。
实际上,编译器总是更好地使用静态变量值,而不是使用实现类的变量(如果存在这样的变量)。 派生类或实现类中的数据类型不需要与超类/接口中的数据类型相同,因此您可以使ShakeHand的名称与Student的名称不同。
例:
public interface ShakesHands {
String name = "Gabe";
public void shakeHands(ShakesHands other);
}
class Student implements ShakesHands {
Integer name = Integer.valueOf(0);
@Override
public void shakeHands(ShakesHands other) {
System.out.println(name.getClass().getSimpleName() + " (in this class) with value "+name+" vs. " + other.name.getClass().getSimpleName()+" (in other class) with value "+other.name);
}
}
对于我的示例调用,printet文本是“Integer(在此类中),值为7 vs. String(在其他类中),值为Gabe”。
另一件事:即使在程序的所有实现中,实现类中都有一个“name”变量:在编译时,编译器不知道在运行时是否仍然如此。 您的JAR可能在另一个定义“UnnamedShakesHand”类(没有“name”变量)的程序中使用。 那会发生什么? 如果实现类用另一个类定义“name”,你的代码会发生什么? 它应该抛出“ClassCastException”,因为你的“String othersName = other.name;” 指令?
简而言之:在“ShakesHands”界面中引入“String getName()”方法。 每个实现类都可以返回其名称的变量值,一切都很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.