Java-basic-6-方法

  • 命令行参数的使用
public class test {
	public static void main(String args[]) {
		for(int i = 0; i < args.length; i++)
			System.out.println("args[" + i + "]" + args[i]);
	}
}


  • 可变参数

一个方法中只能指定一个可变参数,它必须是方法的最后一个参数。

声明方法:type... variableName

public class test {
	public static void main(String args[]) {
		// Attention! array construction
		printMax(new double[]{1.2, 3.4, -0.5});
	}
	// it should be static, otherwise error
	// typeName... variableName
	public static void printMax(double... arr) {
		if(arr.length == 0)
			return;
		double result = arr[0];
		for(int i = 1; i < arr.length; i++) {
			if(arr[i] > result)
				result = arr[i];
		}
		System.out.printf("Max of them is " + result);
	}
}

  • finalize()方法

 在对象被垃圾收集器析构(回收)之前调用,例如,你可以使用finalize()来确保一个对象打开的文件被关闭了。

 在finalize()方法里,你必须指定在对象销毁时候要执行的操作。

public class test {
	public static void main(String args[]) {
		Cake c1 = new Cake(1);
		Cake c2 = new Cake(2);
		// finalize() of c2 will be called.
		c2 = null;
		// garbage collection.
		System.gc();
	}
	
}
class Cake extends Object {
	private int id;
	public Cake(int id) {
		this.id = id;
		System.out.println("Cake " + id + " is created.");
	}
	// Attention.
	protected void finalize() throws java.lang.Throwable {
		// use Object's
		super.finalize();
		System.out.println("Cake " + id + " is disposed.");
	}
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值