关于java的动态绑定 动态绑定:是很重要的特性,它能使程序变得可扩展而不需要重编译已存代码为什么?能不能具体给我讲讲,举个例子之类的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 动态绑定是指“在执行期间”这里说的不是编译时候 判断所引用对象的实际类型,根据其实际的类型调用其相应的方法(实际调用的就是你NEW的那个)我记得THINKING IN JAVA有个很经典的例子 你可以去看看 class Shape { void draw() {} void erase() {} }class Circle extends Shape { void draw() { System.out.println("Circle.draw()"); } void erase() { System.out.println("Circle.erase()"); }}class Square extends Shape { void draw() { System.out.println("Square.draw()"); } void erase() { System.out.println("Square.erase()"); }}class Triangle extends Shape { void draw() { System.out.println("Triangle.draw()"); } void erase() { System.out.println("Triangle.erase()"); }}public class Shapes { public static Shape randShape() { switch((int)(Math.random() * 3)) { default: // To quiet the compiler case 0: return new Circle(); case 1: return new Square(); case 2: return new Triangle(); } } public static void main(String[] args) { Shape[] s = new Shape[9]; // Fill up the array with shapes: for(int i = 0; i < s.length; i++) s[i] = randShape(); // Make polymorphic method calls: for(int i = 0; i < s.length; i++) s[i].draw(); }} ///:~此例子便是动态绑定的例子。。好好看看吧thinking in Java里的 Random(随机数) 【请教】关于换工作直接走人的问题 java学习顺序 请问哪里可以下载 eclipse ???? 求助java.util.Timer问题 一个很小的问题 请进!帮个忙! classpath 设置问题(好容易拿的分哦) 带附件的邮件签名问题 页面特效的一个问题 求助:Java中字符串替换问题?急! 哪位大虾能帮忙写一个自定义事件的处理过程
void draw() {}
void erase() {}
}class Circle extends Shape {
void draw() {
System.out.println("Circle.draw()");
}
void erase() {
System.out.println("Circle.erase()");
}
}class Square extends Shape {
void draw() {
System.out.println("Square.draw()");
}
void erase() {
System.out.println("Square.erase()");
}
}class Triangle extends Shape {
void draw() {
System.out.println("Triangle.draw()");
}
void erase() {
System.out.println("Triangle.erase()");
}
}public class Shapes {
public static Shape randShape() {
switch((int)(Math.random() * 3)) {
default: // To quiet the compiler
case 0: return new Circle();
case 1: return new Square();
case 2: return new Triangle();
}
}
public static void main(String[] args) {
Shape[] s = new Shape[9];
// Fill up the array with shapes:
for(int i = 0; i < s.length; i++)
s[i] = randShape();
// Make polymorphic method calls:
for(int i = 0; i < s.length; i++)
s[i].draw();
}
} ///:~此例子便是动态绑定的例子。。好好看看吧thinking in Java里的