黑马程序员------毕老师视频笔记第六天------面向对象(static关键字)

来源:互联网 发布:人为什么追求卓越 知乎 编辑:程序博客网 时间:2024/05/18 18:42

---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------

static关键字:用于修饰成员(成员变量、成员函数)

被修饰后的成员具备以下特点:

①  随着类的加载而加载,随着类的消失而消失

说明它的生命周期最长,不建议定义太多的静态成员

②  优先于对象存在

对象是先存在的,对象是后存在的

③  被所有对象所共享

④  可以直接被类名调用

使用注意:

①  静态方法只能访问静态成员

②  静态方法中不可以写this、super关键字

③  主函数是静态的

class Person{String name;//成员变量,实例变量String country = "cn"; public void show (){System.out.println(name+"::"+country);}}class Demo{public static void main (String [] args){Person p = new Person();p.name = "lisi";p.show();}}

类的对象中有共同的数据 cn ,占有内存太多空间,能不能把共同的数据取出来共享呢?

 

静态:static

用法:是一个修饰符,只能修饰成员(成员变量、成员函数)

static String country = "cn";             //静态的成员变量,类变量

其他代码没有变,但是内存节约了

 

当成员被static 修饰以后,成员就多了一种调用方式,除了可以被对象调用以外,还可以直接被类名调用,方法时类名.静态成员

 

Person.country;

 

特有内容随着对象在堆中存储,共有内容随着类的加载在方法区加载。

 

方法区(共享区、数据区),放类中的方法show(),静态数据country

 

分析时,注意分析什么数据时对象共有的,什么数据是对象特有的

 

类变量和实例变量的区别:

①存放位置,类变量存在于方法区中,实例变量随着对象的建立存在于堆内存中

②生命周期,类变量的生命周期最长,随着类的消失而消失,实例变量随着对象的消失而消失

 

方法用static修饰的时候,只能调用静态成员

而非静态方法也可以访问静态成员(谁先存在)

 

静态有利有弊

利:对对象的共享数据进行单独空间的存储,节省空间,没有必要每个对象都存储一份,可以直接被类名调用

弊:生命周期过长,访问出现局限性(静态虽好,只能访问静态)

 

主函数是静态的

class Person{String name;static String country = "cn";static public void show (){System.out.println("::"+country);}}class Demo{public static void main (String [] args){System.out.println(Person.country);Person.show();}}

什么什么时候使用静态?

要从两方面入手:(static修饰的内容有成员变量、成员函数)

什么时候定义静态变量(类变量)呢?

         当对象中出现共享数据时,该数据被static修饰,对象中的特有数据要定义成非静态

什么时候定义静态函数呢?

         开发设计中很重要,当功能内部没有访问到非静态数据(对象的特有数据),该功能可以定义为静态的。

 

静态的应用---工具类

class ArrayTool{         publicstatic int getMax(int [] arr)         {                   intmax = 0;                   for(int x=0; x<arr.length ; x++ )                   {                            if(arr[x] > arr[max])                            {                                     max= x;                            }                   }                   returnarr[max];         }         publicstatic int getMin(int [] arr)         {                   intmin = 0;                   for(int x = 0; x<arr.length ; x++ )                   {                            if(arr[x] < arr[min])                            {                                     min= x;                            }                   }                   returnarr[min];         }         publicstatic void selectSort(int [] arr)         {                   for(int x = 0; x<arr.length-1 ; x++ )                   {                            for(int y = x+1; y<arr.length ; y++ )                            {                                     if(arr[x] > arr[y])                                     {                                               swap(arr,x,y);                                     }                            }                   }         }         publicstatic void bubbleSort(int [] arr)         {                   for(int x = 0; x<arr.length-1 ; x++ )                   {                            for(int y = 0; y<arr.length - x - 1 ; y++ )                            {                                     if(arr[y]>arr[y+1])                                     {                                               swap(arr,y,y+1);                                     }                            }                   }         }         //本类内使用         privatestatic void swap(int [] arr, int x, int y)         {                   inttemp = arr[x];                   arr[x]= arr[y];                   arr[y]= temp;         }         publicstatic void printArray(int [] arr)         {                   for(int x = 0; x<arr.length ; x++ )                   {                            System.out.print(arr[x]+",");                   }                   System.out.println();         }}class ArrayToolDemo{         publicstatic void main(String [] args)         {                   int[]arr = {1,8,4,3};                   ArrayTool.printArray(arr);                   intmax = ArrayTool.getMax(arr);                   System.out.println(max);                   intmin = ArrayTool.getMin(arr);                   System.out.println(min);                   ArrayTool.selectSort(arr);                   ArrayTool.printArray(arr);                   int[]arr1 = {3,2,5,7};                   ArrayTool.printArray(arr1);                   ArrayTool.bubbleSort(arr1);                   ArrayTool.printArray(arr1);         }}



静态代码块

格式:

static

{

         语句;

}

特点:随着类的加载而执行,只执行一次。用于给类进行初始化。

 

class StaticCode{static{System.out.println("a");}}class StaticCodeDemo{static{System.out.println("b");}public static void main (String [] args){new StaticCode();new StaticCode();//类已经进内存了,不打印aSystem.out.println("c");}static{System.out.println("d");}}

---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------

0 0
原创粉丝点击