zoukankan      html  css  js  c++  java
  • java中的static关键字, this的使用限制

    static关键字的作用可理解为静态属相, 如静态变量, 静态方法, 即直接使用类名来访问, 虽然使用对象调用也能执行, 但不建议这么用;

    不使用static修饰的变量或方法都只能使用对象调用

    总结: 对象能掉类中一切属相和方法(带不带static都可以), 但是类名调用只能调用带static的属相或方法;   带static的属相和方法即使使用null来调用也可以, 因为其根本不需要对象参与!

            (以上的前提是都在同一个类中, 不在同一个类中的没得玩)

     静态变量在类加载的时候就初始化了, 不需要创建对象, 内存就开辟好了, 其存储在方法区内存中;

    1. 使用static修饰的变量

    public class ClassTest {
    
        public static void main(String[] args) {
            User obj = new User();
    //        obj.func();  使用对象调用static修饰的func, 虽也能执行, 但不建议这么做
            User.func();
            System.out.println(User.i);
    //        System.out.println(User.x);    此处的x没有使用static修饰, 不能用类名来调用
            System.out.println(obj.x);    // 使用对象调用
        }
    
    }
    
    
    class User{
        static int i = 10;
        int x = 10;
    
        public static void func() {
            System.out.println("run func......");
            System.out.println(User.i);
    //        System.out.println(User.x);    此处的x没有使用static修饰, 不能用类名来调用
        }
    }

    2. this关键字代指当前对象, 哪个对象调用就代表哪个对象, 但不可用于带有static修饰的方法中

    class Qu1{
        int i = 100;
        public static void fun1() {
            System.out.println(this.i);  // static修饰的方法中, 使用this会报错, 因为改方法属于类方法, 不涉及对象
        }
    }
    
    
    class Qu2{
        int i = 100;
        public void fun1() {
            System.out.println(this.i);  // this代指调用改方法的对象
        }
    }

    总结: 在使用static修饰的方法, 不涉及对象, 所以其内直接调用不带static的其它方法都不允许;
    不使用static修饰的方法, 都是需要对象调用的, 所以其内可直接调用不带static的其它方法;

     3. this关键字什么时候不能省略?

    public class ClassTest {
        int num;
        public static void main(String[] args) {
            System.out.println("main......");
            ClassTest obj = new ClassTest();
            obj.func(obj.num);
            System.out.println(obj.num);
        }
        
    //    public void func(int num) {
    //        num = num+110;
    //    }
        
        public void func(int num) {
            this.num = num+110;
        }
    
    }

    补充: 如果使用中间注释掉的func的话, 在main中输出的obj.num的值就是0, 因为func中修改的只是局部变量num, 不是类变量num; int变量不赋值, 默认值就是0
    this用于区分局部变量和对象变量的时候不能省略

     4. 构造方法中的this关键字

    public class ClassTest {
    
        public static void main(String[] args) {
            System.out.println("main......");
            Quzq obj1 = new Quzq();
            Quzq obj2 = new Quzq(666);
            obj1.printi();
            obj2.printi();
    
        }
    
    }
    
    
    class Quzq {
        private int x = 1;
    
        public Quzq(int x) {
            super();
            this.x = x;
        }
    
        public Quzq() {
    //        super();            // 此处super方法不能有, 否则this的这种用法会报错
    //        this.x = 110;
            this(110);
        }
        
        public void printi() {
            System.out.println(this.x);
        }
    }

    说明: 在无参实例化时, 其内写死了一个处使化值, 可以直接写死, 但是和上面有参初始化处的代码有冗余, 如何复用上面的有参初始化呢?
      两种方式: 方式一是, 在无参初始化中new一个新对象, 用新对象调用有参初始化设定值, 但是这样一来我们调用无参初始化会产生两个对象!
    方式二是, 使用this关键字, 即this(参数) 的形式, 此时调用无参初始化本质是执行的有参初始化, 其也只会产生一个对象
    注意: this(形参的语法只能出现在构造方法中的第一行!!!!!!)
  • 相关阅读:
    oracle10 http://localhost:5500/em打不开引发的问题
    [bbk5383] 第90集 第11章 数据库诊断 06
    [bbk5373] 第88集 第11章 数据库诊断 04
    ORA01157报错"cannot identify/lock data file"
    [bbk5361] 第86集 第11章 数据库诊断 02
    MFC里AfxGetThread()与AfxGetAPP()的区别
    VC++ 6.0 中类不见了
    MFC中的注释宏
    MFC中OnNcLButtonDown和OnNcLButtonUp的添加方法
    MFC用CWindowDC dc(GetParent())不能在标题栏画线的问题
  • 原文地址:https://www.cnblogs.com/quzq/p/13675235.html
Copyright © 2011-2022 走看看