首页上一页 1 下一页尾页 1 条记录 1/1页
JAVA从入门到精通一书遇到的的问题
发表在Java图书答疑
2010-01-19
是否精华
是
否
版块置顶:
是
否
JAVA从入门到精通 174页 例10.12
public class CopyOfQuadrangle {
private CopyOfQuadrangle[] qtest = new CopyOfQuadrangle[6];可以解释下这句的作用吗?
private int nextIndex = 0;
public void draw(CopyOfQuadrangle q) {
if(nextIndex < qtest.length) {
qtest[nextIndex] = q;可以解释下这句的作用吗?
System.out.println(nextIndex);
nextIndex++;
}
}
public static void main(String[] args) {
CopyOfQuadrangle q = new CopyOfQuadrangle();
q.draw(new CopyOfSquare());
q.draw(new CopyOfParallelogramgle());
}
}
class CopyOfSquare extends CopyOfQuadrangle{
public CopyOfSquare(){
System.out.println("正方形");
}
}
class CopyOfParallelogramgle extends CopyOfQuadrangle{
public CopyOfParallelogramgle(){
System.out.println("平行四边形");
}
}
运行结果是
正方形
0
平行四边形
1
根据向上转型原则,在主方法中以CopyOfSquare和CopyOfParallelogramgle为参数调用draw方法,怎么会显示正方形和平行四边形。怎么会调用子类的构造方法,是draw()方法中哪句起的作用?这里我不明白。希望解释下。谢谢
public class CopyOfQuadrangle {
private CopyOfQuadrangle[] qtest = new CopyOfQuadrangle[6];可以解释下这句的作用吗?
private int nextIndex = 0;
public void draw(CopyOfQuadrangle q) {
if(nextIndex < qtest.length) {
qtest[nextIndex] = q;可以解释下这句的作用吗?
System.out.println(nextIndex);
nextIndex++;
}
}
public static void main(String[] args) {
CopyOfQuadrangle q = new CopyOfQuadrangle();
q.draw(new CopyOfSquare());
q.draw(new CopyOfParallelogramgle());
}
}
class CopyOfSquare extends CopyOfQuadrangle{
public CopyOfSquare(){
System.out.println("正方形");
}
}
class CopyOfParallelogramgle extends CopyOfQuadrangle{
public CopyOfParallelogramgle(){
System.out.println("平行四边形");
}
}
运行结果是
正方形
0
平行四边形
1
根据向上转型原则,在主方法中以CopyOfSquare和CopyOfParallelogramgle为参数调用draw方法,怎么会显示正方形和平行四边形。怎么会调用子类的构造方法,是draw()方法中哪句起的作用?这里我不明白。希望解释下。谢谢