《Java语言程序设计》Java中实现多线程有几种方式_第1页
《Java语言程序设计》Java中实现多线程有几种方式_第2页
《Java语言程序设计》Java中实现多线程有几种方式_第3页
《Java语言程序设计》Java中实现多线程有几种方式_第4页
《Java语言程序设计》Java中实现多线程有几种方式_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

《Java语言程序设计》Java中实现多线程有几种方式?一、简答题1什么是多态性?方法的重载和覆盖有何区别?阅读下列代码,指出其中存在的重载和覆盖,写出输出结果是什么?解释为什么这样输出?(15分)classClass1{ publicvoidfind(){ System.out.println("Class1.find"); }}classClass2extendsClass1{ publicvoidfind(){ System.out.println("Class2.find"); }}classClass3{ publicvoidget(Class1one){ System.out.println("get(Class1)"); one.find(); } publicvoidget(Class2two){ System.out.println("get(Class2)"); two.find(); }}publicclassTest1{ publicstaticvoidmain(String[]args) { Class1one=newClass2(); Class3three=newClass3(); three.get(one); }}get(Class1)Class2.find多态:动态性的概念也可以被说成一个接口,多个方法。Java实现运行时多态性的基础是动态方法调度

方法重载发生在同一个类中,许多名称相同而签名不同或返回类型不同的方法覆盖就是方法名和参数都要相同,但现实的方式不同覆盖是发生在子类中,来覆盖(重写)父类的方法Class2继承Class1其中Class2对Class1find方法进行了覆盖Class3中对get方法进行了重载输出如上结果是因为多态,调用的是Class1的find方法。2、请说说final、finally的区别和作用,举例说明用法;另外用自己的语言介绍throw/throws有什么联系和区别?在程序中应如何使用?(15分)final用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖,类不可继承finally是异常处理语句结构的一部分,表示总是执行如:方法finalvoidfunc(){System.out.println("dwdw");}属性finalintp;try{

}catch(Exceptione){

e.printStackTrace();

}finally{

}3、编写一个描述老师基本情况的类,属性包括姓名,教工号,基本工资,岗位工资和绩效工资,方法包括信息输出,设置姓名和教工号,设置三种工资金额,计算总工资(三种工资加起来)和税后工资(按如下方式计算,3000以内不收税,3000-5000之间的部分扣10%,大于5000的部分扣15%)。在main方法中对方法进行测试(15分)publicclassTestTeacher{staticclassTeacher{privateStringxingming;privateStringjiaogonghao;privatefloatjibengongzhi;privatefloatgangweigongzhi;privatefloatjixiao;publicStringgetXingming(){returnxingming;}publicvoidsetXingming(Stringxingming){this.xingming=xingming;}publicStringgetJiaogonghao(){returnjiaogonghao;}publicvoidsetJiaogonghao(Stringjiaogonghao){this.jiaogonghao=jiaogonghao;}publicfloatgetJibengongzhi(){returnjibengongzhi;}publicvoidsetJibengongzhi(floatjibengongzhi){this.jibengongzhi=jibengongzhi;}publicfloatgetGangweigongzhi(){returngangweigongzhi;}publicvoidsetGangweigongzhi(floatgangweigongzhi){this.gangweigongzhi=gangweigongzhi;}publicfloatgetJixiao(){returnjixiao;}publicvoidsetJixiao(floatjixiao){this.jixiao=jixiao;}publicfloatzhonggongzhi(){returnjibengongzhi+gangweigongzhi+jixiao;}publicfloatshuihougongzhi(){floatzhonggongzhi=zhonggongzhi();if(zhonggongzhi<3000){returnzhonggongzhi;}elseif(zhonggongzhi>=3000&&zhonggongzhi<5000){returnzhonggongzhi-(zhonggongzhi-3000)*0.1f;}elseif(zhonggongzhi>=5000){returnzhonggongzhi-(zhonggongzhi-3000)*0.15f;}return-1;}}publicstaticvoidmain(String[]args){Teacherteacher=newTeacher();teacher.setXingming("张大山");teacher.setJiaogonghao("12345678");teacher.setJibengongzhi(3000);teacher.setGangweigongzhi(2000);teacher.setJixiao(8000);System.out.println("zhonggongzhi:"+teacher.zhonggongzhi());System.out.println("shuihougongzhi:"+teacher.shuihougongzhi());}}4、Java中实现多线程有几种方式?这几种方式有什么区别?然后采取其中一种方式设计一个线程例子,在例子中构造4个线程对象实现对同一数据类对象进行操作(数据初始值为10),其中线程对象1对数据执行乘以10的操作,线程对象2对数据执行乘以20的操作,对象3对数据执行+30的操作,线程对象4对数据执行+40的操作,要求考虑线程同步,保证每一步数据操作的正确性。要求提供程序代码以及运行结果截图(15分)答:常用的方式有1、继承Thread类2、实现Runnable接口publicclassThreadTest{privateintnumber=10;publicstaticvoidmain(String[]args){ThreadTesttestThread=newThreadTest();newThread(newRunnableImpl1(testThread)).start();newThread(newRunnableImpl3(testThread)).start();newThread(newRunnableImpl2(testThread)).start();newThread(newRunnableImpl(testThread)).start();}publicvoidxiugai(intthreadType){synchronized(this){switch(threadType){case0:number=number*10;break;case1:number=number*20;break;case2:number=number+30;break;case3:number=number+40;break;default:break;}System.out.println(Thread.currentThread().getName()+":"+number);}}staticclassRunnableImplimplementsRunnable{privateThreadTesttestThread;publicRunnableImpl(ThreadTesttestThread){this.testThread=testThread;}@Overridepublicvoidrun(){try{while(true){testThread.xiugai(3);sleep(3000);}}catch(InterruptedExceptionex){}}}staticclassRunnableImpl1implementsRunnable{privateThreadTesttestThread;publicRunnableImpl1(ThreadTesttestThread){this.testThread=testThread;}@Overridepublicvoidrun(){try{while(true){testThread.xiugai(0);sleep(3000);}}catch(InterruptedExceptionex){}}}staticclassRunnableImpl2implementsRunnable{privateThreadTesttestThread;publicRunnableImpl2(ThreadTesttestThread){this.testThread=testThread;}@Overridepublicvoidrun(){try{while(true){testThread.xiugai(2);sleep(3000);}}catch(InterruptedExceptionex){}}}二、编程题1、编写一个图形用户界面程序,包含两个按钮,一个信息标签(label)和一个显示面板,两个按钮分别为“掷色子”和“移动”,在显示面板中显示一个小汽车(用小圆\矩形以及线绘制),随机设定小汽车的初始位置,当点击“掷色子”按钮,随机产生移动信息(上移,下移,左移,右移,移动几步),并显示在信息标签中,点击移动,按照产生的移动信息,让小汽车进行移动。要求提供完整程序代码以及运行结果截图(20分)publicclasstestextendsjavax.swing.JFrame{intx=100;inty=100;intrandomNum=0;intrandomPosition=0;String[]strs={"上移","下移","左移","右移"};publicmain(){initComponents();}@SuppressWarnings("unchecked")//<editor-folddefaultstate="collapsed"desc="GeneratedCode">privatevoidinitComponents(){jButton1=newjavax.swing.JButton();jButton2=newjavax.swing.JButton();jLabel1=newjavax.swing.JLabel();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);jButton1.setText("掷色子");jButton1.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jButton1ActionPerformed(evt);}});jButton2.setText("移动");jButton2.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jButton2ActionPerformed(evt);}});javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addGap(42,42,42).addComponent(jButton2).addGap(43,43,43).addComponent(jLabel1,javax.swing.GroupLayout.PREFERRED_SIZE,95,javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,53,Short.MAX_VALUE).addComponent(jButton1).addGap(41,41,41)));layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(31,31,31).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jButton2).addComponent(jButton1).addComponent(jLabel1,javax.swing.GroupLayout.PREFERRED_SIZE,32,javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap(237,Short.MAX_VALUE)));pack();}//</editor-fold>privatevoidjButton1ActionPerformed(java.awt.event.ActionEventevt){Randomrandom=newRandom();randomNum=random.nextInt(4)+1;randomPosition=random.nextInt(4);jLabel1.setText(strs[randomPosition]+randomNum+"步");}privatevoidjButton2ActionPerformed(java.awt.event.ActionEventevt){repaint();}publicstaticvoidmain(Stringargs[]){try{for(javax.swing.UIManager.LookAndFeelInfoinfo:javax.swing.UIManager.getInstalledLookAndFeels()){if("Nimbus".equals(info.getName())){javax.swing.UIManager.setLookAndFeel(info.getClassName());break;}}}catch(ClassNotFoundException|InstantiationException|IllegalAccessException|javax.swing.UnsupportedLookAndFeelExceptionex){java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);}//</editor-fold>//</editor-fold>/*Createanddisplaytheform*/java.awt.EventQueue.invokeLater(newRunnableImpl());}@Overridepublicvoidpaint(Graphicsg){super.paint(g);switch(randomPosition){case0:y=y-randomNum*15;break;case1:y=y+randomNum*15;break;case2:x=x-randomNum*15;break;case3:x=x+randomNum*15;break;default:break;}g.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论