Java教程

java 16.静态static关键字修饰成员

本文主要是介绍java 16.静态static关键字修饰成员,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

static

在一个类中,如果使用static关键字来修饰类属性(成员变量),那这个变量就属于类,类里面的所有对象都可以共享这个变量。

什么意思呢?我们看一下代码就明白了:

现在有一个类Student

package static_demo;

public class Student {
    private String name;
    private  int age;
    static String room;

    public Student() {
        System.out.println("调用空Student");
    }

    public Student(String name, int age) {
        System.out.println("调用带参Student");
        this.name = name;
        this.age = age;
    }

    public String getName() {
        System.out.println("调用getName方法");
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        System.out.println("调用getAge方法");
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static String getRoom() {
        System.out.println("调用getRoom方法");
        return room;
    }

    public static void setRoom(String room) {
        Student.room = room;
    }
}

然后我们下面这个函数调用Student类实例化两个Student成员对象one和two、并且定义one.room= "101教室",这里two可以不声明而直接使用!这就是因为在Student类中我们通过static来修饰room这个变量,room不是one对象的,虽然他对其做了声明,但是room是属于Student类的,不管创建几个Student的成员对象,成员之间都是共享一个room,这个room属于Student类。

package static_demo;

public class staticField {
    public static void main(String[] args) {
        Student one = new Student("郭靖",19);
        one.room = "101教室";
        System.out.println("姓名:" + one.getName() + ",年龄:" + one.getAge() + "所在教室:" + one.room);
        Student two = new Student("黄蓉",16);
        System.out.println("姓名:" + two.getName() + ",年龄:" + two.getAge() + "所在教室:" + one.room);
    }
}

在这个例子里,没有执行到空Student对象public Student()


下面我们实现这样一个效果:每次new Student对象都做一个加一操作,让学号递增。

也很简单,Student类代码如下

package static_demo;

public class Student {
    private int no;  //学号
    private String name;
    private  int age;
    static String room;
    private static int noCounter = 0;  //学号计数器

    public Student() {
        System.out.println("调用空Student");
        noCounter++;
    }

    public Student(String name, int age) {
        System.out.println("调用带参Student");
        this.name = name;
        this.age = age;
        this.no = ++noCounter;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public static int getNoCounter() {
        return noCounter;
    }

    public static void setNoCounter(int noCounter) {
        Student.noCounter = noCounter;
    }

    public String getName() {
        System.out.println("调用getName方法");
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        System.out.println("调用getAge方法");
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static String getRoom() {
        System.out.println("调用getRoom方法");
        return room;
    }

    public static void setRoom(String room) {
        Student.room = room;
    }
}

调用类的代码

package static_demo;

public class staticField {
    public static void main(String[] args) {
        Student one = new Student("郭靖",19);
        one.room = "101教室";
        System.out.println("姓名:" + one.getName()
                + ",年龄:" + one.getAge() + "所在教室:" + one.room + ",学号:" + one.getNo());
        Student two = new Student("黄蓉",16);
        System.out.println("姓名:" + two.getName()
                + ",年龄:" + two.getAge() + "所在教室:" + two.room + ",学号:" + two.getNo());
        Student oyf = new Student("欧阳锋",56);
        System.out.println("欧阳锋的学号是:" + oyf.getNo());
        Student yg = new Student("杨过",14);
        System.out.println("杨过的学号是:" + yg.getNo());
        Student xln = new Student("小龙女",16);
        System.out.println("小龙女的学号是:" + xln.getNo());
    }
}

输出

调用带参Student
调用getName方法
调用getAge方法
姓名:郭靖,年龄:19所在教室:101教室,学号:1
调用带参Student
调用getName方法
调用getAge方法
姓名:黄蓉,年龄:16所在教室:101教室,学号:2
调用带参Student
欧阳锋的学号是:3
调用带参Student
杨过的学号是:4
调用带参Student
小龙女的学号是:5

这篇关于java 16.静态static关键字修饰成员的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!