zoukankan      html  css  js  c++  java
  • static关键字和内存使用

    1 static静态的,用来修饰属性,方法,代码块,内部类

    2 当其中一个变量对此属性进行修改,会导致其他对象对此属性的一个调用

    vs 实例变量:对象各有一套副本

    3 静态变量和方法随着类的加载而加载,可以直接用类调用或对象调用,因为类加载早于对象。

    4. static方法(类方法)中不能调用非静态的属性或方法。而非静态的方法可以调用静态的属性或方法。由于静态的结构生命周期早于非静态的结构,死亡还晚。所以在静态方法中调用时对象还没创建,自然不能调用非静态结构。反之亦然。静态方法中不能有this或super。

    5 static的应用:使用静态变量可以实现“累加”的效果,因为静态变量在内存中独一份。

    新建 Microsoft PowerPoint 演示文稿

    package lianxi3;
    
    public class TestStatic {
    
        public static void main(String[] args) {
            Account user1 = new Account("123", 3000);
            Account user2 = new Account("456", 5000);
            Account user3 = new Account("789", 8000);
            System.out.println(user1.toString());
            System.out.println(user2);
            System.out.println(user3);
    
        }
    
    }
    
    class Account {
        private int id;
        private String password;
        private static int rate;
        private double balance;
        private static double minbalance;
        private static int init = 1200; // 初始化账号
    
        public Account(String password, double balance) { // 注意:不用再写int id,已经设置初值了
            this.id = init++;
            this.password = password;
            this.balance = balance;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public static int getRate() {
            return rate;
        }
    
        public static void setRate(int rate) {
            Account.rate = rate;
        }
    
        public double getBalance() {
            return balance;
        }
    
        public void setBalance(double balance) {
            this.balance = balance;
        }
    
        public static double getMinbalance() {
            return minbalance;
        }
    
        public static void setMinbalance(double minbalance) {
            Account.minbalance = minbalance;
        }
    
        public static int getInit() {
            return init;
        }
    
        public static void setInit(int init) {
            Account.init = init;
        }
    
        public int getId() {
            return id;
        }
    
        @Override
        public String toString() {
            return "Account [balance=" + balance + ", id=" + id + ", password="
                    + password + "]";
        }
    
    }

    结果:

    Account [balance=3000.0, id=1200, password=123]
    Account [balance=5000.0, id=1201, password=456]
    Account [balance=8000.0, id=1202, password=789]

  • 相关阅读:
    calc属性
    transform,transtion属性
    cursor:属性
    html页面禁止用户右键粘贴复制保存的代码
    git 删除与撤回
    git 仓库原理
    git 创建本地仓库
    git提示错误关于错误:ssh: Could not resolve hostname github.com: Name or service not known.fatal: Could not read from remote repository.
    AttributeError: module 'socket' has no attribute 'SO_REUSEPORT'
    python paramiko模块sftp异常:paramiko.ssh_exception.SSHException: EOF during negotiation
  • 原文地址:https://www.cnblogs.com/yjtm53/p/4135310.html
Copyright © 2011-2022 走看看