//Shoot射击游戏第二天: 给6个对象类添加构造方法,并测试
/**
*在讲第二天代码前先给大家普及一下知识
*什么是一个好的代码?
*
* 高质量的代码:-----------最终的目标
* 复用性好、扩展性好、维护性好、可移植性好、
* 健壮性好、可读性好、效率好......
*所以我们的代码要像这方面看齐,不知道大家有没有发现昨天的代码重复的地方很多大量的代码,这样的代码称为垃圾代码
*我们工作的时候也不要写这样的垃圾代码所以我们用到一个新的知识点--构造方法
*/
在讲构造方法之前先简单介绍一下概念问题这些刚开始看不懂没有关系还是那句话,多敲,敲得多了就明白为什么这样写了,java是一个往后走的学习过程往后走的长了前面的就会了,刚开始不要钻牛角尖.
1方法的签名:方法名+参数列表
Student(String name,int age,String address){
}
这里Student就是方法名 String name,int age,String address参数列表
然后{}里面的就是 语句块
2.方法的重载(Overload):
1)发生在同一类中,方法名称相同,参数列表不同,方法体不同
2)编译器在编译时会根据方法的签名自动绑定方法
3.构造方法:----------------代码复用
1)给成员变量赋初值
2)与类同名,没有返回值类型(连void没有)
3)在创建(new)对象时被自动调用
4)若自己不写构造方法,则编译器默认提供一个无参的构造方法,
若自己写了构造方法,则不再默认提供
5)构造方法可以重载
4.this:指代当前对象,哪个对象调用方法它指的就是哪个对象
只能用在方法中,方法中访问成员变量之前默认有个this.
this的用法:
1)this.成员变量名-----------访问成员变量
2)this.方法名()-------------调用方法(一般不用)
3)this()--------------------调用构造方法(应用率低)
java规定:
成员变量和局部变量是可以同名的
----使用的时候默认采取就近原则
----此时若想访问成员变量,则this不能省略
当成员变量与局部变量同名时
----若想访问成员变量,则this不能省略
class Student {
String name; //成员变量(整个类中好使)
int age;
String address;
Student(String name,int age,String address){ //局部变量(当前方法中好使)
this.name = name;
this.age = age;
this.address = address;
}
void study() {
System.out.println(name+"在学习...");
}
void sayHi() {
System.out.println("大家好,我叫"+this.name+",今年"+this.age+"岁了,家住"+this.address);
}
}
5.null:表示空,没有指向任何对象
若引用的值为null,则该引用不能再进行任何点操作了
若点操作则发生NullPointerException空指针异常
引用类型是可以赋值为null的
引用类型的默认值为就是null
Student zs = null; //正确
int a = null; //编译错误
//这里为什么错了?是因为他不是引用类型(要解释明白就要用到内存管理的知识)
除了八大基本类型以外的都是引用类型!不知道哪八大可以看我置顶的语言基础知识点
内存管理:由JVM来管理的
1)堆:new出来的对象(包括成员变量)
2)栈:局部变量(包括方法的参数)
3)方法区:-----以后在说
方法外-------------成员变量
方法中-------------局部变量
基本类型变量中-----装具体的值
引用类型变量中-----装地址
附图:
(有点啰嗦大家看不懂没有关系主要还是敲,敲着就有了感觉下面上代码)
package cn.tedu.shoot;
import java.util.Random;
public class Airplane {
int x;
int y;
int width;
int height;
int speed;
Airplane(){
width=48;
height=50;
Random rand=new Random();
x= rand.nextInt(400-width);
y=-height;
speed=2;
}
void step(){
System.out.println("小敌机");
}
}
package cn.tedu.shoot;
import java.util.Random;
public class Bee {
int width;
int height;
int xspeed;
int x;
int y;
int awardType;
int yspeed;
Bee(){
width=60;
height=51;
xspeed=1;
yspeed=1;
Random rand=new Random();
x=rand.nextInt(400-width);
awardType=rand.nextInt(2);
y=-height;
}
void step(){
System.out.println("小蜜蜂");
}
}
package cn.tedu.shoot;
import java.util.Random;
public class BigAirplane {
int x;
int y;
int speed;
int width;
int height;
BigAirplane(){
Random rand=new Random();
x=rand.nextInt(400-height);
y=-height;
speed=2;
width=66;
height=89;
}
void step(){
System.out.println("大敌机");
}
}
package cn.tedu.shoot;
public class Bullet {
int x;
int y;
int speed;
int width;
int height;
Bullet(int x,int y){
width=8;
this.x=x;
this.y=y;
height=20;
speed=3;
}
void step(){
System.out.println("子弹来了");
}
}
package cn.tedu.shoot;
public class Hero {
int height;
int width;
int x;
int y;
int life;
int fire;
Hero(){
height=139;
width=97;
x=140;
y=100;
life=3;
fire=0;
}void step(){
System.out.println("英雄机出动");
}
}
package cn.tedu.shoot;
public class Sky {
int x;
int y;
int y1;
int width;
int height;
int speed;
Sky(){
x=400;
y=700;
y1=-700;
speed=1;
width=400;
height=700;
}
void step(){
System.out.println("天空");
}
}
package cn.tedu.shoot;
import java.util.Random;
public class World {
Hero h;
Sky s;
Airplane a1;
Airplane a2;
Airplane a3;
Airplane a4;
Bee b1;
Bee b2;
Bullet bt1;
Bullet bt2;
BigAirplane Bi1;
BigAirplane Bi2;
void action(){
h=new Hero();
s=new Sky();
a1=new Airplane();
a2=new Airplane();
a3=new Airplane();
a4=new Airplane();
b1=new Bee();
b2=new Bee();
bt1=new Bullet(200,20);
bt2=new Bullet(65,21);
Bi1=new BigAirplane();
Bi2=new BigAirplane();
b1.step();
System.out.println(h.width+","+h.height+","+h.x+","+h.y+","+h.life+","+h.fire);
System.out.println(s.width+","+s.height+","+s.x+","+s.y+","+s.y1+","+s.speed);
System.out.println(a1.width+","+a1.height+","+a1.x+","+a1.y+","+a1.speed);
System.out.println(a2.width+","+a2.height+","+a2.x+","+a2.y+","+a2.speed);
System.out.println(a3.width+","+a3.height+","+a3.x+","+a3.y+","+a3.speed);
System.out.println(a4.width+","+a4.height+","+a4.x+","+a4.y+","+a4.speed);
System.out.println(b1.width+","+b1.height+","+b1.x+","+b1.y+","+b1.xspeed+","+b1.awardType+","+b1.yspeed);
System.out.println(b2.width+","+b2.height+","+b2.x+","+b2.y+","+b2.xspeed+","+b2.awardType+","+b2.yspeed);
System.out.println(bt1.width+","+bt1.height+","+bt1.x+","+bt1.y+","+bt1.speed);
System.out.println(bt2.width+","+bt2.height+","+bt2.x+","+bt2.y+","+bt2.speed);
System.out.println(Bi1.width+","+Bi1.height+","+Bi1.x+","+Bi1.y+","+Bi1.speed);
System.out.println(Bi2.width+","+Bi2.height+","+Bi2.x+","+Bi2.y+","+Bi2.speed);
}
public static void main(String[] args) {
World x=new World();
x.action();
}
}
(这次代码的目的是为了给各种对象赋值代码有大量的重复部分大家不想就复制,但是老规矩要写至少5遍,明白怎么运行的)
为什么要写构造方法?
在这个代码中还没有完美的体验出来,每个类中必须有构造方法,写了有参的就调用有参的,不写就默认给你一个无参的,写构造方法的作用就是代码的复用.