oo.day03
Shoot射击游戏第一天:
1.创建了6个对象类,并创建World类测试
Shoot射击游戏第二天:
1.给6个对象类添加构造方法,并测试
Shoot射击游戏第三天:
1.设计小敌机、大敌机、小蜜蜂、子弹数组,并测试
2.设计FlyingObject超类,6个对象类分别继承
3.给FlyingObject超类设计两个构造方法,对象类分别调用
回顾:
1.方法的重载(Overload):
1)发生在一个类中,方法名相同,参数列表不同,方法体不同
2)编译器在编译时根据方法的签名自动绑定调用的方法
2.构造方法:
给成员变量赋初值,与类同名、没有返回值类型
创建对象时被自动调用
若自己不写则默认无参构造,自己写了则不再默认提供
可以重载
3.this:哪个对象调用方法指的就是哪个对象
this.成员变量名------------访问成员变量
this.方法名()--------------调用方法
this()---------------------调用构造方法
4.null:空,没有指向任何对象
引用的值为null,则该引用不能再进行任何操作了,
若操作则发生NullPointerException空指针异常
5.引用类型变量画等号:
指向同一个对象,会影响
基本类型变量画等号:
赋值、不会影响
笔记:
1.引用类型数组:
1)Student[] stus = new Student[3];
stus[0] = new Student("zhangsan",25,"LF");
stus[1] = new Student("lisi",26,"JMS");
stus[2] = new Student("wangwu",28,"SD");
stus[1].age = 36;
2)Student[] stus = new Student[]{
new Student("zhangsan",25,"LF"),
new Student("lisi",26,"JMS"),
new Student("wangwu",28,"SD")
};
3)int[][] arr = new int[3][];------数组的数组
arr[0] = new int[4];
arr[1] = new int[4];
arr[2] = new int[4];
arr[1][0] = 100; //给arr第2个元素中的第1个元素赋值为100
4)int[][] arr = new int[3][4]; //3行4列
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j] = (int)(Math.random()*100);
System.out.println(arr[i][j]);
}
}
2.继承:
1)作用:代码复用
2)通过extends来实现继承
3)超类/父类:所有派生类所共有的属性和行为
派生类/子类:派生类所特有的属性和行为
4)派生类继承超类后,派生类具有:派生类的+超类的
5)一个超类可以有多个派生类
一个派生类只能有一个超类------单一继承
6)继承具有传递性
7)java规定:构造派生类之前必须先构造超类
派生类中若没有调用超类的构造方法
-------则默认super()调用超类的无参构造方法
派生类中若调用了超类的构造方法
-------则不再默认提供
super()调用超类构造时必须位于派生类构造的第一行
3.super:指代当前对象的超类对象
super的用法:
1)super.成员变量名--------访问超类的成员变量
2)super.方法名()----------调用超类的方法(明天讲)
3)super()-----------------调用超类的构造方法
1)创建类Person,包含:
1.1)成员变量:name,age,address
1.2)构造方法:Person(3个参数){}
1.3)方法:sayHi(){输出3个成员变量的值}
2)创建类Student,继承Person,包含:
2.1)成员变量:stuId
2.2)构造方法:Student(4个参数){}
3)创建类Teacher,继承Person,包含:
3.1)成员变量:salary
3.2)构造方法:Teacher(4个参数){}
4)创建类Doctor,继承Person,包含:
4.1)成员变量:level
4.2)构造方法:Doctor(4个参数){}
5)创建类Test,main()中:
5.1)创建Student数组,包含3个元素,遍历分别问好
5.2)创建Teacher数组,包含3个元素,遍历分别问好
5.3)创建Doctor数组,包含2个元素,遍历分别问好
class Aoo{-----------------------a
int a;
}
class Boo extends Aoo{-----------b+a
int b;
}
class Coo extends Boo{-----------c+b+a
int c;
}
class Doo extends Coo{-----------d+c+b+a
int d;
}
Student zs = new Student();
zs.stuId/study();
zs.name/age/address/eat()/sleep();
Teacher ls = new Teacher();
ls.salary/teach();
ls.name/age/address/eat()/sleep();
class Person{------------------------//超类
String name;
int age;
String address;
void eat(){}
void sleep(){}
}
class Student extends Person{---------派生类
String stuId;
void study(){}
}
class Teacher extends Person{---------派生类
double salary;
void teach(){}
}
class Doctor extends Person{----------派生类
String level;
void cut(){}
}
程序中的继承:
代码不用自己写,自己也能用
生活中的继承:
1)继承财产:
钱不用自己挣,自己也能花
2)继承皇位:
江山不用自己打,自己也能坐
3)继承工作:
工作不用自己找,自己也能干
3:为arr的长度
4:为arr中每个元素的长度
int[][] arr = new int[3][4]; //3行4列
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j] = 100;
}
}
int[] arr = new int[3];
Student[] stus = new Student[3];
//声明int[]的数组,包含3个元素
//每个元素都是int[]型,默认值为null
int[][] arr = new int[3][];
arr[0] = new int[2];
arr[1] = new int[3];
arr[2] = new int[2];
//给arr中第2个元素中的第1个元素赋值为100
arr[1][0] = 100;
arr--------------------int[][]
arr[0]-----------------int[]
arr[0][0]--------------int
int[] arr = new int[]{
1,
5,
7
};
Student[] stus = new Student[]{
new Student(),
new Student(),
new Student()
};
基本类型变量----------装具体的数
引用类型变量----------装地址
int[] arr = new int[3];
arr[0] = 100;
Student[] stus = new Student[3]; //创建Student数组对象
stus[0] = new Student(); //创建Student对象
Airplane[] as = new Airplane[5];
as[0] = new Airplane();
//声明整型数组arr,包含3个元素
//每个元素都是int类型,默认值为0
int[] arr = new int[3];
//声明Student数组,包含3个元素
//每个元素都是Student类型,默认值为null
Student[] stus = new Student[3];
//声明Airplane数组as,包含20个元素
//每个元素都是Airplane类型,默认值为null
Airplane[] as = new Airplane[20];
//声明BigAirplane数组bas,包含23个元素
//每个元素都是BigAirplane类型,默认值为null
BigAirplane[] bas = new BigAirplane[23];
Bee[] bs;
Bullet[] bts;
基本类型------------一个
数组类型------------同类型
自己造个数据类型
class Emp{
String name;
int age;
double salary;
}
void print(String name,int age,double salary){
}