zoukankan      html  css  js  c++  java
  • 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.");
    	}
    }
    

      

  • 相关阅读:
    微信公众号扫一扫接口
    JDBC-用户登录验证(sql注入)
    JDBC
    Shell脚本
    java-变量总结
    java-那些方法不能被重写
    java-数组工具类
    java-类初始化与实例初始化
    java-static
    java-native修饰符
  • 原文地址:https://www.cnblogs.com/pxy7896/p/6743600.html
Copyright © 2011-2022 走看看