Java-方法

Java 方法

  • 方法的命名规则

    1. 可以包含数字,必须以字母、'_'、'$'开头
  • 方法的定义
    修饰符 返回值类型 方法名(参数类型 参数名) {
    ...
    方法体
    ...
    return 返回值
    }

    1. 修饰符:可选,定义方法的访问类型,告诉编译器如何调用该方法

    2. 返回值类型:没有返回值时,关键字为void

    3. 方法名:方法的实际名称,方法名与参数表共同构成方法签名

    4. 参数类型:参数列表是指方法的参数类型,顺序和参数个数

    5. 包含具体语句,定义方法功能

       public static int age(int birthday) {...}
      
  • 方法调用

    1. 当方法返回一个值时,方法调用通常被当做一个值

       int large = max(30, 40);
      
    2. 方法返回值是void,方法调用一定是一条语句

       System.out.println("Hello world");
      
    3. 方法定义及调用实例

                   public class TestMax {
       	//主方法
       	public static void main(String[] args) {
       		int i = 5;
       		int j = 2;
       		int k = max(i, j);
       		System.out.println(i + "和" + j + "比较,最大值是:" + k); 
       	}
       	//返回两个整数变量较大值
       	public static int max(int num1,int num2) {
       		int result;
       		if (num1 > num2)
       			result = num1;
       		else
       			result = num2;
       	}
       }
      
  • 方法的重载:一个类的两个方法拥有相同的名字,但是有不同的参数列表;Java编译器根据方法签名判断哪个方法应该被调用;重载方法必须拥有不同的参数列表

  • 命令行参数的使用

      public class CommandLine {
      	public static void main(String args[]) {
      		for(int i = 0; i < args.length; i++) {
      			System.out.println("args[" + i + "]:" + args[i]);
      		}
      	}
      }
      // 运行命令
      // javac CommandLine.java
      // java CommandLine this is a command line 200 -100
    
  • 构造方法
    当一个对象被创建时,构造方法用来初始化该对象你。构造方法和他所在的类的名字相同,但构造方法没有返回值。通常会使用构造方法给一个类的实例变量赋初值,或者执行其他必要的步骤来创建一个完整的对象。不管与否自定义构造方法,因为Java自动提供了一个默认构造方法,它把所有成员初始化为0.一旦自定义了构造方法,默认构造方法就会失效

      class MyClass {
      	int x;
      	// 构造函数
      	MyClass() {
      		x = 10;
      	}
      }
      // 调用构造方法初始化一个对象
      public class ConsDemo {
      	public static void main(String args[]) {
      		MyClass t1 = new MyClass();
      		MyClass t2 = new MyClass();
      		System.out.println(t1.x + " " + t2.x);
      	}
      }
      // 有参数的构造方法
      class MyClass {
      	int x;
      	// 构造方法
      	MyClass(int i) {
      		x = 1;
      	}
      }
      // 调用构造方法初始化一个对象
      public class ConsDemo {
      	public static void main(String args[]) {
      		MyClass t1 = new MyClass(10);
      		MyClass t2 = new MyClass(20);
      		System.out.println(t1.x + " " + t2.x);
      	}
      }
    
  • 可变参数 typename... parameterName 只能有一个可变参数,切必须是最后一个参数;普通参数必须在其之前声明

      public class VaragsDemo {
      	public static void main(String args[]) {
      		//调用可变参数方法
      		printMax(34, 3, 3, 2, 56.5);
      		printMax(new double[] {1, 2, 3});
      	}
      	public static void printMax(double... number) {
      		if (numbers.length == 0) {
      		 System.out.println("No argument passed");
      		 return;
      		}
    
      		double result = numbers[0];
    
      		for (int i = 1; i < numbers.length; i++) {
      			if (numbers[i] > result) {
      				result = numbers[i];
      			}
      		}
      		System.out.println("The max value is" + result);
      	}
      }
    
  • finalize()方法,它在对象被垃圾收集器析构(回收)之前调用,用来清除回收对象

      public class FinalizationDemo {
      	public static void main(String[] args) {
      		Cake c1 = new Cake(1);
      		Cake c2 = new Cake(2);
      		Cake c3 = new Cake(3);
    
      		c2 = c3 =null;
      		System.gc();//调用Java垃圾收集器
      	}
      }
    
      class Cake extends Object {
      	private int id;
      	public Cake(int id) {
      		this.id = id;
      		System.out.println("Cake Object" + id + "is created");
      	}
    
      	protected void finalize() throws java.lang.Thrawable {
      		super.finalize();
      		System.out.println("Cake Object" + id + "is diaposed")
      	}
      }
    
posted @ 2017-08-17 21:35  YFIFE  阅读(135)  评论(0编辑  收藏  举报