总结

try{
//...
//..
//...


}catch(Exception e){
    e.printStackTrace();
    throw new RuntimeException();
}


对文件操作,是很容易出现异常的---
所以java必须做异常处理


try{
BufferedImage img= ImageIO.read(FlyingObject.class.getResource(filename));//这种方法要求图片和当前的类再同包中
return img;

}catch(Exception e){
    e.printStackTrace();
    throw new RuntimeException();
}
 
将所有图片放到包里


sky extends FlyingObject
static{
image = 调超类中的方法(子类给参数,传给父类,父类调方法返回image给子类)----loadImage("background.png");

}
----------------------------------
子弹类


Bullet extends FlyingObject
static{
image = loadImage("bullet.png");

}
--------------------------------
英雄机
Hero extends FlyingObject
private static BufferedImage[] images;
static{
images =new BufferedImage[6];------------6张图
接下来是给image[0]到image[5]赋值 
//images[0] = loadImage("hero0.png");
//images[1] = loadImage("hero1.png"); 
//images[2] = loadImage("hero2.png");
//images[3] = loadImage("hero3.png"); 
//images[4] = loadImage("hero4.png");
//images[5] = loadImage("hero5.png");
代码重复么---重复
固定次数么---固定----用for
for(int i =0;i<images.length;i++){
images[i]= loadImage("hero"+i+".png");
}
}
----------------------------------
小敌机
Airplane extends FlyingObject
private static BufferedImage[] images;
static{
images=new BufferedImage[6];------------6张图
接下来是给image[0]到image[5]赋值 
//images[0] = loadImage("hero0.png");
//images[1] = loadImage("hero1.png"); 
//images[2] = loadImage("hero2.png");
//images[3] = loadImage("hero3.png");
//images[4] = loadImage("hero4.png");
//images[5] = loadImage("hero5.png");
for(int i =0;i<images.length;i++){
images[i]= loadImage("airplane"+i+".png");//把图片名字改了
}
}
---------------------------------------------
 BigAirplane 
 BigAirplane  extends FlyingObject
private static BufferedImage[] images;
static{
images=new BufferedImage[5];-----------5张图
接下来是给image[0]到image[5]赋值 
//images[0] = loadImage("hero0.png");
//images[1] = loadImage("hero1.png"); 
//images[2] = loadImage("hero2.png");
//images[3] = loadImage("hero3.png");
//images[4] = loadImage("hero4.png");
//images[5] = loadImage("hero5.png");
for(int i =0;i<images.length;i++){
images[i]= loadImage("bigplane"+i+".png");//把图片名字改了
}
}
---------------------------------------------
小蜜蜂
 Bee  extends FlyingObject
private static BufferedImage[] images;
static{
images=new BufferedImage[5];-----------5张图
接下来是给image[0]到image[5]赋值 
//images[0] = loadImage("hero0.png");
//images[1] = loadImage("hero1.png"); 
//images[2] = loadImage("hero2.png");
//images[3] = loadImage("hero3.png");
//images[4] = loadImage("hero4.png");
//images[5] = loadImage("hero5.png");
for(int i =0;i<images.length;i++){
images[i]= loadImage("bee"+i+".png");//把图片名字改了
}
}
----------------------------------------------------------------
Shoot射击游戏第一天:
1)设计Hero、Sky、Airplane、BigAirplane、Bee、Bullet对象类
2)设计World世界类,action()中: 创建对象并测试


Shoot射击游戏第二天:
1)设计Hero、Sky、Airplane、BigAirplane、Bee、Bullet的构造方法
2)在World类的action()中: 创建对象并测试


Shoot射击游戏第三天:
1)设计Airplane数组、BigAirplane数组、Bee数组、Bullet数组
2)创建FlyingObject超类,重构6个对象类(继承)
3)在超类FlyingObject中设计两个构造方法--------代码复用
4)在World类的action()中: 创建对象并测试


Shoot射击游戏第四天:
1)将Airplane数组、BigAirplane数组、Bee数组装到一个FlyingObject数组中
2)重写超类FlyingObject中的step()方法
3)画窗口
4)在World类的action()中: 创建对象并测试


Shoot射击游戏第五天:
1)将所有类的成员添加访问控制修饰符
2)设计对象的图片属性
3)在World类的action()中: 创建对象并测试






回顾:
1.向上造型:
  1)超类型的引用指向派生类的对象
  2)能点出来什么,看引用的类型
2.方法的重写(Override):
  1)发生在父子类中,方法名相同,参数列表相同,方法体不同
  2)重写方法被调用时,看对象的类型
3.重写与重载的区别:
  1)重写:发生在父子类中,方法名相同,参数列表相同,方法体不同
         "运行期"绑定,看对象
  2)重载:发生在一个类中,方法名相同,参数列表不同,方法体不同
         "编译期"绑定,看参数(引用)










笔记:
1.package:
  1)作用:避免类名的冲突
  2)包名常常是有层次结构的
  3)类的全称: 包名.类名
  4)建议:包名所有字母都小写
  import:
  1)同包中的类可以直接访问,
    不同包中的类不能直接访问,想访问只有如下两种方式:
