理解java类成员变量

Understanding Class Members理解类成员变量

  1. static描述符的提出(class variables)

    When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables. In the case of the Bicycle class, the instance variables are cadence, gear, and speed. Each Bicycle object has its own values for these variables, stored in different memory locations. Sometimes, you want to have variables that are common to all objects.
    当大量的对象从相同的类中被创建时,他们每一个都有自己独有的实例变量。在Bicycle类的例子中,节奏,齿轮和速度都是实例变量。每一个Bicycle对象对于这些变量都有自己的值,被存在不同的内存位置上。

但是有时候,你想要有一种变量它是被所有的对象所共用的。

  1. 应用举例

    情景需求:想要知道有多少个Bicycle对象被创建

  2. 代码示例

public class Bicycle {

    private int cadence;
    private int gear;
    private int speed;

    private int id;

    private static int numberOfBicycles = 0;


    public Bicycle(int startCadence,
                   int startSpeed,
                   int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;

        id = ++id;
    }

    public Bicycle(int startCadence, int startSpeed) {
    }

    public int getID() {
        return id;
    }

    public static int getNumberOfBicycles() {
        return numberOfBicycles;
    }

    public int getCadence() {
        return cadence;
    }

    public void setCadence(int newValue) {
        cadence = newValue;
    }

    public int getGear(){
        return gear;
    }

    public void setGear(int newValue) {
        gear = newValue;
    }

    public int getSpeed() {
        return speed;
    }

    public void applyBrake(int decrement) {
        speed -= decrement;
    }

    public void speedUp(int increment) {
        speed += increment;
    }
}
public class Bicycle {

    private int cadence;
    private int gear;
    private int speed;

    private int id;

    private static int numberOfBicycles = 0;


    public Bicycle(int startCadence,
                   int startSpeed,
                   int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;

        id = ++numberOfBicycles;
    }

    public Bicycle(int startCadence, int startSpeed) {
    }

    public int getID() {
        return id;
    }

    public static int getNumberOfBicycles() {
        return numberOfBicycles;
    }

    public int getCadence() {
        return cadence;
    }

    public void setCadence(int newValue) {
        cadence = newValue;
    }

    public int getGear(){
        return gear;
    }

    public void setGear(int newValue) {
        gear = newValue;
    }

    public int getSpeed() {
        return speed;
    }

    public void applyBrake(int decrement) {
        speed -= decrement;
    }

    public void speedUp(int increment) {
        speed += increment;
    }
}

  1. 运行结果及分析
NumberOfBicycles: 1
NumberOfBicycles: 2
id: 2

假如将 id = ++numberOfBicycles; 改为 id = ++ id;每创建一个对象,id的值都为1,不能解决上述需求。

  1. Class Methods
public static int getNumberOfBicycles() {
    return numberOfBicycles;
}

相关的一些规则

1,Instance methods can access instance variables and instance methods directly.
2,Instance methods can access class variables and class methods directly.
3,Class methods can access class variables and class methods directly.
4,Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.
参考链接:[Understanding Class Members](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值