案例
类除了属性外,还有一些行为,就通过成员方法来完成。
案例一:输出语句方法
public class method1 {
public static void main(String[] args) {
class Person {
String name;
int age;
public void speak() {
// public表示方法是公开的,void表示没有返回值,speak是方法名,括号内没有传参。
System.out.println("我是一个好人");
}
}
Person p1 = new Person();
p1.name = "xiaoming";
p1.age = 22;
//方法的调用
p1.speak();
}
}
我是一个好人
Process finished with exit code 0
案例二:计算从1~1000的结果
//计算从1~1000
public class method2 {
public static void main(String[] args) {
class Person {
String name;
int age;
public void cal01(){
int sum = 0;
for(int i = 1;i<1001;i++){
sum = sum + i;
}
System.out.println(sum);
}
}
Person p1 = new Person();
p1.cal01();
}
}
500500
Process finished with exit code 0
案例三:接受一个参数n,计算1加到n的结果
public class method4 {
public static void main(String[] args) {
class Person {
public void cal(int n){
int sum = 0;
for (int i = 1; i<=n; i++){
sum = sum + i;
}
System.out.println(sum);
}
}
Person p1 = new Person();
p1.cal(1000);
}
}
500500
Process finished with exit code 0
案例四:计算两个数的和
public class method5 {
public static void main(String[] args) {
class Person {
public int cal(int a,int b){
int sum = 0;
sum = a + b;
return sum;
}
}
Person p1 = new Person();
System.out.println( p1.cal(2,3));
}
}
5
Process finished with exit code 0
注意上述又有不同,返回值不为空了,有return。
方法的作用
减少代码的冗余度。提高代码的复用性。
public class Method6 {
public static void main(String[] args) {
class Cal {
public void calculate(int [][] map){
for (int i = 0; i< map.length; i++){
for (int j = 0; j<map[i].length; j++){
System.out.print(map[i][j]+" ");
}
System.out.println();
}
}
}
Cal cal01 = new Cal();
int[][] map = {{1,2,3},{4,5,6},{7,8,9}};
cal01.calculate(map);
}
}
1 2 3
4 5 6
7 8 9
Process finished with exit code 0
注意事项
- 方法定义最前面的public是访问修饰符,作用是控制方法使用的范围。如果不写访问修饰符,就是默认访问。一共有四种 public protected 默认 private。
- 一个方法最多有一个返回值,如果有多个返回值,可以返回一个数组。要求返回值类型必须要与return的值类型一致或兼容。
public class Method7 {
public static void main(String[] args) {
class AA {
public int[] getSum(int a, int b){
int[] res = new int[2];
res[0] = a + b;
res[1] = a - b;
return res;
}
}
AA cal = new AA();
int[] result = cal.getSum(5,2);
for (int i = 0;i <result.length; i++){
System.out.println(result[i]);
}
}
}
7
3
Process finished with exit code 0
- 实参向形参传值时,个数和顺序要一致。
- 方法之中不能再定义方法。方法不能嵌套定义。
调用细节
同一个类中的方法,直接调用即可。
public class Method8 {
public static void main(String[] args) {
class A {
public void print(int n){
System.out.println(n);
}
//同一个类里的方法直接调用即可
public void say(){
print(10);
}
}
A num = new A();
num.say();
}
}
10
Process finished with exit code 0
跨类中的方法,需要通过对象名调用。
public class Method9 {
public static void main(String[] args) {
class A {
public void print(int n){
System.out.println(n);
}
}
class B {
public void sayHi(){
//需要先创建一个A类对象,然后再调用方法
A a = new A();
a.print(100);
}
}
B b = new B();
b.sayHi();
}
}
100
Process finished with exit code 0
练习
编写类AA新方法,判断一个数是奇数还是偶数,返回Boolean。
//编写类AA新方法,判断一个数是奇数还是偶数,返回Boolean。
//如果是偶数,则为true;如果为奇数,则为false。
public class Method10 {
public static void main(String[] args) {
class AA {
public boolean methods(int n){
if (n % 2 == 0){
return true;
}else{
return false;
}
}
}
AA a = new AA();
System.out.println(a.methods(5));
}
}
false
Process finished with exit code 0
根据行、列、字符打印对应行数和列数的字符。
//根据行、列、字符打印对应行数和列数的字符。
public class Method10 {
public static void main(String[] args) {
class AA {
public void methods(int m,int n,char num){
for (int i = 0;i<m;i++){
for (int j = 0; j<n; j++){
System.out.print(num + " ");
}
System.out.println( );
}
}
}
AA a = new AA();
a.methods(3,2,'*');
}
}
* *
* *
* *
Process finished with exit code 0
方法传参机制
public class Method11 {
public static void main(String[] args) {
class AA{
public void swap(int a,int b){
int temp;
temp = a;
a = b;
b = temp;
System.out.print(a + " ");
System.out.print(b + " ");
}
}
AA aa = new AA();
int a = 3;
int b = 2;
aa.swap(a,b);
System.out.print(a + " ");
System.out.print(b);
}
}
2 3 3 2
Process finished with exit code 0