首页 > 代码库 > JAVA笔记4__static关键字/对象数组/foreach/方法的可变参数

JAVA笔记4__static关键字/对象数组/foreach/方法的可变参数

/** * static关键字:修饰属性(实质就是全局变量)、方法(无需本类的对象即可调用此方法)、类。 * 1.static方法只能调用static方法 * 2.static方法只能访问static数据 * 3.static方法不能以任何方式引用this或super */public class Main {    public static void main(String[] args) {        A a = new A();  a.val = 2;        A a1 = new A(); a1.val = 3;        System.out.println(a.val+" "+a1.val);        B b = new B();  b.val = 2;        B b1 = new B(); b1.val = 3;        System.out.println(b.val+" "+b1.val); //b和b1拥有同一份val        A.say();    }}class A{    int val = 1;    public static void say(){        System.out.println("Hello~~");    }}class B{    static int val = 1; //静态数据不属于对象,属于类}

 

import java.lang.String;import java.util.Arrays;import java.util.Scanner;/** * 对象数组 */public class Main {    public static void main(String[] args) {        MonkeyManager  mm = new MonkeyManager();        mm.add(new Monkey("1"));        mm.add(new Monkey("2"));        mm.add(new Monkey("2"));        mm.add(new Monkey("4"));        mm.delete("2");        mm.list();                Monkey temp1 = new Monkey("1");        mm.update(temp1);        mm.list();    }}class MonkeyManager{    private Monkey[] ms = new Monkey[3];    private int count = 0;    public void add(Monkey m){        if(count<ms.length){            ms[count++] = m;            System.out.println("Successful add monkey: "+m.name);        }        else{            System.out.print("空间已满!进行扩充....");             int newLen = (ms.length*3)/2+1;            ms = Arrays.copyOf(ms, newLen);            System.out.println("扩充后的长度为:"+newLen);            ms[count++] = m;            System.out.println("Successful add monkey: "+m.name);        }    }    public void list(){        for(int i=0;i<count;++i) ms[i].say();    }    public void delete(String name){        boolean flag = true;        while(flag){            flag = false;            for(int i=0;i<count;++i) if(ms[i].name.equals(name)){                for(int j=i;j<count-1;++j) ms[j]=ms[j+1];                ms[count-1] = null;                --count;                System.out.println("Successful delete monkey: "+name);                flag = true;                break;            }        }    }    public Monkey find(String name){        for(int i=0;i<count;++i) if(ms[i].name.equals(name)) return ms[i];        return null;    }    public void update(Monkey m){        Monkey s = find(m.name);        if(s!=null){            s.name = "helloWorld";        }    }}class Monkey{    String name;    public Monkey(String name){        this.name = name;    }    public void say(){        System.out.println("I‘m "+name);    }}

 

public class Main {    public static void main(String[] args) {        /**         * foreach语法         * for(类型 变量名:数组或集合){ //输出操作 }         */        String[] names = {"1","2","3","4"};        for(String i:names) System.out.println(i);                        /**        * 方法的可变参数        * 1.只能有一个        * 2.有多个参数时,可变参数放在最后        */        method1(names);        method2(123,"a1","a2","a3");        method2(123,names);    }    public static void method1(String[] na){        for(String i:na) System.out.println(i);    }    public static void method2(int x,String... na){ //参数为可变参数(只能有一个)        System.out.println("x="+x);        for(String i:na) System.out.println(i);    }}

 

JAVA笔记4__static关键字/对象数组/foreach/方法的可变参数