我正在为我的一个类做一个任务,在其中,我必须使用Java语法给出静态和动态绑定的示例.
我理解基本概念,即静态绑定在编译时发生,动态绑定在运行时发生,但我无法弄清楚它们实际上是如何工作的.
我在网上找到了一个静态绑定的例子,给出了这个例子:
public static void callEat(Animal animal) {
System.out.println("Animal is eating");
}
public static void callEat(Dog dog) {
System.out.println("Dog is eating");
}
public static void main(String args[])
{
Animal a = new Dog();
callEat(a);
}
Run Code Online (Sandbox Code Playgroud)
并且这将打印"动物正在吃"因为调用callEat使用静态绑定,但我不确定为什么这被认为是静态绑定.
到目前为止,我所看到的所有来源都没有设法以我能够遵循的方式解释这一点.
在下面的代码中,第一个和第二个打印语句如何打印出SubObj?顶部和子指向同一个Sub类吗?
class Top {
public String f(Object o) {return "Top";}
}
class Sub extends Top {
public String f(String s) {return "Sub";}
public String f(Object o) {return "SubObj";}
}
public class Test {
public static void main(String[] args) {
Sub sub = new Sub();
Top top = sub;
String str = "Something";
Object obj = str;
System.out.println(top.f(obj));
System.out.println(top.f(str));
System.out.println(sub.f(obj));
System.out.println(sub.f(str));
}
}
Run Code Online (Sandbox Code Playgroud)
以上代码返回以下结果.
SubObj
SubObj
SubObj
Sub
Run Code Online (Sandbox Code Playgroud) 我写了下面这段代码:
class Plane {}
class Airbus extends Plane {}
public class Main {
void fly(Plane p) {
System.out.println("I'm in a plane");
}
void fly(Airbus a) {
System.out.println("I'm in the best Airbus!");
}
public static void main(String[] args) {
Main m = new Main();
Plane plane = new Plane();
m.fly(plane);
Airbus airbus = new Airbus();
m.fly(airbus);
Plane planeAirbus = new Airbus();
m.fly(planeAirbus);
}
}
Run Code Online (Sandbox Code Playgroud)
结果是:
I'm in a plane
I'm in the best Airbus!
I'm in a plane
Run Code Online (Sandbox Code Playgroud)
不出所料的两个第一调用给予I'm in …
在SO处回答了一个问题,我找到了一个解决方案,如果有可能扩展Class类,那将是很好的:
这个解决方案包括尝试修饰Class类,以便只允许包含某些值,在本例中,是扩展具体类的类C.
public class CextenderClass extends Class
{
public CextenderClass (Class c) throws Exception
{
if(!C.class.isAssignableFrom(c)) //Check whether is `C` sub-class
throw new Exception("The given class is not extending C");
value = c;
}
private Class value;
... Here, methods delegation ...
}
Run Code Online (Sandbox Code Playgroud)
我知道这段代码不是Class最终的,我想知道为什么Class是最终的.我知道它必须与安全有关,但我无法想象延伸Class是危险的情况.你能举一些例子吗?
顺便说一下,我能达到的理想行为的更接近的解决方案是:
public class CextenderClass
{
public CextenderClass(Class c) throws Exception
{
if(!C.class.isAssignableFrom(c)) //Check whether is `C` sub-class
throw new Exception("The given class is …Run Code Online (Sandbox Code Playgroud) 我问这个只是为了澄清我的想法是否正确。
静态/动态类型 如果在编译时变量的类型是已知的,则语言是静态类型的。这实际上意味着您作为程序员必须指定每个变量的类型。示例:Java、C、C++。
如果在运行时解释变量的类型,则语言是动态类型化的。这意味着您作为程序员可以更快地编写代码,因为您不必每次都指定类型。示例:Perl
Static/Dynamic Binding——下面的链接清楚地解释了Static Binding和Dynamic Binding的区别
我想问的主要问题从这里开始。我知道静态范围和动态范围之间的区别。然而,当我经历堆栈溢出时,人们说 C++ 和 Python 是静态作用域的。
在 C++ 中,如果我输入
int void main(){
cout<<i;
int i=15;
}
int i=10;
Run Code Online (Sandbox Code Playgroud)
它可以工作(即使在 java 中)。但是它的 python 等价物
def foo():
print(x)
x=10
x='global'
foo()
Run Code Online (Sandbox Code Playgroud)
给出一个错误。
java ×4
oop ×2
c# ×1
c++ ×1
decorator ×1
overloading ×1
polymorphism ×1
python ×1
reflection ×1
scope ×1
security ×1