1.1)先import声明类再访问类------建议
1.2)类的全称---------------------太繁琐,不建议
2.访问控制修饰符:
  1)public:公共的,任何类
  2)private:私有的,本类
  3)protected:受保护的,本类、子类、同包类
  4)默认的:什么也不写,本类、同包类
  类的访问控制修饰符,只能是public和默认的
  类中成员的访问控制修饰符,如上4种都可以
3.final:最终的、不可改变的---------应用率低
  1)修饰变量:变量不能被改变
  2)修饰方法:方法不能被重写
  3)修饰类:类不能被继承
4.static:静态的
  1)静态变量:
    1.1)由static修饰
1.2)属于类的,存在方法区中,只有一份
1.3)常常通过类名点来访问
1.4)何时用:所有对象所共享的数据(图片、音频、视频等)
  2)静态方法:
    2.1)由static修饰
2.2)属于类的,存在方法区中,只有一份
2.3)常常通过类名点来访问
2.4)静态方法没有隐式this传递,
   所以在静态方法中不能直接访问实例成员
2.5)何时用:方法的操作仅与参数相关而与对象无关
  3)静态块:
    3.1)由static修饰
3.2)属于类的,在类被加载期间自动执行
   因类只被加载一次,所以静态块也只执行一次
3.3)何时用:加载/初始化静态资源(图片、音频、视频等)


5.static final常量:明天讲


静态变量: 对象的图片属性
静态方法: 读取图片行为
静态块: 给对象的图片属性赋值
-------------------------------------
API-------------sun公司写好的,可供直接调用的功能(在Java语言中,这些功能以类的形式封装)
JDK API包括的类库功能强大,经常使用的有:字符串操作,集合操作,文件操作,输入输出操作,网络操作,多线程等等
Application Programming Interface
API也叫系统库   ----jar封装许多jar文件
jar文件 有很多包
文档注释             /**
value是对象的属性
开始引用 ABC对象的地址
字符串对象没有改变
字面量--------------直接写死的 ---------“ABC”
String ----字符串对象
也有个value对象
常量池存的是引用
字符串连接结果不参与优化
java编译期能算的就算了
String s7=“A”+"B"+"C"--------------------"字面量"的连接结果
          String s4="A" String s5 = "BC"  String s6=s4+s5;          字符串变量连接结果不参与常量池优化


提高字符串的性能!!!节省内存耗用
//经典面试题目
------------------------------------------
字符串的每个字符16位--2个字节 采用Unicode;
String类型提供了Length()方法
返回字符串字符的个数
String s1="Hello 世界!";
//              0123456789
int l = s1.length();
System.out.println(l);//9
凡是.都可以理解为 的
数组长度是属性,字符串长度是方法
byte[] bytes = s1.getBytes();
System.out.println(bytes.length);
----------------------------------------getBytes()对字符进行编码,并且返回编码以后的结果,不同编码返回字节数两不同
getByte方法返回在字节长度不是内存中实际占用的字节长度
char c = s1.charAt(6);//按照位置获取字符------------返回 世
判断
int n = name.indexOf("王");-------------“”
int n = name.indexOf('王');--------------“欧阳”
liucs@tedu.cn












StringBulider buf = new StringBuilder();
buf.length()-----检查有效字符的个数
buf.capacity()------------2倍加2不懂
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public abstract class FlyingObject { public static final int LIFE = 0; //活着呢 public static final int DEAD = 1; //死了的(先爆破) public static final int REMOVE = 2; //删除了(爆破后删除) protected int state = LIFE; //当前状态(默认活着) protected int width; //宽 protected int height; //高 protected int x; //x坐标 protected int y; //y坐标 /** 无参构造方法 */ public FlyingObject(){ } /**专门给小敌机、大敌机、小蜜蜂提供的构造方法 */ public FlyingObject(int width,int height){ this.width = width; this.height = height; Random rand = new Random(); x = rand.nextInt(World.WIDTH-width); //x:0到(窗口-小敌机的宽)之间的随机数 y = -height; //y:负的小敌机的高 } /** 专门给英雄机、子弹、天空提供的构造方法 */ public FlyingObject(int width,int height,int x,int y){ this.width = width; this.height = height; this.x = x; this.y = y; } /** 读取图片 */ public static BufferedImage loadImage(String fileName){ try{ BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName)); return img; }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(); } } /** 飞行物移动了 */ public abstract void step(); public abstract BufferedImage getImage(); /** 判断是否活着 */ public boolean isLife(){ return state==LIFE; } /** 判断是否死了的 */ public boolean isDead(){ return state==DEAD; } /** 判断是否删除的 */ public boolean isRemove(){ return state==REMOVE; } /** 画对象 g:画笔 */ public void paintObject(Graphics g){ g.drawImage(getImage(), x, y, null); } /** 检测飞行物是否越界 */ public abstract boolean outOfBounds(); /** 敌人与子弹/英雄机的碰撞 this:敌人 other:子弹或英雄机 */ public boolean hit(FlyingObject other){ int x1 = this.x-other.width; //x1:敌人的x int x2 = this.x+this.width; //x2:敌人的x int y1 = this.y-other.height; //y1:敌人的y int y2 = this.y+this.height; //y2:敌人的y int x = other.x; //x:子弹的x int y = other.y; //y:子弹的y return x>=x1 && x=y1 && y<=y2; } public void goDead(){ state = DEAD; //修改当前状态为死了的 } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值