Shoot射击游戏第一天:
1.创建了6个对象类,创建World类测试
Shoot射击游戏第二天:
1.创建6个对象类的构造方法,在World中测试
回顾:
1.什么是类?什么是对象?
2.如何创建类?如何创建对象?如何访问成员?
笔记:
1.方法的重载(Overload):
1)发生在一个类中,方法名称相同,参数列表不同
2)编译器在编译时会根据方法的签名自动绑定调用的方法
2.构造方法:构造函数、构造器,构建器
1)给成员变量赋初值
2)与类同名、没有返回值类型
3)在创建(new)对象时被自动调用
4)若自己不写构造方法,则编译器默认一个无参构造方法,
若自己写了构造方法,则不再默认提供
5)构造方法可以重载
3.this:指代当前对象,哪个对象调用方法指的就是哪个对象
只能用在方法体中,方法中访问成员变量之前默认有个this.
this的用法:
1)this.成员变量名--------------访问成员变量
2)this.方法名()----------------调用方法(几乎不用)
3)this()-----------------------调用构造方法
x = (int)(Math.random()*(400-49));
Random rand = new Random();
x = rand.nextInt(400-49);
class Student{
String name;
int age;
String address;
Student(String name){
this.name = name;
this.age = 0;
this.address = null;
}
Student(String name,int age){
this.name = name;
this.age = age;
this.address = 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"
}
void study(){
System.out.println(name+"在学习...");
}
}
成员变量和局部变量是可以同名的,使用时采用的是就近原则
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();
class Student {
String name;
int age;
String address;
void study(){ ls.name
System.out.println(this.name+"在学习...");
}
void sayHi(){ ls.name ls.age ls.address
System.out.println("大家好,我叫"+this.name+",今年"+this.age+"岁了,家住"+this.address);
}
}
Student zs = new Student("zhangsan",25,"LF");
Student zs = new Student();
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("zhangsan",25,"河北廊坊");
Student ls = new Student("lisi",26,"JMS");
class Student{
String name;
int age;
String address;
//给成员变量赋初值
Student(String name1,int age1,int 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 printlnInt(int a){
}
void printlnDouble(double a){
}
void printlnChar(char a){
}
void printlnBoolean(boolean a){
}
void printlnString(String a){
}
System.out.println();
System.out.println(34);
System.out.println(45.6);
System.out.println('你');
System.out.println(true);
System.out.println("Hello");
OverloadDemo
java--day02
猜你喜欢
转载自www.cnblogs.com/xiaofeixx/p/8903686.html
今日推荐
周排行