博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
静态static关键字修饰成员变量
阅读量:2162 次
发布时间:2019-05-01

本文共 1334 字,大约阅读时间需要 4 分钟。

package cn.learn.day08.demo03;public class Student {    private int id; // 学号    private String name; // 姓名    private int age; // 年龄    static String room; // 所在教室    private static int idCounter = 0; // 学号计数器,每当new了一个新对象的时候,计数器++    public Student() {        this.id = ++idCounter;    }    public Student(String name, int age) {        this.name = name;        this.age = age;        this.id = ++idCounter;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}
package cn.learn.day08.demo03;/*如果一个成员变量使用了static关键字,那么这个变量不再属于对象自己,而是属于所在的类。多个对象共享同一份数据。 */public class Demo01StaticField {    public static void main(String[] args) {        Student two = new Student("黄蓉", 16);        two.room = "101教室";        System.out.println("姓名:" + two.getName()                + ",年龄:" + two.getAge() + ",教室:" + two.room                + ",学号:" + two.getId());        Student one = new Student("郭靖", 19);        System.out.println("姓名:" + one.getName()                + ",年龄:" + one.getAge() + ",教室:" + one.room                + ",学号:" + one.getId());    }}

 

转载地址:http://rgzzb.baihongyu.com/

你可能感兴趣的文章
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【linux】nohup和&的作用
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【python】re模块常用方法
查看>>
剑指offer 19.二叉树的镜像
查看>>
剑指offer 20.顺时针打印矩阵
查看>>
剑指offer 21.包含min函数的栈
查看>>
剑指offer 23.从上往下打印二叉树
查看>>
剑指offer 25.二叉树中和为某一值的路径
查看>>
剑指offer 60. 不用加减乘除做加法
查看>>
Leetcode C++《热题 Hot 100-14》283.移动零
查看>>
Leetcode C++《热题 Hot 100-15》437.路径总和III
查看>>
Leetcode C++《热题 Hot 100-17》461.汉明距离
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-19》543.二叉树的直径
查看>>