??xml version="1.0" encoding="utf-8" standalone="yes"?>
目临近l束Q不得不感叹Q时间过的真快啊......?nbsp; 阅读全文
]]>
]]>
]]>
]]>
]]>
]]>
]]>
]]>
自定义ClassLoader的实玎ͼ 阅读全文
]]>
]]>
/**
*title 用多U程实现厨师与服务生的问?br />*@author:realsmy
*date 2006-10-22 14:10
*/
public class Test{
public static void main(String args[]){
CanGuan c=new CanGuan();
new Thread(new ChuShi(c)).start();
new Thread(new FuWuSheng(c)).start();
}
}
//厨师一直执行餐馆类的set()Ҏ
class ChuShi implements Runnable{
CanGuan c;
public ChuShi(CanGuan c){
this.c=c;
}
public void run(){
while(true){
c.set();
}
}
}
//服务生一直执行餐馆类的get()Ҏ
class FuWuSheng implements Runnable{
CanGuan c;
public FuWuSheng(CanGuan c){
this.c=c;
}
public void run(){
while(true){
c.get();
}
}
}
class CanGuan
{
private boolean b = true;
private int i =1;
public synchronized void set()
{
if(!b)
try{
wait();
}catch(Exception e){}
System.out.println("厨师做好了菜"+i);
try{
Thread.sleep(1000);
}catch(Exception e){}
b = false;
notify();
}
public synchronized void get()
{
if(b)
try{
wait();
}catch(Exception e){}
System.out.println("服务生取C?+i);
i++;
try{
Thread.sleep(1000);
}catch(Exception e){}
b = true;
notify();
}
}
public class B extends A {
int i=2;
public static void main (String [] args) {
B b = new B();
System.out.println(b.getI());
}
}
l果?,而不?.
q个问题感觉q不错?br />
1.q不是private成员不会被?事实上子cd以承父cȝM变量和方法。private、protected只是讉K权限而已。即使i是public?br />输出l果也肯定是1.
2.Ҏ可以覆盖,成员变量q没有覆盖一_也就是子cȝi和父cȝi是共存在子类体内的。父cd象中有i变量、set get两个Ҏ。子cclass中应有i【承于父类对象】、i【子cd象】两个变量、set get两个Ҏ【承于父类对象】?/p>
3.java中的原则是调用的是哪个类的方法,那么q个Ҏ讉K的就是这个类中的成员。子cM没有覆盖public int getI()q个Ҏ的话,那么子类调用的其实是父类的getI()Ҏ.所以返回的是父cM的i.
所以如果子c覆盖了getI()Ҏ,那么׃输出的是子类中的i了,因ؓ调用的是子类中的Ҏ?br />
以上是我曄遇到的一个问?今天遇到另一个例?q而进行思?
class A{
public int m = 1;
A(){
System.out.println("A have construct");
System.out.println(m);
tt();//因ؓtt()Ҏ已经被覆?所以此处调用的是子cȝtt()Ҏ;
//子类的数据成员此时还未显式的初始?所以返回gؓ默认?;
}
public void tt(){
System.out.println(m);
}
}
public class B extends A{
public int n = 2;
B(){
System.out.println("B have construct");
System.out.println(n);
tt();
}
public void tt(){
System.out.println(n);
}
public static void main(String args[]){
B b = new B();
}
}
输出l果?
A have construct
1
0
B have construct
2
2
如注释部分的解说!
q个例子?B中的tt()Ҏ已经覆盖了父cM的tt()Ҏ.所以在执行父类的构造方法时,调用的是子类的tt()Ҏ;
PS:
构造一个对象的q程?
先父c?后子c?
先成员变量初始化,后构造函?
private SigCls(String aName, int aAge) {
name = aName;
age = aAge;//初始化两个成员属?br /> }
public String getName() {
return name;
}
public int getAge() {
return age;
}
public static SigCls getInstance() {
if (inst == null)
{
System.out.println("none");
inst = new SigCls("xiaozhang", 19);//初始化实?br /> return inst;
}
else
{
System.out.println("Sig");
return inst;
}
}
}
//TestSigCls.java文g
public class TestSigCls
{
public static void main(String[] args)
{
SigCls sc1;
SigCls sc2;
sc1 = SigCls.getInstance();
sc2 = SigCls.getInstance();
System.out.println(sc1.getName());
System.out.println(sc2.getName());
}
}
应该q有其他的方?有待研究!
class Clock extends JFrame
{
public static double PI = 3.14159265;
Calendar now;
int hh;
int mm;
int ss;
Clock()
{
super("我的旉");
setSize(400,400);
setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRoundRect(100,100,200,200,200,200);
g.setColor(Color.BLACK);
g.drawString("1",(int)(200-2+100*(Math.sin(30*2*PI/360))),(int)(200+10-100*(Math.cos(30*2*PI/360))));
g.drawString("2",(int)(200-6+100*(Math.sin(30*2*2*PI/360))),(int)(200+10-100*(Math.cos(30*2*2*PI/360))));
g.drawString("3",(int)(200-7+100*(Math.sin(30*3*2*PI/360))),(int)(200-100*(Math.cos(30*3*2*PI/360))));
g.drawString("4",(int)(200-6+100*(Math.sin(30*4*2*PI/360))),(int)(200-100*(Math.cos(30*4*2*PI/360))));
g.drawString("5",(int)(200-2+100*(Math.sin(30*5*2*PI/360))),(int)(200-2-100*(Math.cos(30*5*2*PI/360))));
g.drawString("6",(int)(200+100*(Math.sin(30*6*2*PI/360))),(int)(200-100*(Math.cos(30*6*2*PI/360))));
g.drawString("7",(int)(200+100*(Math.sin(30*7*2*PI/360))),(int)(200-100*(Math.cos(30*7*2*PI/360))));
g.drawString("8",(int)(200+100*(Math.sin(30*8*2*PI/360))),(int)(200-100*(Math.cos(30*8*2*PI/360))));
g.drawString("9",(int)(200+100*(Math.sin(30*9*2*PI/360))),(int)(200-100*(Math.cos(30*9*2*PI/360))));
g.drawString("10",(int)(200+2+100*(Math.sin(30*10*2*PI/360))),(int)(200+2-100*(Math.cos(30*10*2*PI/360))));
g.drawString("11",(int)(200+100*(Math.sin(30*11*2*PI/360))),(int)(200+10-100*(Math.cos(30*11*2*PI/360))));
g.drawString("12",(int)(200+100*(Math.sin(30*12*2*PI/360))),(int)(200+10-100*(Math.cos(30*12*2*PI/360))));
g.setColor(Color.RED);
Calendar now=Calendar.getInstance();
int hh=now.get(Calendar.HOUR_OF_DAY);//时
int mm=now.get(Calendar.MINUTE);//分钟
int ss=now.get(Calendar.SECOND);// U?
g.drawLine(200,200,(int)(200+90*(Math.sin(6*ss*2*PI/360))),(int)(200-90*(Math.cos(6*ss*2*PI/360)))); //ȝ?br /> g.setColor(Color.black);
g.drawLine(200,200,(int)(200+70*(Math.sin(6*mm*2*PI/360))),(int)(200-70*(Math.cos(6*mm*2*PI/360))));//d?br /> g.setColor(Color.blue);
g.drawLine(200,200,(int)(200+50*(Math.sin((30*hh+0.5*mm)*2*PI/360))),(int)(200-50*(Math.cos((30*hh+0.5*mm)*2*PI/360)))); //L?br /> try{Thread.sleep(500);}catch(Exception e){}
repaint();
}
public static void main(String[] args)
{
Clock c = new Clock();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class YouXi extends JFrame
{
Container c;
Draw_table d_table; //中间的画?br /> Mypanel panel_east; //双的面?br /> private static Icon[] faces;
YouXi()
{
super("ȝ坦克 版权所有:久城");
c = getContentPane();
c.setLayout(new BorderLayout());
panel_east = new Mypanel();
panel_east.setLayout(new GridLayout(6,1,20,20));
faces= new Icon[]{new ImageIcon("d:\11.jpg","")};
JButton button1 = new JButton("重新开?);
JButton button2 = new JButton(new String ("ȝ坦克"),new ImageIcon(getClass().getResource("11.jpg")));
JTextField text1 = new JTextField("realsmy");
JTextField text2 = new JTextField("0");
JLabel lb1 = new JLabel("玩??);
JLabel lb2 = new JLabel("杀人数");
text1.setEditable(false);
text2.setEditable(false);
panel_east.add(button2);
panel_east.add(lb1);
panel_east.add(text1);
panel_east.add(lb2);
panel_east.add(text2);
panel_east.add(button1);
//d键盘监听Q不知道加到哪好Q随表找个按钮加上去了哈哈!
button2.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
if(d_table.fangxiang == 1)
{
d_table.zx = d_table.x+50;
d_table.zy = d_table.y+10;
d_table.key_fangxiang = 1;
}
if(d_table.fangxiang == 2)
{
d_table.zx = d_table.x+10;
d_table.zy = d_table.y+50;
d_table.key_fangxiang = 2;
}
if(d_table.fangxiang == 3)
{
d_table.zx = d_table.x-10;
d_table.zy = d_table.y+10;
d_table.key_fangxiang = 3;
}
if(d_table.fangxiang == 4)
{
d_table.zx = d_table.x+10;
d_table.zy = d_table.y-10;
d_table.key_fangxiang = 4;
}
d_table.key = true;
}
if(e.getKeyCode()==KeyEvent.VK_UP){
d_table.y-=10;
d_table.fangxiang = 4;
}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
d_table.y += 10;
d_table.fangxiang = 2;
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
d_table.x -= 10;
d_table.fangxiang = 3;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
d_table.x +=10;
d_table.fangxiang = 1;
}
}
}
);
d_table = new Draw_table();
d_table.setBackground(Color.red);
c.add(panel_east,BorderLayout.EAST);
c.add(d_table,BorderLayout.CENTER);
setSize(600,600);
setLocation(200,100);
setVisible(true);
}
public static void main(String args[])
{
YouXi yx = new YouXi();
yx.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
//定义Lc?br />class Draw_table extends Canvas// implements runnable
{
static boolean key = false;
static int x=20,y=20; //坦克坐标
static int zx,zy; //子弹L坐标
static int fangxiang = 1; //定义车头方向
static int key_fangxiang = 1; //定义子弹方向
int k = 1;
public void paint(Graphics g)
{
if(fangxiang == 1)
{
g.setColor(Color.black);
g.fillRect(x,y,50,30); //车n
g.fillRect(x+50,y+10,10,10);//车头=子弹
g.setColor(Color.blue);
g.fillOval(x+15,y+5,20,20);
}
if(fangxiang == 2)
{
g.setColor(Color.black);
g.fillRect(x,y,30,50); //车n
g.fillRect(x+10,y+50,10,10);//车头=子弹
g.setColor(Color.blue);
g.fillOval(x+5,y+15,20,20);
}
if(fangxiang == 3)
{
g.setColor(Color.black);
g.fillRect(x,y,50,30); //车n
g.fillRect(x-10,y+10,10,10);//车头=子弹
g.setColor(Color.blue);
g.fillOval(x+15,y+5,20,20);
}
if(fangxiang == 4)
{
g.setColor(Color.black);
g.fillRect(x,y,30,50); //车n
g.fillRect(x+10,y-10,10,10);//车头=子弹
g.setColor(Color.blue);
g.fillOval(x+5,y+15,20,20);
}
if(k==1)
{
try{Thread.sleep(200);}catch(Exception e){}
}
if(key == true)
{
//new Thread(new draw_table()).start();
//key = false;
//new Zidan_thread().start();
k = 0;
zidan();
}
repaint();
}
public void zidan()
{
Graphics g = getGraphics();
g.setColor(Color.yellow);
g.fillRect(zx, zy, 10, 10);
if(key_fangxiang == 1)
{
zx+=10;
}
if(key_fangxiang == 2)
{
zy+=10;
}
if(key_fangxiang == 3)
{
zx-=10;
}
if(key_fangxiang == 4)
{
zy-=10;
}
try{Thread.sleep(200);}catch(Exception e){}
}
//public void run()
//{
// zidan();
//}
}
//面板cd?br />class Mypanel extends JPanel
{
public Insets insets()
{
return new Insets(60,10,200,10);
}
}
class Pic_button extends JFrame
{
Container c;
Pic_button()
{
super("realsmy");
c = getContentPane();
c.setLayout(new BorderLayout());
JButton button = new JButton(new ImageIcon(getClass().getResource("11.gif")));
c.add(button,BorderLayout.CENTER);
setSize(600,600);
setLocation(200,100);
setVisible(true);
}
public static void main(String args[])
{
Pic_button pb = new Pic_button();
pb.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
</code>