java--day03

Shoot射击游戏第一天:
1.创建了6个对象类,创建World类测试

Shoot射击游戏第二天:
1.创建6个对象类的构造方法,在World中测试

Shoot射击游戏第三天:
1.设计小敌机数组、大敌机数组、小蜜蜂数组、子弹数组,并测试
2.设计超类FlyingObject,6个对象类继承超类
3.给超类FlyingObject设计两个构造,6个对象类分别调用超类构造


回顾:
1.方法的重载(overload):
  1)发生在一个类中,方法名称相同,参数列表不同
  2)编译器在编译时会根据方法的签名自动绑定调用的方法
2.构造方法:
  1)给成员变量赋初值
  2)与类同名,没有返回值类型
  3)创建对象时被自动调用
  4)若自己不写,则默认无参构造,若自己写了,则不再默认提供
  5)可以重载
3.this:指代当前对象,哪个对象调方法指的就是哪个对象
       方法中访问成员变量之前,默认有个this.
  this的用法:
    1)this.成员变量名---------------访问成员变量
    2)this.方法名()-----------------调用方法
    3)this()------------------------调用构造方法


笔记:
1.引用类型数组:
  1)Student[] stus = new Student[3];
    stus[0] = new Student();
    stus[1] = new Student();
    stus[2] = new Student();
    stus[1].age = 25; //给第2个学生的年龄赋值给25
  2)Student[] stus = new Student[]{
      new Student(),
      new Student(),
      new Student()
    };
  3)int[][] arr = new int[3][];------------数组的数组
    arr[0] = new int[2];
    arr[1] = new int[3];
    arr[2] = new int[2];
    arr[1][0] = 100; //给arr第2个元素中的第1个元素赋值为100
  4)int[][] arr = new int[3][4];
    for(int i=0;i<arr.length;i++){
      for(int j=0;j<arr[i].length;j++){
        arr[i][j] = 100;
      }
    }
2.继承:
  1)作用:代码复用
  2)通过extends来实现继承
  3)超类:所有派生类所共有的属性和行为
    派生类:派生类所特有的属性和行为
  4)派生类继承超类后,派生类具有:派生类的+超类的
  5)一个超类可以有多个派生类
    一个派生类只能有一个超类-----------单一继承
  6)继承具有传递性
  7)java规定:构造派生类之前必须构造超类
    在派生类的构造方法中,若自己不调用超类的构造方法
    --------------则默认super()调用超类的无参构造方法
    在派生类的构造方法中,若自己调用了超类的构造方法
    --------------则不再默认提供
    super()调用超类的构造必须位于派生类构造中的第一行
3.super:指代当前对象的超类对象
  super的用法:
    super.成员变量名---------访问超类的成员变量
    super.方法名()-----------调用超类的方法---------下次课讲
    super()------------------调用超类的构造方法
4.null:空,没有指向任何对象
       若引用的值为null,则该引用不能进行任何操作
       若操作则发生NullPointerException空指针异常



练习:
1.oo.day04包中,要求:
  1)创建Person超类,包含:
    1.1)成员变量:name,age,address
    1.2)构造方法:Person(3个参数){}
    1.3)方法:sayHi()---输出name+age+address的值
  2)创建Student类,继承Person,包含:
    2.1)成员变量:stuId
    2.2)构造方法:Student(4个参数){调超类构造,再给stuId赋值}
  3)创建Teacher类,继承Person,包含:
    2.1)成员变量:salary
    2.2)构造方法:Teacher(4个参数){调超类构造,再给salary赋值}
  4)创建Doctor类,继承Person,包含:
    2.1)成员变量:level
    2.2)构造方法:Doctor(4个参数){调超类构造,再给level赋值}
  5)创建Test类,在main()中:
    5.1)创建Student数组,Teacher数组,Doctor数组,并填充数据
        遍历三个数组,分别跟大家问好

2.将今天的项目代码重写两次















SuperDemo










FlyingObject






















class Aoo{---------------------------------a
  int a;
}
class Boo extends Aoo{---------------------b+a
  int b;
}
class Coo extends Boo{---------------------c+b+a
  int c;
}


















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(){}
}
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();



程序中的继承:
    代码不用自己写,自己也能用

生活中的继承:
1)继承财产:
    钱不用自己挣,自己也能花
2)继承皇位:
    江山不用自己打,自己也能坐
3)继承工作:
    工作不用自己找,自己也能干

























ArrayDemo
















int     [] arr  = new int[3];
Student [] stus = new Student[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] = (int)(Math.random()*100);
  }
}







//声明int[]型数组arr,包含3个元素
//每个元素都是int[]型,默认值为null
int[][] arr = new int[3][];
arr[0] = new int[4];
arr[1] = new int[4];
arr[2] = new int[4];
//给arr第2个元素中的第1个元素赋值为100
arr[1][0] = 100;


arr----------------------int[][]
arr[0]-------------------int[]
arr[0][0]----------------int


















int[] arr = new int[]{
  1,
  4,
  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对象
stus[1] = new Student();
stus[2] = new Student();













Airplane[] as = new Airplane[4];
as[0] = new Airplane();










//声明Student数组stus,包含3个元素
//每个元素都是Student型,默认值为null
Student[] stus = new Student[3];

//声明Airplane数组as,包含3个元素
//每个元素都是Airplane型,默认值为null
Airplane[] as = new Airplane[3];



















int[] arr  = new int[3];

Student[] stus = new Student[3];
Teacher[] tes = new Teacher[3];
Dog[] ds = new Dog[6];
Airplane[] as = new Airplane[3];
Bee[] bs = new Bee[2];






















内存管理:由JVM来管理的
1)堆:new出来的对象(包括成员变量)
2)栈:局部变量
3)方法区:.class字节码文件(包括方法)














class Aoo{
  int a;
  static void main(String[] args){
    int c;
  }
}






















猜你喜欢

转载自www.cnblogs.com/xiaofeixx/p/8903687.html