1、java复习指南设计2复习指南:考试题型:1、选择题,30题 共45分3、读程序 4题 共20分4、简答题 2题 10分3、编程题 25分 (2题)复习重点:前13章 和22章选择题:出题思路:围绕吴老师理论课所讲内容出题,以基本概念、基本语法以及类库知识等等:这一部分大家要多看书,跟我之前考PASCAL有点像。 简答题复习重点:1、什么是多态,java中如何实现多态?多态,也称为动态绑定,指在执行期间(而非编译期间)判断所引用对象的实际类型,根据其实际的类型调用其相应的方法。 子类重写父类的方法,并把子类对象的引用赋值给父类,当你调用父类里面被覆盖方法的时候, 实际当中new的是哪个对象,
2、就调用当前这个对象(子类对象)的与之相关的覆盖方法。2、什么叫容器布局,布局都有哪些?布局是指:将组件按一定的顺序和规则放在窗体上的方式。有BorderLayout、FlowLayout、GridLayout、GridBagLayout、BoxLayout、绝对定位。3、Java中有一种小应用程序片,Applet,简述如何工作的 Applet是一种可以在Internet上传递,并在Web浏览器中运行的程序(出于安全性,只能在所谓的沙盒中运行)。含有Applet的网页的HTML文件代码中部带有 和这样一对标记,当支持Java的网络浏览器遇到这对标记时,就将下载相应的小应用程序代码并在本地计算机上
3、执行该Applet。 具体步骤:用Java语言编写一个Applet源程序; 把Applet源程序编译成字节码; 将Applet字节码嵌入Web页,即设计一个包含Applet的HTML文件; 用带有Java解释功能的浏览器浏览嵌有Applet的Web页。此时,浏览器会根据 HTML文件的有关标记下载相应的Applet,然后,由浏览器的字节码解释器边解释边执行。4、迭代器的作用及其优势作用:用来抓取容器中的元素,并将抓取到的元素提交给迭代器用户。不用重写代码就可以应用于不同类型的容器。优点:可以非常方便的遍历整个容器中的对象序列,而不用关心底层的结构是什么。此外迭代器被称为轻量级对象:创建它的代价
4、很小。5、内部类的作用概念:定义在另一个类的定义的内部的类叫做内部类。作用:允许你把一些逻辑相关的类组织在一起,并控制位于内部的类的可视性。内部类还是一种代码隐藏机制,更重要的是,它了解外围类,并能与之通信;1.内部类可以很好的实现隐藏,一般的非内部类是不允许有 private 与protected权限的,但内部类可以2.内部类拥有外围类的所有元素的访问权限 3.可以实现多重继承 4.可以避免修改接口而实现同一个类中两种同名方法的调用。6、类加载器的作用 通过一个类的全限定名来获取定义此类的二进制字节流这个动作是在Java虚拟机外部来实现的,以便让应用程序自己决定如何去获取所需要的类。实现这个
5、动作的代码模块被称为“类加载器”。7、设计和实现图形用户界面的工作主要有哪几方面? 搭界面:创建组成界面的各个元素,指定它们的属性和位置关系,从而构成完整的GUI外观。写代码:定义好各个界面元素对不同事件的响应,从而实现GUI与用户的交互功能。 首先要了解可以设计图形用户的各种组件以及用法 利用布局管理器在窗体上巧妙布局,并且给界面美工 绑定相应的事件模型,例如常见的动作事件、鼠标事件、键盘事件等等。编程题的复习重点:1) 创建一个名为Amphibian的类。由此继承产生一个称为Frog 的类。在基类中设置适当的方法。在main( )中,创建一个Frog并向上转型至Amphibian,然后说明
6、所有方法都可以工作。(P140-16)class Amphibian protected void swim() System.out.println(Amphibian swim); protected void speak() System.out.println(Amphibian speak); void eat() System.out.println(Amphibian eat); static void grow(Amphibian a) System.out.println(Amphibian grow); a.eat(); public class Frog extends
7、Amphibian public static void main(String args) Frog f = new Frog(); / call base-class methods: f.swim(); f.speak(); f.eat(); / upcast Frog to Amphibian argument: Amphibian.grow(f); /*Output:Amphibian swimAmphibian speakAmphibian eatAmphibian growAmphibian eat2) 编写一个名为Outer的类,它包含一个名为Inter的类。在Outer中添加
8、一个方法,它返回一个Inter类型的对象。在main( )中,创建并初始化一个指向某个Inter对象的引用。(p191-1)public class Outer1 class Inner Inner() System.out.println(Inner(); Outer1() System.out.println(Outer1(); / make an Inner from within a non-static method of outer class: Inner makeInner() return new Inner(); public static void main(String
9、 args) Outer1 o = new Outer1(); Inner i = o.makeInner(); /*Output:Outer1()Inner()3) 创建一个Cycle类,它具有子类Unicycle、Bicycle和Tricycle,实现每一个类型的实例都可以经由ride( )方法向上转型为Cycle。class Cycle private String name = Cycle; public static void travel(Cycle c) System.out.println(Cycle.ride() + c); public String toString()
10、return this.name; class Unicycle extends Cycle private String name = Unicycle; public String toString() return this.name; class Bicycle extends Cycle private String name = Bicycle; public String toString() return this.name; class Tricycle extends Cycle private String name = Tricycle; public String t
11、oString() return this.name; public class Biking public static void ride(Cycle c) c.travel(c); public static void main(String args) Unicycle u = new Unicycle(); Bicycle b = new Bicycle(); Tricycle t = new Tricycle(); ride(u); ride(b); ride(t); /*Output:Cycle.ride() UnicycleCycle.ride() BicycleCycle.r
12、ide() Tricycle4) 编写一个类名为chap的应用程序,用于演示JButton按钮操作,当单击左边的按钮时,中间的按钮和左边的按钮将失效,当右边的按钮由失效变成有效. import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPane
13、l; public class Chap public static void main(String args) Frame frame = new Frame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); class Frame extends JFrame public Frame() setTitle(Chap); Panel panel = new Panel(); add(panel);pack(); class Panel extends JPanel JButto
14、n display; JButton left_button = new JButton(左边); JButton mid_button = new JButton(中间); JButton right_button = new JButton(右边); private JPanel panel; private static final long serialVersionUID = 1L; public Panel() setLayout(new BorderLayout(); LeftAction left = new LeftAction(); MidAction mid = new
15、MidAction(); RightAction right = new RightAction(); panel = new JPanel(); panel.setLayout(new GridLayout(1, 4); left_button.addActionListener(left); panel.add(left_button); mid_button.addActionListener(mid); panel.add(mid_button); right_button.addActionListener(right); panel.add(right_button); add(p
16、anel, BorderLayout.CENTER); private class LeftAction implements ActionListener public void actionPerformed(ActionEvent event) display.setText(您点击的是左边的按钮!); private class MidAction implements ActionListener public void actionPerformed(ActionEvent event) display.setText(您点击的是中间的按钮!); private class Rig
17、htAction implements ActionListener public void actionPerformed(ActionEvent event) display.setText(您点击的是右边的按钮!); /*Output:5) 编写一个应用程序,它包括一个文本域和三个按钮,单击每个按钮时在文本区显示不同的文字。import java.awt.event.*;import java.awt.*;import javax.swing.*;public class Button3 extends JFrame private JButton button1=new JButton
18、(button1), button2=new JButton(button2), button3=new JButton(button3); private JTextField txtf=new JTextField(10); public static void run(final JFrame f,final int width,final int height) SwingUtilities.invokeLater(new Runnable() public void run() / TODO Auto-generated method stub f.setDefaultCloseOp
19、eration(JFrame.EXIT_ON_CLOSE); f.setTitle(f.getClass().getSimpleName(); f.setSize(width, height); f.setVisible(true); ); private ActionListener bl=new ActionListener() public void actionPerformed(ActionEvent e) String name=(JButto)e.getSource().getText(); txtf.setText(name); ; public Button3() butto
20、n1.addActionListener(bl); button2.addActionListener(bl); button3.addActionListener(bl); setLayout(new FlowLayout(); add(button1); add(button2); add(button3); add(txtf); public static void main(String args) / TODO Auto-generated method stub run(new Button3(), 400, 300); /*Output:6)、编写程序实现以下继承关系:动物类派生
21、出鸟类、鱼类,鸟类派生出燕子类,鱼类派生出鲨鱼类。要求:每个类包含域“类别”、方法“返回类别”请读懂以下程序并给出运行结果:/程序1public class Guess /* * param args */ public static void main(String args) / TODO Auto-generated method stub int a,b; for(a=14;a=90;a+) for(b=11;b50;b+) if(a-10)=(b-10)*4)&(a+10)%(b+10)=0) System.out.println(ag +a+ daf+b); /*Output:ag
22、 50 daf20/程序2class OverrideDemo void test() System.out.println( No parameters ); void test(int a) System.out.println(a: + a); void test(int a,int b) System.out.println(a and b: + a + + b); void test(double a) System.out.println(double a: + a);Public class Override public static void main(String args
23、) OverrideDemo ob = new OverrideDemo(); ob.test(); ob.test(10); ob.test(10,20); ob.test(123.25); /*Output:No parameters a: 10a and b:10 20double a: 123.25/程序3import java.awt.*;import java.awt.event.*;import java.applet.*;public class ButtonApplet extends Applet implements ActionListener int i1,i2; B
24、utton btn1,btn2; public void init() i1=0; i2=0; btn1=new Button(确定); btn2=new Button(取消); add(btn1); add(btn2); btn1.addActionListener(this); btn2.addActionListener(this); public void paint(Graphics g) g.setColor(Color.blue); g.drawString(你点击了确定+ i1+次,20,100); g.setColor(Color.red); g.drawString(你点击
25、了确定+ i2+次,20,120); public void actionPerformed(ActionEvent e) if(e.getSource()=btn1) i1+; if(e.getSource()=btn2) i2+; repaint(); /*Output: /程序4interface Weapon String name = weapon; void attack();class Tank implements Weapon public void attack() System.out.println(Tank attack); class Cannon implemen
26、ts Weapon public void attack() System.out.println(Cannon attack); class testWeapon public static void main(String args) Weapon wp; wp = new Tank(); wp.attack(); wp = new Cannon(); wp.attack(); /*Output:Tank attackCannon attack/程序5public class Parcel2 class Contents private int i=11; public int value
27、()return i; class Destination private String lable; Destination(String whereTo) lable=whereTo; String readLabel()return lable; public Destination to(String s) return new Destination(s); public Contents contents() return new Contents(); public void ship(String dest) Contents c=contents(); Destination
28、 d=to(dest); System.out.println(d.readLabel(); public static void main(String args) Parcel2 p=new Parcel2(); p.ship(Tasmania); Parcel2 q=new Parcel2(); Parcel2.Contents c=q.contents(); Parcel2.Destination d=q.to(Borneo); /*Output:Tasmania/程序6:import java.util.*;public class SimpleCollection public static void main(String args) int b=0; Collection c=new ArrayList(); for(int i=0;i10;i+) b=i+3; c.add(b); for(Integer
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1