成员方法不需要写static
类的使用:
1、导包
import 包名称.类名称;
对于和当前类属于同一个包的情况可以不写导包语句
2、创建
Student stu = new Student();
3、使用
使用成员变量:对象名.成员方法名
使用成员方法:对象名.成员方法名(参数)
代码:
package com.company;
public class Main {
public static void main(String[] args) {
//方法的调用
Student s = new Student();//实例化对象
s.introduction("J",21);//调用成员方法
}
public static class Student{//定义学生类
//成员方法--自我介绍
public void introduction(String name,int age){
System.out.println("我叫" + name + "今年" + age +"岁");
}
//成员方法--吃
public void eat(){ //注意没有static!!
System.out.println("干饭!");
}
}
}
我叫J今年21岁