oo.day02

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

Shoot射击游戏第二天:
1.给6个对象类添加构造方法,并测试


回顾:
1.什么是类?什么是对象?
2.如何创建类?如何创建对象?如何访问成员?


笔记:
0.方法的签名:方法名+参数列表
1.方法的重载(Overload):
  1)发生在一个类中,方法名称相同,参数列表不同,方法体不同
  2)编译器在编译时根据方法的签名自动绑定调用的方法
2.构造方法:构造函数/构造器/构建器
  1)给成员变量赋初值
  2)与类同名,没有返回值类型
  3)在创建(new)对象时被自动调用
  4)若自己不写构造方法,则编译器默认一个无参构造方法
    若自己写了构造方法,则编译器不再默认提供
  5)构造方法可以重载
3.this:指代当前对象,哪个对象调用方法它指的就是哪个对象
       只能用在方法中,方法中访问成员变量之前默认有个this.
  this的用法:
    1)this.成员变量名---------访问成员变量
    2)this.方法名()-----------调用方法(一般不用)
    3)this()------------------调用构造方法
4.null:空,没有指向任何对象
       若引用的值为null,则该引用不能再进行任何操作了,
       若操作则发生NullPointerException空指针异常
5.引用类型变量画等号:
  1)指向同一个对象
  2)通过一个引用对数据的修改会影响另一个引用对数据的访问
    eg:房子钥匙
  基本类型变量画等号:
  1)赋值
  2)对一个变量的修改不会影响另一个变量的访问
    eg:身份证复印件


练习:
1)画图
2)将今天的项目代码在昨天基础之上重做两次

 


房子:对象
房子钥匙:引用
配一把钥匙:引用


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


void show(int a){
  double b = 34.567;
}

Student zs = new Student();


x = (int)(Math.random()*(400-this.width));

Random rand = new Random();
x = rand.nextInt(400-this.width);


y = 


class Student{
  String name;
  int age;
  String address;

Student zs = new Student("zhangsan"); //zhangsan,0,null
Student zs = new Student("zhangsan",25); //zhangsan,25,null
Student zs = new Student("zhangsan",25,"LF"); //zhangsan,25,LF


Student zs = new Student("zhangsan",25); //zhangsan,25,null
Student zs = new Student("zhangsan"); //zhangsan,0,null

  Student(String name){
    this(name,0);
  }
  Student(String name,int age){
    this(name,age,null);
  }
  Student(String name,int age,String address){
    this.name = name;
    this.age = age;
    this.address = address;
  }
}


Student zs = new Student("zhangsan",25,"LF");
Student ls = new Student("lisi",26,"JMS");

class Student{
  String name;
  int age;
  String address;
  Student(String name,int age,String address){
    this.name = name;        //ls.name="lisi"
    this.age = age;          //ls.age=26
    this.address = address;  //ls.address="JMS"
  }
}


就近原则
当成员变量与局部变量同名时,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("大家好,我叫"+name+",今年"+age+"岁了,家住"+address);
  }
}


Student zs = new Student();
zs.name = "zhangsan";
zs.age = 25;
zs.address = "LF";
zs.study();
zs.sayHi();

Student ls = new Student();
ls.name = "lisi";
ls.age = 26;
ls.address = "JMS";
ls.study();
ls.sayHi();

Student ww = new Student();
ww.study();
ww.sayHi();

class Student{
  String name=null;
  int age=0;
  String address=null;

  void study(){          ww.name
    System.out.println(this.name+"在学习...");
  }
  void sayHi(){                         ww.name            ww.age                ww.address
    System.out.println("大家好,我叫"+this.name+",今年"+this.age+"岁了,家住"+this.address);
  }
}

class Student{
  String name;
  int age;
  String address;
  Student(){
  }
  //给成员变量赋初值
  Student(String name1,int age1,String address1){
    name = name1;
    age = age1;
    address = address1;
  }
}

Student zs = new Student();
Student zs = new Student("zhagnsan",25,"LF");


class Student{
  String name;
  int age;
  String address;
  //给成员变量赋初值
  Student(String name1,int age1,String address1){
    name = name1;
    age = age1;
    address = address1;
  }
}

Student zs = new Student(); //编译错误
Student zs = new Student("zhangsan",25,"LF");


Student zs = new Student("zhangsan",25,"LF");
Student ls = new Student("lisi",26,"JMS");

class Student{
  String name;
  int age;
  String address;
  //给成员变量赋初值
  Student(String name1,int age1,String address1){
    name = name1;
    age = age1;
    address = address1;
  }
}

//1)创建一个学生对象
//2)给成员变量赋默认值
//3)调用Student类的构造方法
Student zs = new Student();

Student zs = new Student();
zs.setInfo("zhangsan",25,"河北廊坊");

Student ls = new Student();
ls.setInfo("lisi",26,"黑龙江佳木斯");

Student zs = new Student();
zs.name = "zhangsan";
zs.age = 25;
zs.address = "河北廊坊";

Student ls = new Student();
ls.name = "lisi";
ls.age = 26;
ls.address = "黑龙江佳木斯";


void println(){
}
void println(int a){
}
void println(double a){
}
void println(boolean a){
}
void println(char a){
}
void println(String a){
}

System.out.println();
System.out.println(23);
System.out.println(35.7);
System.out.println(true);
System.out.println('你');
System.out.println("Hello");


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值