我不明白动态无常带来的好处。有人可以向我解释吗?
我正在学习Java,并遇到了“动态方法调度”。我想我了解它的意思,但是我无法理解它带来的好处。 我的意思是,为什么这样
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9,5);
Triangle t = new Triangle(10,8);
Figure figuref;
figuref = f;
System.out.println("Area is: "+figuref.area());
figuref = r;
System.out.println("Area is: "+figuref.area());
figuref = t;
System.out.println("Area is: "+figuref.area());
比这更好吗?
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9,5);
Triangle t = new Triangle(10,8);
System.out.println("Area is: "+f.area());
System.out.println("Area is: "+r.area());
System.out.println("Area is: "+t.area());
有人可以向我解释吗?