抽象类不可以实例化 可是我在许多源码中东看到有这种情况啊
比如说最常见的有
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;
}
}还是看不明白
哪为大哥给小弟讲讲啊。点拨一下就好 呵呵 谢了
比如说最常见的有
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;
}
}还是看不明白
哪为大哥给小弟讲讲啊。点拨一下就好 呵呵 谢了
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;
}
}
具体操作应该在
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
好像只有两个子类 究竟是哪个啊?
if (g instanceof ConstrainableGraphics) {
((ConstrainableGraphics) g).constrain(x, y, width, height);
} else {
g.translate(x,y);
g.setClip(0, 0, width, height);
}
ConstrainableGraphics 是个抽象接口