方法是类或对象的行为特征的抽象,方法是类或对象最重要的组成部分。方法在逻辑上要么属于类,要么属于对象。如果是static方法,则该方法属于类;否则属于类的实例。
需要注意的是:同一个类的一个方法调用另外一个方法时,如果被调用方法是普通方法,则默认使用this作为调用者;如果被调用方法是静态方法,则默认使用类作为调用者。也就是说,所有方法都必须使用“对象.方法”或者“类.方法”的形式调用,而同一个类内的方法之间的调用,可以用this,也可以省略this不写,是隐式的。一个类之内的方法调用,可以调用static方法,也可以调用private方法
为了能够更好的理解static方法,类内方法的调用,static方法内调用非静态方法的结果,写了一个小程序,仅供参考
import java.util.*;
public class Student
{
private String name;
private String gender;
private String phone;
private String address;
private String email;
private int age=0;
public void setter()//接收用户输入
{
Scanner scan = new Scanner(System.in);
System.out.println("please enter name:");
this.name = scan.nextLine();
System.out.println("please enter gender:");
this.gender = scan.nextLine();
System.out.println("please enter phone:");
this.phone = scan.nextLine();
System.out.println("please enter address:");
this.address = scan.nextLine();
System.out.println("please enter mail:");
this.email = scan.nextLine();
System.out.println("please enter age:");
this.age = scan.nextInt();
getter();//调用private方法,在同一个类内调用其他方法可以不用this前缀,但是实际上是有this的
kaka();//调用static方法
//search();
}
public void setter(String name,String gender,String phone,String address,String email,int age)
{
this.name=name;
this.gender=gender;
this.phone=phone;
this.address=address;
this.email=email;
this.age=age;
System.out.println("setter running...");
}
private void getter()//输出方法
{
System.out.println("getter running...");
System.out.println("your information is:");
System.out.println("name\tgender\tage\tphone\t\taddress\t\t\tmail");
System.out.println(name+"\t"+gender+"\t"+age+"\t"+phone+"\t"+address+"\t\t"+email);
}
public void search()//查找方法,功能暂时未实现
{
System.out.println("please enter name,email or address for looking somebody:");
Scanner scan = new Scanner(System.in);
String info=scan.nextLine();
if(this.name==info)
{
System.out.println("name\tgender\tage\tphone\t\taddress\t\t\tmail");
System.out.println(this.name+"\t"+this.gender+"\t"+this.age+"\t"+this.phone+"\t"+this.address+"\t\t"+this.email);
}
else if(this.email==info)
{
System.out.println("name\tgender\tage\tphone\t\taddress\t\t\tmail");
System.out.println(name+"\t"+gender+"\t"+age+"\t"+phone+"\t"+address+"\t\t"+email);
}
else if(this.address==info)
{
System.out.println("name\tgender\tage\tphone\t\taddress\t\t\tmail");
System.out.println(name+"\t"+gender+"\t"+age+"\t"+phone+"\t"+address+"\t\t"+email);
}
else
{System.out.println("can't found!");}
}
public void eat()
{System.out.println("eat way running...");}
public void drink()
{System.out.println("drink way running...");}
public void play()
{System.out.println("play way running...");}
public void sleepy()
{System.out.println("sleepy way running...");}
public Student fuck(){ //类类型的方法需返回this
age++;
return this; //必须要有返回语句
}
public static void kaka(){System.out.println("this is the static way!");}
public static void main(String[] args)
{
Student s1=new Student();
Student[] a=new Student[100];
a[0]=s1;
a[0].setter();
//a[0].getter();
//a[0].search();
//getter();//此语句无法编译,因为main方法是静态的static,而getter是非静态的
//静态方法不能直接访问非静态方法,只能通过对象来调用
s1.fuck();
s1.fuck();
s1.fuck();
System.out.println("s1的age成员变量是:"+s1.age);
}
}
运行结果:
please enter name:
xx
please enter gender:
male
please enter phone:
12345678
please enter address:
china_beijing
please enter mail:
12345678@gmai.com
please enter age:
26
getter running...
your information is:
name gender age phone address mail
xx male 26 12345678 china_beijing 12345678@gmai.com
this is the static way!
s1的age成员变量是:29
PS:没有考虑时间空间复杂度,仅仅是演示而已啦!