初学java 求教

sxnucseven 2006-08-11 10:50:17
抽象类不可以实例化 可是我在许多源码中东看到有这种情况啊
比如说最常见的有
Graphics g=[Component].getGraphics()
这里getGraphics()函数返回的是Graphics的具体子类吗?
函数具体定义是:
public Graphics getGraphics() {
if (peer instanceof LightweightPeer) {
// This is for a lightweight component, need to
// translate coordinate spaces and clip relative
// to the parent.
if (parent == null) return null;
Graphics g = parent.getGraphics();
if (g == null) return null;
if (g instanceof ConstrainableGraphics) {
((ConstrainableGraphics) g).constrain(x, y, width, height);
} else {
g.translate(x,y);
g.setClip(0, 0, width, height);
}
g.setFont(getFont());
return g;
} else {
ComponentPeer peer = this.peer;
return (peer != null) ? peer.getGraphics() : null;
}
}

还是看不明白
哪为大哥给小弟讲讲啊。点拨一下就好 呵呵 谢了
...全文
191 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxnucseven 2006-08-11
  • 打赏
  • 举报
回复
我就是不知道 Graphics 类 是实例化的那个子类 我想找到它研究一下
sxnucseven 2006-08-11
  • 打赏
  • 举报
回复
是在这里:
if (g instanceof ConstrainableGraphics) {
((ConstrainableGraphics) g).constrain(x, y, width, height);
} else {
g.translate(x,y);
g.setClip(0, 0, width, height);
}
ConstrainableGraphics 是个抽象接口
sxnucseven 2006-08-11
  • 打赏
  • 举报
回复
这是个递归过程
具体操作应该在
if (g instanceof ConstrainableGraphics) {
((ConstrainableGraphics) g).constrain(x, y, width, height);
} else {
g.translate(x,y);
g.setClip(0, 0, width, height);
}
g.setFont(getFont());
return g;
} else {
ComponentPeer peer = this.peer;
return (peer != null) ? peer.getGraphics() : null;
}
里吧
就是我找不到啊。
Graphics
好像只有两个子类 究竟是哪个啊?
meadking 2006-08-11
  • 打赏
  • 举报
回复
接口
public interface MyIn {
void MySpring(String name);

}

子类
public class MySpringDo implements MyIn {
public MySpringDo() {
}

public void MySpring(String name) {
// TODO Auto-generated method stub
System.out.println("MySpring---good work");
System.out.println("void"+name);
}

}
测试类
public class TestMySpring {

public static void main(String[] args) {
MyIn testMySpring = new MySpringDo();///可以事例化他的子类呵呵

testMySpring.MySpring;
}
}
dreamover 2006-08-11
  • 打赏
  • 举报
回复
Graphics g = parent.getGraphics();

再往getGraphics()里面找
sxnucseven 2006-08-11
  • 打赏
  • 举报
回复
可是getGraphics()函数中 我看不出返回的是哪个子类啊
dreamover 2006-08-11
  • 打赏
  • 举报
回复
返回某个不是抽象类的子类实例

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