收藏 分销(赏)

Java复习指南.doc

上传人:仙人****88 文档编号:11876010 上传时间:2025-08-18 格式:DOC 页数:17 大小:126.50KB 下载积分:10 金币
下载 相关
Java复习指南.doc_第1页
第1页 / 共17页
Java复习指南.doc_第2页
第2页 / 共17页


点击查看更多>>
资源描述
复习指南: 复习重点:前13章 和22章 出题思路:围绕吴老师理论课所讲内容出题,以基本概念的掌握为出题思路, 考试题型: 1、选择题,14题 共21分(基本概念) 2、填空题 14空 共21分(围绕基本概念PPT) 3、简答题复习重点:(20分,4题) 1) 什么是多态,java中如何实现多态 2) 什么叫容器布局,布局都有哪些 3)请从构造器的引入、构造器的名字和构造器的种类及构造器的特点进行介绍。 初始化,与类名相同,带参数和不带参数(默认构造器), 4)请解释初始化的第一原则和第二原则。 第一原则:可以用构造器来进行初始化 但执行顺序是:先自动初始化,再指定 初始化,最后调用构造器进行初始化 第二原则:初始化的顺序是先静态对象,而后是非静态对象 5)this和super关键字 指向对象本身的引用,通过this关键字让成员函数知道被那里对象调用,即在类的内部代表对象本身 构造器调用构造器 使用super关键字来实现子类中需要调用父类的被覆盖的方法 用于子类构造器调用父类构造器 6)简述java的授权事件模型 7)java中重载与覆盖的区别 重载的定义: 类对自身已有的同名方法的重新定义 本质:利用不同的参数列表,包括形式参数的个数、类型、顺序的不同来区分重载 覆盖的定义:子类对继承自父类方法的重新定义 本质:是指在子类中定义了这样的一个方法: 该方法具有与父类完全相同的返回值 类型、方法名和参数列表(但是重新 编写了方法体) 关键:发生的地点不同 重载发生在同一个类中 覆盖发生在基类与派生类中 8)什么是匿名内部类及其使用原则。 匿名内部类就是没有名字的内部类, 原则:1、匿名内部类不能自行创建构造函数,2匿名内部类一定跟在new的后面,用以隐含实现一个接口或继承一个类 3、匿名内部类不能定义任何静态的成员、方法 4、因匿名内部类为内部类,所以内部类的所有限制都对其生效 9)设计和实现图形用户界面的工作主要有哪几方面? 想好布局 安排好添加布局和控件的顺序 ★ 搭界面 ◆ 创建组成界面的各个元素,指定它们的属性和位置关系,从而构成完整的GUI外观 ★ 写代码 ◆ 定义好各个界面元素对不同事件的响应,从而实现GUI与用户的交互功能 编程题的复习重点:(20分) 1) 自己编写几个连续继承关系的自定义类,编写多个CATCH语句的代码来了解多CACTH语句的异常扑捉顺序 /*自己编写几个连续继承关系的自定义类,编写多个CATCH语句的代码来了解多CACTH 语句的异常扑捉顺序*/ 此代码为师兄们所写,大家借鉴借鉴 class Loferror extends Exception { Loferror() { super("Loferror");//调用Exception类的构造器并初始化 } } class A { public void f() throws Exception, Throwable { throw new RuntimeException(); } } class B extends A { public void f() throws Exception, Throwable { throw new Exception(); } public int rey() { return 1 / 0; } } class C extends A { public void f() throws Throwable { throw new Throwable(); } } public class Test1 { public static void main(String[] args) { B c1 = new B(); try { c1.rey(); throw new Loferror(); } catch (Loferror e) { System.out.println(e); } catch (ArithmeticException e) { System.out.println(e); } catch (RuntimeException e) { System.out.println(e); } catch (Exception e) { System.out.println(e); } catch (Throwable e) { System.out.println(e); } finally { System.out.println("final"); } } } 2) 创建一个Frame类,完成一个计算机的功能。 package pack; import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; public class Calculator implements ActionListener{ JFrame frame; JPanel panel; JTextField tfShow;/*定义显示文本框*/ JButton b1[]=new JButton[10]; /*数字按钮*/ JButton b2[]=new JButton[6]; /*操作按钮*/ boolean isNumber;/*判断是否输入多位数字的变量*/ double number;/*存储输入数值、显示结果的变量*/ double result;/*存储中间运算结果的变量*/ char operator;/*存储当前操作符的成员变量*/ public Calculator(){ frame=new JFrame("计算器"); frame.setSize(300,300);/*指定框架窗口的大小*/ frame.setResizable(false);/*使框架窗口不可改变大小*/ JPanel contentPane=(JPanel)frame.getContentPane(); contentPane.setBorder(new EmptyBorder(20,20,20,20));/*绘制框架的指定大小的空透明边框*/ tfShow=new JTextField("0",25);/*指定属性的文本域*/ tfShow.setHorizontalAlignment(JTextField.RIGHT);/*设置文本域中文本的对齐方式*/ isNumber=true;/*初始值设置*/ number=0;/*初始值设置*/ result=0;/*初始值设置*/ operator=' ';/*初始值设置*/ for(int i=0;i<b1.length;i++){ b1[i]=new JButton(Integer.toString(i));/*创建数字按钮*/ b1[i].setActionCommand(Integer.toString(i)); b1[i].addActionListener(this); b1[i].setForeground(Color.blue); } String bs[]={"/","*","-","C","+","="}; for(int i=0;i<b2.length;i++){ b2[i]=new JButton(bs[i]);/*创建操作按钮*/ b2[i].setActionCommand(bs[i]); b2[i].addActionListener(this); b2[i].setForeground(Color.red); } panel=new JPanel(); panel.setLayout(new GridLayout(4,5)); panel.add(b1[1]); panel.add(b1[2]); panel.add(b1[3]); panel.add(b2[0]); panel.add(b1[4]); panel.add(b1[5]); panel.add(b1[6]); panel.add(b2[1]); panel.add(b1[7]); panel.add(b1[8]); panel.add(b1[9]); panel.add(b2[2]); panel.add(b1[0]); panel.add(b2[3]); panel.add(b2[4]); panel.add(b2[5]); frame.add(tfShow,BorderLayout.NORTH);/*将文本框放置在框架上方*/ frame.add(panel,BorderLayout.CENTER);/*将装有按钮组的panel放在框架的中心*/ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/*设置框架窗口的默认窗口关闭操作*/ frame.setVisible(true);/*设置框架可见*/ } public double getDisplay(){/*返回要显示的结果*/ return number; } public void reDisplay(){/*刷新文本域的内容*/ tfShow.setText(""+getDisplay()); } /*对输入数字的处理*/ public void numberProcess(int num){ if(isNumber&&num!=0){ String s1=Integer.toString(num); String s2=Integer.toString((int)(this.number)); this.number=Double.parseDouble(s2+s1);/*对多位数字的处理*/ }else{ this.number=num; } isNumber=true;/*输入连续数字(即多位数字)时为真*/ } public void operationProcess(char operator){/*根据输入的操作符改变当前操作符*/ switch(operator){ case '-': this.operator='-'; break; case '+': this.operator='+'; break; case '*': this.operator='*'; break; case '/': this.operator='/'; break; } result=number; isNumber=false;/*输入操作符时表示输入连续数字的标记变量为假*/ } public void clear(){ number=0; result=0; } public void equal(){/*计算运算结果*/ switch(operator){ case '-': result=result-number; break; case '+': result=result+number; break; case '*': result=result*number; break; case '/': result=result/number; break; case ' ': result=number; break; } number=result; /*把运算结果赋值给显示变量*/ isNumber=false; operator=' '; } public static void main(String args[]){ Calculator cal=new Calculator();/*创建计算器*/ } public void actionPerformed(ActionEvent e){ String command=e.getActionCommand();/*获取按钮激发的操作事件的命令名称*/ char c=command.charAt(0);/*将按钮命令名称的第一个字符赋值给一个字符c*/ switch(c){ case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': int number=Integer.parseInt(command); numberProcess(number);/*输入数字的处理*/ break; case '+': case '-': case '*': case '/': operationProcess(c);/*算数运算符的处理*/ break; case '=':equal();break;/*计算运算结果*/ case 'C':clear();break;/*清零*/ } reDisplay(); /*在文本域中显示信息*/ } } 3) 编写一个名为Outer的类,它包含一个名为Inter的类。在Outer中添加一个方法,它返回一个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[] args) { Outer1 o = new Outer1(); Inner i = o.makeInner(); } } 4)请编写一个Applet程序,该程序包含一个TextField对象,和一个TextArea对象,当用户在TextField对象中输入或修改文本时候,TextArea对象的文本区域中可以得到一个同步的拷贝。 import java.applet.*; import java.awt.*; import java.awt.event.*; public class myApplet extends Applet implements ActionListener ,TextListener { TextField tf; TextArea ta; public void init() { tf = new TextField(45); ta = new TextArea (5,45); this.add(tf); this.add(ta); tf.addActionListener(this); tf.addTextListener(this); } // 单行文本(TextEvent)改变引发的事件 public void textValueChanged(TextEvent e) { if(e.getSource()==tf) ta.setText(((TextField)e.getSource()).getText()); } //在单行文本按回车引发的时间 public void actionPerformed(ActionEvent e) { if(e.getSource()== tf) { ta.setText(""); //回车,文本域清空 } } } 5)写一个方法,使用Iterator遍历Collection,并打印容器中每个对象的toString(),填充各种类型的Collection,然后对其使用此方法。 import java.util.*; import static org.greggordon.tools.Print.*; public class Ex11 { public static void printAny(Collection c) { Iterator it = c.iterator(); while(it.hasNext()) print(it.next() + " "); println(); } public static void main(String[] args) { ArrayList<Integer> al = new ArrayList<Integer>(Arrays.asList(1, 2, 3)); LinkedList<Character> ll = new LinkedList<Character>(Arrays.asList('a', 'b', 'c')); HashSet<Float> hs = new HashSet<Float>(Arrays.asList(1.1f, 2.2f, 3.3f)); TreeSet<Double> ts = new TreeSet<Double>(Arrays.asList(1.11, 2.22, 3.33)); LinkedHashSet<Integer> lhs = new LinkedHashSet<Integer>(Arrays.asList(11, 22, 33)); printAny(al); printAny(ll); printAny(hs); printAny(ts); printAny(lhs); } } 6)编写应用程序,它包含一个文本域和三个按钮,单击每个按钮的时候,在文本域中显示不同的文字。 import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JTextField; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Test extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L; private JPanel jPanel; private JButton jButton1,jButton2,jButton3; private JTextField tf1; public Test(String title) { super(title); init(); } private void init() { jPanel=new JPanel(); jPanel.setLayout(new FlowLayout()); tf1=new JTextField(20); jButton1=new JButton("按钮1"); jButton2=new JButton("按钮2"); jButton3=new JButton("按钮3"); jButton1.addActionListener(this); jButton2.addActionListener(this); jButton3.addActionListener(this); jPanel.add(tf1); jPanel.add(jButton1); jPanel.add(jButton2); jPanel.add(jButton3); this.add(jPanel); this.setSize(300,100); this.setResizable(false); this.setVisible(true); this.addWindowListener(new WindowAdapter() { public void windowClosing(final WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent e) { if(e.getSource().equals(jButton1)){ tf1.setText("按钮1"); } if(e.getSource().equals(jButton2)){ tf1.setText("按钮2"); } if(e.getSource().equals(jButton3)){ tf1.setText("按钮3"); } } public static void main(String[] args){ new Test("文字变换"); } } 请读懂以下程序并给出运行结果:(18分) //程序1 public class Guess { public static void main(String[] args) { int a,b; for(a=14;a<=90;a++) for(b=11;b<50;b++) { if(((a-10)==(b-10)*4)&&((a+10)%(b+10)==0)) System.out.println("ag "+a+" daf"+b); } } } //程序2 class 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);} } class Override { public static void main(String args[]) { OverrideDemo ob = new OverrideDemo(); ob.test(); ob.test(10); ob.test(10,20); ob.test(123.25); } } //程序3 import java.awt.*; import java.awt.event.*; import java.applet.*; public class ButtonApplet extends Applet implements ActionListener{ int i1,i2; Button 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("你点击了确定"+ i2+"次",20,120); } public void actionPerformed(ActionEvent e){ if(e.getSource()==btn1){ i1++; } if(e.getSource()==btn2){ i2++; } repaint(); } } //程序4 interface Weapon { String name = "weapon"; void attack(); } class Tank implements Weapon { public void attack() { System.out.println("Tank attack"); } } class Cannon implements 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(); } } //程序5 import java.awt.*; import javax.swing.*; import java.util.Date; public class DrawLines extends JFrame { public void paint(Graphics g){ int x1,y1,x2,y2; for(int i=0;i<100;i++){ x1=(int)(Math.random()*400); x2=(int)(Math.random()*400); y1=(int)(Math.random()*400); y2=(int)(Math.random()*400); g.setColor(new Color((int)(Math.random()*256), (int)(Math.random()*256),(int)(Math.random()*256))); g.drawLine(x1, y1, x2, y2); } } public static void main(String[] args) { DrawLines window=new DrawLines(); window.setSize(400, 400); window.setVisible(true); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // window.repaint(); } } //程序6: class Person1{ protected String name; protected double classsum; public Person1(){ this.name=""; } public Person1(String name){ this.name=name; } } class Teacher1 extends Person1 implements PersonPay{ private final int baseWage=800; private double classsum; public Teacher1(String name,double classsum){ super(name); this.classsum=classsum; } public double pay(){ return baseWage+classsum*30; } public String toString(){ return "姓名:"+this.name+"\t 工资支出:"+this.pay()+"\n"; } } class CollegeStudent1 extends Person1 implements PersonPay{ private double scholarship; public CollegeStudent1(String name,double scholarship){ super(name); this.scholarship=scholarship; } public double pay(){ return scholarship; } public String toString(){ return "姓名:"+this.name+"\t工资支出:"+this.pay()+"\n"; } } public class WageTest1 { public static void main(String[] args) { Person1 ps[]=new Person1[2]; ps[0]=new Teacher1("张三",120); ps[1]=new CollegeStudent1("李四",500); String output=""; for(int i=0;i<ps.length;i++) output+=ps[i]; System.out.println(output); } } //程序7 public class ko { public static void main(String args[]) { int t, z=10; t=sum(z); System.out.println("sum="+t); } static int sum(int x) { if(x==1) return(1); else return(sum(x-1)+x)
展开阅读全文

开通  VIP、SVIP  下载更划算
下载10份以上建议开通 VIP 会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 考试专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        关注我们

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服