JAVA---类中方法的声明和使用

方法:描述类应该具有的功能

方法的声明: 权限修饰符 返回值类型 方法名(形参列表){

​ 方法体

​ }

简单类示范:

package lll;


public class Test {
	public static void main(String[] args) {
		Person p1=new Person();
		p1.name="Amy";
		p1.age=18;
		p1.sex=0;
		p1.study();
		p1.showAge();
		p1.addAge(2);
		p1.showAge();
	}
}
class Person{
	String name;
	int age;
	int sex;
	public void study(){
		System.out.println("studying");
	}
	public void showAge(){
		System.out.println("age:"+age);
	}
	public int addAge(int i){
		age+=i;
		return age;
	}
}
posted @ 2022-01-06 15:41  ice--cream  阅读(171)  评论(0编辑  收藏  举报