Home

韧桂

思考,总结

static 的应用

static 的应用一、

package nine;

//static的一个应用:使用静态的变量可以实现“累加”的效果。因为静态的变量在内存中独一份!
public class TestCircle {
	public static void main(String[] args) {
		Circle c1 = new Circle(2.0);
		Circle c2 = new Circle(2.1);
		System.out.println(Circle.getTotal());
		Circle.show();
		c1.setInfo("我是一个漂亮的圆");
		Circle.show();
	}
}
class Circle{
	private double radius;//半径
	private static String info = "我是一个圆";
	private static int total = 0;//因为total是static的,在内存中独一份,所以可以用来记录创建的对象的个数

	public Circle(double radius){
		this.radius = radius;
		total++;
	}
	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}

	public static String getInfo() {
		return info;
	}

	public static void setInfo(String info) {
		Circle.info = info;
	}

	public static int getTotal() {
		return total;
	}

	public static void show(){
		System.out.println(info);
	}

	@Override
	public String toString() {
		return "Circle [radius=" + radius + "]";

	}

}

static 的应用二、 代码一

package nine;

public class ArrayUtil {
    //求数组的最大值
    public static int getMax(int[] arr){
        int max = arr[0];
        for(int i = 1;i < arr.length;i++){
            if(max < arr[i]){
                max = arr[i];
            }
        }
        return max;
    }
    //求数组的最小值
    public static int getMin(int[] arr){
        int min = arr[0];
        for(int i = 1;i < arr.length;i++){
            if(min > arr[i]){
                min = arr[i];
            }
        }
        return min;
    }

    //遍历数组元素
    public static void printArray(int[] arr){
        System.out.print("[");
        for(int i = 0;i < arr.length;i++){
            System.out.print(arr[i] + "\t");
        }
        System.out.println("]");
    }

    //求数组的平均值
    public static int avg(int[] arr){
        int sum = getSum(arr);
        return sum / arr.length;
    }

    //求数组的总和
    public static int getSum(int[] arr){
        int sum = 0;
        for(int i = 0;i < arr.length;i++){
            sum += arr[i];
        }
        return sum;
    }

    //数组的反转
    public static int[] reverse(int[] arr){
        for(int x = 0,y = arr.length - 1;x < y;x++,y--){
            int temp = arr[x];
            arr[x] = arr[y];
            arr[y] = temp;
        }
        return arr;
    }

    //实现数组的复制
    public static int[] copy(int[] arr){
        int[] arr1 = new int[arr.length];
        for(int i = 0;i < arr.length;i++){
            arr1[i] = arr[i];
        }
        return arr1;
    }

    //对数组进行排序
    public static void sort(int[] arr,String desc){
        if(desc == "asc") {//ascend :从小到大
            for (int i = 0; i < arr.length - 1; i++) {
                for (int j = 0; j < arr.length - 1 - i; j++) {
                    if (arr[j] > arr[j + 1]) {
//                        int temp = arr[j];
//                        arr[j] = arr[j + 1];
//                        arr[j + 1] = temp;
                        swap(arr,j,j+1);
                    }
                }
            }
        }else if(desc == "desc"){//从大到小
            for (int i = 0; i < arr.length - 1; i++) {
                for (int j = 0; j < arr.length - 1 - i; j++) {
                    if (arr[j] < arr[j + 1]) {
                        swap(arr,j,j+1);
                    }
                }
            }
        }else {
            System.out.println("你输入的排序方式有误!");
        }
    }
    //交换数组中指定位置的元素:交换arr数组中索引为i和j 的两个元素
    public static void swap(int[] arr,int i,int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

代码二

package nine;

public class FiveDemoArrayUtil {
    public static void main(String[] args) {
        int[] arr = new int[]{99,66,88,55,-2019,0,2020};


        int max = ArrayUtil.getMax(arr);
        System.out.println("最大值为:" + max);

        int avg = ArrayUtil.avg(arr);
        System.out.println("平均值为:" + avg);

        ArrayUtil.printArray(arr);

        System.out.println("反转数组:");
        ArrayUtil.reverse(arr);

        ArrayUtil.printArray(arr);

        System.out.println("对数组排序:");
        ArrayUtil.sort(arr,"desc");//asc

        ArrayUtil.printArray(arr);
    }
}

static 的应用三、

package nine;

/*
 * 编写一个类实现银行账户的概念,包含的属性有“帐号”、“密码”、“存款余额”、“利率”、“最小余额”,定义封装这些属性的方法。账号要自动生成。
编写主类,使用银行账户类,输入、输出3个储户的上述信息。
考虑:哪些属性可以设计成static属性。

 */
public class TestAccount {
	public static void main(String[] args) {
		Account a1 = new Account("abc123",1000);
		Account a2 = new Account("abc456",2000);
		Account a3 = new Account("abc456",2000);
		System.out.println(a1);
		System.out.println(a2);
		System.out.println(a3);
	}
}
class Account{
	private int id;//账号
	private String password;//密码
	private double balance;//余额
	private static double rate = 0.05;//利率
	private static double minBalance = 1;//最小余额
	private static int init = 1000;

	public Account(String password,double balance){
		this.id = init++;
		this.password = password;
		this.balance = balance;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	public static double getRate() {
		return rate;
	}
	public static void setRate(double rate) {
		Account.rate = rate;
	}
	public static double getMinBalance() {
		return minBalance;
	}
	public static void setMinBalance(double minBalance) {
		Account.minBalance = minBalance;
	}

	@Override
	public String toString() {
		return "Account [id=" + id + ", password=" + password + ", balance="
				+ balance + "]";
	}
}

韧桂 2019-12-30