java method basics

Program function method is used to solve the problem, the program will first begin contents inside the main method

How to define a method and use:

Create a new object: object name = new class name class name ();

Call Method: Method name object name (or no parameters including parameters);

Also note:

1. In the main the same method call a method of a class in object requires a new call, but the call process method can be directly used method name () call

2. The method of transfer of ordinary value the value defined in the main method will not be changed

3. The method of transmission array values array of values defined in the main method is to be changed

package com.Method;

public class test0 {
	// 打印星星号 无参数无返回值的方法
	public void Hello() {
		System.out.println("*************************");
	}

	// 求长方形面积 无参数有返回值的方法
	public double Area() {
		double a = 9.0, b = 3.0;
		return a * b;
	}
	//求三角形面积 有参数无返回值的方法
	public void Area(double h,double d) {
		System.out.println(h*d/2);
	}
	//求平行四边形面积 有参数有返回值的方法
	public float Area(float a,float b) {
		return a*b;
	}

	public static void main(String[] args) {
		test0 one = new test0();
		one.Hello();
		System.out.println("欢迎使用无参数无返回值的方法");
		one.Hello();
		System.out.println();
		one.Hello();

		System.out.println("欢迎使用无参数有返回值的方法" + "\n" + "求出的长方形面积是:" + one.Area());
		one.Hello();
		System.out.println();
		one.Hello();
		System.out.println("欢迎使用有参数无返回值的方法" + "\n" + "求出的三角形面积是:" );
		one.Area(4.0, 5.0);
		one.Hello();
		System.out.println();
		one.Hello();
		System.out.println("欢迎使用有参数有返回值的方法" + "\n" + "求出的三角形面积是:" +one.Area(2.0f, 6.0f));
		one.Hello();
		System.out.println();
	}

}


Using an array as a parameter

package com.Method;

public class test1 {
	//使用数组作为方法参数
	public void printArray(int arr[]) {
		for(int n:arr) System.out.print(n+" ");
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		test1 one=new test1();
		int array[]=new int [5];
		for(int i=0;i<5;i++) {
			array[i]=i+1;
		}
		one.printArray(array);
	}

}


Basic data types passed value problem

package com.Method;

import java.util.Scanner;

public class test2 {

	public void swapab(int m, int n) {
		System.out.println("交换前的m=" + m + "\tn=" + n);
		int temp = m;
		m = n;
		n = temp;
		System.out.println("交换后的m=" + m + "\tn=" + n);
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int a, b;
		a = sc.nextInt();
		b = sc.nextInt();
		System.out.println("交换前的a=" + a + "\tb=" + b);
		test2 tes = new test2();
		tes.swapab(a, b);
		System.out.println("交换后的a=" + a + "\tb=" + b);

	}

}


Array parameter passing problem

package com.Method;

public class test3 {
	public void swaparr(int ar[]) {
		ar[4]=122;
		System.out.println("在方法中改变了数组的值后数组是:");
		for(int i=0;i<5;i++) System.out.print(ar[i]+"\t");
		System.out.println();
	}
	public static void main(String[] args) {
		int arr[]= {1,8,9,2,4};
		System.out.println("调用前数组的值是:");
		for(int i=0;i<5;i++) System.out.print(arr[i]+"\t");
		System.out.println();
		test3 one=new test3();
		one.swaparr(arr);
		System.out.println("调用后数组的值是:");
		for(int i=0;i<5;i++) System.out.print(arr[i]+"\t");
	}

}

Published 44 original articles · won praise 15 · views 20000 +

Guess you like

Origin blog.csdn.net/IGGIRing/article/details/88336786