Java知识重点梳理_第1页
Java知识重点梳理_第2页
Java知识重点梳理_第3页
Java知识重点梳理_第4页
Java知识重点梳理_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

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

文档简介

Newnew是在堆中为对象men申请了一块空间。其中new实际是在调用父类的构造方法。注释 单行:/多行:/* */ /* */变量与常量一般常量(成员常量) final int x类常量 static int x修饰符关键字final(最终的)修饰的变量表示常量,赋值后无法改变修饰父类中Private成员方法,默认是final类型,表示不能被子类覆盖。Static (全局、静态)被static修饰的成员变量和成员方法独立于该类的任何对象。也就是说,它不依赖类特定的实例,被类的所有实例共享。static对象可以在它的任何对象创建之前访问,无需引用任何对象。private是访问权限限定,static表示不要实例化就可以使用三大特性:继承,多态,封装。继承:继承是为了重用父类代码,同时为实现多态性作准备。能够大大缩短开发周期,降低开发费用。封装:类使得数据和对数据的操作集成在一起,从而对使用该类的其他人来说,可以不管它的实现方法,而只管用它的功能,从而实现所谓的信息隐藏。多态:方法的重写、重载与动态连接构成多态性。因为单继承的限制,(猫 extends 动物),造成功能上的限制,所以引入多态。重载、重写重载:多个同名函数同时存在,具有不同的参数个数/类型。(型构不同)重写(覆盖):重写方法只能存在于具有继承关系中,重写方法只能重写父类非私有的方法。当子类继承自父类的相同方法,输入数据一样,但要做出有别于父类的响应时,你就要覆盖父类方法,即在子类中重写该方法相同型构,不同实现。最终目的:设计一个结构清晰而简洁的类接口与抽象类abstract class和interface是Java语言中对于抽象类定义进行支持的两种机制。对比:抽象类表示该类中可能已经有一些方法的具体定义,但是接口就仅仅只能定义各个方法的界面(方法名,参数列表,返回类型),并不关心具体细节。从某种意义上说,interface是一种特殊形式的abstract class。类是现实中实际存在的事物。抽象类:实际不存在的抽象概念,即一些性质的集合,比如Object、形状、家、生物、神仙等等抽象类里面可以有非抽象方法但接口里只能有抽象方法 声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用于要创建一个体现某些基本行为的类,并为该类声明方法,但不能在该类中实现该类的情况。不能创建abstract 类的实例。接口(interface)是抽象类的变体。在接口中,所有方法都是抽象的。多继承性可通过实现这样的接口而获得。Java Swing一个用户登录框实例SwingLoginExample.java 文件代码如下:import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField; public class SwingLoginExample public static void main(String args) / 创建 JFrame 实例 JFrame frame = new JFrame(Login Example); / Setting the width and height of frame frame.setSize(350, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /* 创建面板,这个类似于 HTML 的 div 标签 * 我们可以创建多个面板并在 JFrame 中指定位置 * 面板中我们可以添加文本字段,按钮及其他组件。 */ JPanel panel = new JPanel(); / 添加面板 frame.add(panel); /* * 调用用户定义的方法并添加组件到面板 */ placeComponents(panel); / 设置界面可见 frame.setVisible(true); private static void placeComponents(JPanel panel) /* 布局部分我们这边不多做介绍 * 这边设置布局为 null */ panel.setLayout(null); / 创建 JLabel JLabel userLabel = new JLabel(User:); /* 这个方法定义了组件的位置。 * setBounds(x, y, width, height) * x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。 */ userLabel.setBounds(10,20,80,25); panel.add(userLabel); /* * 创建文本域用于用户输入 */ JTextField userText = new JTextField(20); userText.setBounds(100,20,165,25); panel.add(userText); / 输入密码的文本域 JLabel passwordLabel = new JLabel(Password:); passwordLabel.setBounds(10,50,80,25); panel.add(passwordLabel); /* *这个类似用于输入的文本域 * 但是输入的信息会以点号代替,用于包含密码的安全性 */ JPasswordField passwordText = new JPasswordField(20); passwordText.setBounds(100,50,165,25); panel.add(passwordText); / 创建登录按钮 JButton loginButton = new JButton(login); loginButton.setBounds(10, 80, 80, 25); panel.add(loginButton); 执行以下命令输出结果:$ javac SwingLoginExample.java$ java SwingLoginExample概念解析:JFrame java的GUI程序的基本思路是以JFrame为基础,它是屏幕上window的对象,能够最大化、最小化、关闭。JPanel Java图形用户界面(GUI)工具包swing中的面板容器类,包含在javax.swing 包中,可以进行嵌套,功能是对窗体中具有相同逻辑功能的组件进行组合,是一种轻量级容器,可以加入到JFrame窗体中。JLabel JLabel 对象可以显示文本、图像或同时显示二者。可以通过设置垂直和水平对齐方式,指定标签显示区中标签内容在何处对齐。默认情况下,标签在其显示区内垂直居中对齐。默认情况下,只显示文本的标签是开始边对齐;而只显示图像的标签则水平居中对齐。JTextField一个轻量级组件,它允许编辑单行文本。JPasswordField 允许我们输入了一行字像输入框,但隐藏星号(*) 或点创建密码(密码)JButton JButton 类的实例。用于创建按钮类似实例中的 Login。事件监听* * Java事件监听处理自身类实现ActionListener接口,作为事件监听器 * * author codebrother */class EventListener1 extends JFrame implements ActionListener private JButton btBlue, btDialog; public EventListener1() setTitle(Java GUI 事件监听处理); setBounds(100, 100, 500, 350); setLayout(new FlowLayout(); btBlue = new JButton(蓝色); btDialog = new JButton(弹窗); / 将按钮添加事件监听器 btBlue.addActionListener(this); btDialog.addActionListener(this); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); / *事件处理* Override public void actionPerformed(ActionEvent e) if (e.getSource() = btBlue) Container c = getContentPane(); c.setBackground(Color.BLUE); else if (e.getSource() = btDialog) JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); /* * Java事件监听处理内部类处理 * * author codebrother */class EventListener3 extends JFrame private JButton btBlue, btDialog; / 构造方法 public EventListener3() setTitle(Java GUI 事件监听处理); setBounds(100, 100, 500, 350); setLayout(new FlowLayout(); btBlue = new JButton(蓝色); btDialog = new JButton(弹窗); / 添加事件监听器对象(面向对象思想) btBlue.addActionListener(new ColorEventListener(); btDialog.addActionListener(new DialogEventListener(); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); / 内部类ColorEventListener,实现ActionListener接口 class ColorEventListener implements ActionListener Override public void actionPerformed(ActionEvent e) Container c = getContentPane(); c.setBackground(Color.BLUE); / 内部类DialogEventListener,实现ActionListener接口 class DialogEventListener implements ActionListener Override public void actionPerformed(ActionEvent e) JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); /* * Java事件监听处理匿名内部类处理 * * author codebrother */class EventListener2 extends JFrame private JButton btBlue, btDialog; public EventListener2() setTitle(Java GUI 事件监听处理); setBounds(100, 100, 500, 350); setLayout(new FlowLayout(); btBlue = new JButton(蓝色); btDialog = new JButton(弹窗); / 添加事件监听器(此处即为匿名类) btBlue.addActionListener(new ActionListener() / 事件处理 Override public void actionPerformed(ActionEvent e) Container c = getContentPane(); c.setBackground(Color.BLUE); ); / 并添加事件监听器 btDialog.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); ); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /* * Java事件监听处理外部类处理 * * author codebrother */class EventListener4 extends JFrame private JButton btBlue, btDialog; public EventListener4() setTitle(Java GUI 事件监听处理); setBounds(100, 100, 500, 350); setLayout(new FlowLayout(); btBlue = new JButton(蓝色); btDialog = new JButton(弹窗); / 将按钮添加事件监听器 btBlue.addActionListener(new ColorEventListener(this); btDialog.addActionListener(new DialogEventListener(); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); / 外部类ColorEventListener,实现ActionListener接口class ColorEventListener implements ActionListener private EventListener4 el; ColorEventListener(EventListener4 el) this.el = el; Override public void actionPerformed(ActionEvent e) Container c = el.getContentPane(); c.setBackground(Color.BLUE); / 外部类DialogEventListener,实现ActionListener接口class DialogEventListener implements ActionListener Override public void actionPerformed(ActionEvent e) JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); public class ActionListenerTest public static void main(String args) new EventListener2(); 游戏实战:坦克大战设计思路必须提前理清:Blood.javapackage com.hkm.TankWar;import java.awt.*;/* * 血块类,我方坦克吃了可回血; * author Hekangmin * */public class Blood private int x,y,w,h;/血块的位置和宽度高度; private TankWarClient tc; private int step=0;/纪录血块移动的步数; private boolean live=true; public boolean isLive() return live; public void setLive(boolean live) this.live = live; /* * 纪录血块的位置; */ private int pos=400,300,400,320,420,320,440,300,440,330,480,400,520,400,540,400; public Blood() x=pos00; y=pos01; w=h=18; public void draw(Graphics g) if(!live) return; Color c=g.getColor(); g.setColor(Color.CYAN); g.fillOval(x, y, w, h); g.setColor(c); move(); /* * 移动血块 */ public void move() step+; if(step=pos.length) step=0; else x=posstep0; y=posstep1; public Rectangle getRect() return new Rectangle(x,y,w,h); Explode.javapackage com.hkm.TankWar;import java.awt.*;/* * 爆炸类 * author Hekangmin * */public class Explode private int x,y;/爆炸发生的位置 private boolean Live=true; int dia=4,8,12,16,32,40,20,14,4;/用园模拟,代表圆的直径; int step=0;/区别移到第几个直径 private TankWarClient tc;/持有引用 public Explode(int x,int y,TankWarClient tc) this.x=x; this.y=y; this.tc=tc; public void draw(Graphics g) if(!Live) tc.explodes.remove(this); return; if(step=dia.length)/如果到了最后一个直径爆炸死亡; Live=false; step=0; return; Color c=g.getColor(); g.setColor(Color.YELLOW); g.fillOval(x, y, diastep, diastep); g.setColor(c); step+; Tank.javapackage com.hkm.TankWar;import java.awt.*;import java.awt.event.KeyEvent;import java.util.*;/* * 坦克类 * author Hekangmin * */public class Tank public static final int XSPEED=5;/坦克x方向速度 public static final int YSPEED=5; public static final int WIDTH=30; public static final int HEIGHT=30; private BloodBar bb=new BloodBar();/血条 private int life=100; public int getLife() return life; public void setLife(int life) this.life = life; private static Random r=new Random(); private static int step=r.nextInt(12)+3;/定义一个数表示敌军坦克随机东的步数; private boolean bL=false,bU=false,bR=false,bD=false; enum DirectionL,LU,U,RU,R,RD,D,LD,STOP;/利用枚举类型定义坦克方向; private int x,y; private int oldX,oldY;/纪录上一步坦克的位置; private boolean live=true;/判断是否活着 public boolean isLive() return live; public void setLive(boolean live) this.live = live; private boolean good;/坦克是好是坏 public boolean isGood() return good; private Direction ptDir=Direction.D;/新增炮筒的方向; TankWarClient tc;/为了持有对方的引用以可以方便访问其成员变量; Direction dir=Direction.STOP;/一开始将坦克方向设为stop; public Tank(int x,int y,boolean good,Direction dir,TankWarClient tc) this.x=x; this.y=y; this.oldX=x; this.oldY=y; this.good=good; this.dir=dir; this.tc=tc;/持有对方的引用; public void draw(Graphics g) if(!live)/如果死亡则不再draw; if(!good) tc.tanks.remove(this); if(tc.tanks.size()5)/少于5辆坦克时添加坦克; for(int i=0;i10;i+) int posX=r.nextInt(800); int posY=r.nextInt(600); tc.tanks.add(new Tank(posX,posY,false,Direction.D,tc);/使得坦克出现的位置随机 return; Color c=g.getColor(); if(good) g.setColor(Color.RED); bb.draw(g); else g.setColor(Color.BLACK); g.fillOval(x, y, WIDTH, HEIGHT); g.setColor(c); switch(ptDir)/画出炮筒的方向; case L: g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x-10, y+Tank.HEIGHT/2);/画出炮筒,画一条直线代替; break; case LU: g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x-7, y-7); break; case U: g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH/2, y-10); break; case RU: g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH+7, y-7); break; case R: g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH+10, y+Tank.HEIGHT/2); break; case RD: g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH+7, y+Tank.HEIGHT+7); break; case D: g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH/2, y+Tank.HEIGHT+10); break; case LD: g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x-7, y+HEIGHT+7); break; move(); public void move() oldX=x;/纪录坦克上一步的位置 oldY=y; switch(dir) case L: x-=XSPEED; break; case LU: x-=XSPEED; y-=YSPEED; break; case U: y-=YSPEED; break; case RU: x+=XSPEED; y-=YSPEED; break; case R: x+=XSPEED; break; case RD: x+=XSPEED; y+=YSPEED; break; case D: y+=YSPEED; break; case LD: x-=XSPEED; y+=YSPEED; break; case STOP: break; if(this.dir!=Direction.STOP) this.ptDir=this.dir; /* * 防止坦克越界; */ if(x0) x=0; if(yTankWarClient.GAME_WIDTH) x=TankWarClient.GAME_WIDTH-30; if(y+Tank.HEIGHTTankWarClient.GAME_HEIGHT) y=TankWarClient.GAME_HEIGHT-30; if(!good) Direction dirs=Direction.values();/将枚举类型转化成数组; if(step=0) step=r.nextInt(12)+3; int rn=r.nextInt(dirs.length);/产生length以内随机的整数; dir=dirsrn; step-; if(r.nextInt(40)20) this.fire();/使敌军坦克发射子弹; /* * 处理按键 * param e键盘事件; */ public void KeyPressed(KeyEvent e) int key=e.getKeyCode(); switch(key) case KeyEvent.VK_LEFT: bL=true; break; case KeyEvent.VK_RIGHT: bR=true; break; case KeyEvent.VK_UP: bU=true; break; case KeyEvent.VK_DOWN: bD=true; break; locationDir(); public void keyReleased(KeyEvent e) int key=e.getKeyCode(); switch(key) case KeyEvent.VK_CONTROL: fire(); break; case KeyEvent.VK_LEFT: bL=false; break; case KeyEvent.VK_RIGHT: bR=false; break; case KeyEvent.VK_UP: bU=false; break; case KeyEvent.VK_DOWN: bD=false; break; case KeyEvent.VK_A: superFire(); break; case KeyEvent.VK_F2: reBorn(); break; locationDir(); /* * 发射子弹 * return返回子弹类型 */ public Missile fire() if(!live) return null; int mx=this.x+Tank.WIDTH/2-Missile.WIDTH/2;/计算子弹发射的位置; int my=this.y+Tank.HEIGHT/2-Missile.HEIGHT/2; Missile m=new Missile(mx,my,good,ptDir,this.tc);/根据炮筒方向发射子弹 tc.missiles.add(m); return m; public Missile fire(Direction dir) if(!live) return null; int mx=this.x+Tank.WIDTH/2-Missile.WIDTH/2; int my=this.y+Tank.HEIGHT/2-Missile.HEIGHT/2; Missile m=new Missile(mx,my,good,dir,this.tc);/根据坦克的方向发射子弹; tc.missiles.add(m); return m; public void superFire() Direction dirs=Direction.values(); for(int i=0;i8;i+) fire(dirsi); public void locationDir() if(bL&!bU&!bR&!bD) dir=Direction.L; else if(bL&bU&!bR&!bD) dir=Direction.LU; else if(!bL&bU&!bR&!bD) dir=Direction.U; else if(!bL&bU&bR&!bD) dir=Direction.RU; else if(!bL&!bU&bR&!bD) dir=Direction.R; else if(!bL&!bU&bR&bD) dir=Direction.RD; else if(!bL&!bU&!bR&bD) dir=Direction.D; else if(bL&!bU&!bR&bD) dir=Direction.LD; else if(!bL&!bU&!bR&!bD) dir=Direction.STOP; public Rectangle getRect()/获取tank的矩形区域 return new Rectangle(this.x,this.y,this.WIDTH,this.HEIGHT); /* * 坦克撞墙 * param w墙 * returntrue撞上,false未撞上; */ public boolean colliedsWithWall(Wall w) if(this.live&this.getRect().intersects(w.getRect() this.stay(); return true; return false; /* * 处理坦克与坦克相撞,防止其互相穿越; * param tanks敌军坦克; * return true撞上,false未撞上; */ public boolean colliedsWithTanks(java.util.List tanks) for(int i=0;itanks.size();i+) Tank t=tanks.get(i); if(this!=t) if(this.live&this.isLive()&this.getRect().intersects(t.getRect() this.stay();/返回上一步的位置; t.stay();/返回上一步的位置 return true; return false; private void stay() x=oldX; y=oldY; /* * 为Tank的内部类;血条,显示在我方坦克的头顶上; * author Hekang

温馨提示

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

评论

0/150

提交评论