zoukankan      html  css  js  c++  java
  • java --static

    static修饰属性(类变量)
    * 1.由类创建的所有对象,都共用这一个属性
    * 2.当其中一个对象对此属性进行修改,会导致其他的对象对此属性的一个调用,即其他对象获取这个属性的值都是被修改过后的值。
    * 3.类变量是随着类的 加载 而 加载 的,而且独一份。
    * 4.静态的变量可以直接通过“类.类变脸”的形式来调用
    * 5.类变量的加载是早于对象的,所以,当有对象之后,可以“对象.类变量”使用,但是“类.实例变量”是不行的。
    * 6.类变量存在于静态域中。


    static修饰方法(类方法)
    * 1.随着类的加载而加载,在内存中独一份。
    * 2.可以直接通过“类.类方法”的方式来调用
    * 3.静态方法的内部只能调用静态的属性和静态的方法。因为它们 的生命周期是一样的。而不能调用非静态的。非静态的方法是可以调用静态的方法和静态的属性的。
    * 静态的(static的属性,方法,代码块,内部类)结构的生命周期要早于非静态的结构,同时被回收也要晚于非静态的结构,
    * >静态的方法里面是不可以有super和this关键字的。

     1 public class statication {
     2     public static void main(String[] args){
     3         SportMan s1 = new SportMan("lee",23);
     4         SportMan s2 = new SportMan("lei",21);
     5         s1.name ="lie";
     6         s1.nation ="China";
     7         System.out.println(s1);
     8         System.out.println(s2);
     9         System.out.println(SportMan.nation);
    10         s1.show1(); 
    11         SportMan.show();
    12         s1.show();
    13     }
    14 }
    15 class SportMan{
    16     //实例变量(是随着对象的创建而被加载的)
    17     String name;
    18     int age;
    19     //类对象
    20      static String nation;
    21     public SportMan(String name, int age) {
    22         super();
    23         this.name = name;
    24         this.age = age;
    25         this.nation = "中国";
    26     }
    27     @Override
    28     public String toString() {
    29         return "SportMan [name=" + name + ", age=" + age + ", nation=" + nation
    30                 + "]";
    31     }
    32     public void show1(){
    33         System.out.println("我是一个普通方法");
    34         info();
    35         this.show();
    36         System.out.println("nation:"+nation);
    37     }
    38     public static void show(){
    39         //静态方法只能调用静态的属性,而不能调用一般属性
    40         info();
    41         //System.out.println("age:"+this.age)  这样写是错误的,生命周期不一样。
    42         System.out.println("nation:"+nation);
    43         System.out.println("我是一个类方法");
    44     }
    45     public static void info(){
    46         System.out.println("woshiyigejingtaidefangfa");
    47     }
    48     }
  • 相关阅读:
    第七章-方法区
    wchar_t 字符拼接
    C++获取appdata路径
    char * 、BSTR、long、wchar_t *、LPCWSTR、string、QString类型转换
    climits 与 符号常量
    Qt数据结构-QString二:QString的arg能不能像Python的format一样使用
    Qt数据结构-QString一:常用方法
    怎么查看摄像头的硬件ID
    jenkins提示使用java11版本
    Jenkins:the input device is not a TTY
  • 原文地址:https://www.cnblogs.com/Terminaling/p/4111783.html
Copyright © 2011-2022 走看看