一、参数列表
一个方法中可以不带参数,也可以带参数,参数不限。
参数的作用就是为方法带入所需的原始值。
多个参数之间用逗号分隔。
语法:
修饰符 返回类型 方法名(类型 参数1,类型 参数2,...,类型 参数n){
方法体;
}
二、带参数的方法的调用
调用带参数的方法,实参的个数一定要和形参个数一样,并且类型要相同,顺序也要一样。
1、定义时的参数,称为形参
2、调用时所使用的参数,称为实参。
调用过程中,实参的值一一对应传给形参。实参1传给形参1,实参2传给形参2,以此类推。
public class Student {
long stuID;
String name;
int age;
boolean sex;
public int show(){
System.out.println("stuID:" + stuID + " name:" + name);
return age;
}
public void setData(long x, String y, int z){//形参
stuID = x;
name = y;
age = z;
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s = new Student();
int age;
age = s.show();
s.setData(20131546, "denghong", 7);
s.show();
}
}
三、main方法
main方法是程序的入口,并不属于任何一个类,放在某个类中,只是借用位置。
public class Student {
long stuID;
String name;
int age;
boolean sex;
public int show(){
System.out.println("stuID:" + stuID + " name:" + name);
return age;
}
public void setData(long x, String y, int z){//形参
stuID = x;
name = y;
age = z;
}
public static void main(String[] args) {
System.out.println(stuID);//错误,不能直接使用stuID,只能通过对象来进行调用。
}
}
四、生成对象
语法:
1)声明变量:类 变量名;
2)开辟空间:变量名 = new 构造方法();
public class Test {
public static void main(String[] args) {
// Student s = new Student();
Student s;//声明变量
s = new Student();//开辟空间
}
}
只有声明,是不能进行调用的。如需调用,必须开辟空间。
public class Test {
public static void main(String[] args) {
Student s;//声明变量
s.show();//错误,只有声明就不能进行调用
}
}
第二步开辟空间的过程,会为成员变量赋初值:数值类型赋0;引用类型赋null,字符赋空字符。
五、成员的使用
语法:
对象.成员;
成员包括成员变量和成员方法。
使用某个对象的成员。前提条件是这个成员必须是可见的。
类是对象的模板,对象是类的实例化。一个类可以生成多个对象,每个对象都有自己独立的内存空间。一个对象只能由一个类生成。
public class Test {
public static void main(String[] args) {
Student s = new Student();
Student s1 = new Student();
s.name = "denghong";
System.out.println("stuID = " + s1.name);
}
}
六、作用域
变量有成员变量和局部变量
直接在类里面定义的称为成员变量;
在方法中定义的称为局部变量;
public class Student {
String name;//成员变量
public void setData(String name){//局部变量
name = name;//两个name都是局部变量
}
}
作用域就是变量所能起作用的区域,或者能够调用到某个变量的区域。
成员变量的作用域是整个类;局部变量的作用域是整个方法。
如果两个作用域重合了。作用域小的变量起作用。
七、this指针
this指针代表是本身对象。就是本身类的一个对象。
如果成员变量和局部变量重名,this指针所调用的重名变量就为成员变量。
public class Student {
long stuID;
String name;
int age;
boolean sex;
public void setData(String name){
this.name = name;
}
}
八、static 关键字
使用static修饰的变量,称为静态变量;
使用static修饰的方法,称为静态方法。
1、静态变量
静态变量是指所有的对象都共享同一个空间。
public class Student {
long stuID;
String name;
static int counter;
public void setData(String name){//形参
this.name = name;
}
}
public class Test {
public static void main(String[] args) {
Student s1 = new Student();//开辟空间
Student s2 = new Student();//开辟空间
s1.counter = 1;
System.out.println(s2.counter);//1
}
}
非静态变量是属于每个对象,而静态变量不属于对象,只属于类。静态变量的调用不需要生成对象,可以直接使用类名进行调用。
调用的语法:
类名.静态变量;
当然,也可以使用对象来进行调用。
public class Test {
public static void main(String[] args) {
Student.counter = 1;//直接使用类进行访问
}
}
2、静态方法
静态方法中不能访问非静态变量,只能访问静态变量和其本身的局部变量。静态方法也只属于类,也可以直接使用类名来进行调用。
public class Student {
long stuID;
String name;
static int counter;
public static void setData(String name){//形参
this.name = name;//错误,在静态方法中不能访问非静态变量
}
}
public class Student {
long stuID;
String name;
static int counter;
public static void setData(int c){//形参
counter = c;
}
}
public class Test {
public static void main(String[] args) {
Student.setData(4);
System.out.println(Student.counter);
}
}