面向对象 static关键字

来源:互联网 发布:淘宝介入退货退款流程 编辑:程序博客网 时间:2024/05/05 20:18

在JAVA中可以使用static声明属性或方法,因为在之前所讲解的属性和方法都属于非static的,这样一来,每个对象都占有各自的内容,如果现在希望一个属性被所有对象共同拥有,则可以将其声明成static类型,
声明成static类型的属性和方法之后,此属性和方法称为类方法,可以由类名称直接调用。

1.1使用static关键字声明属性

class Person{    String name ; //定义String属性,暂不疯转    int age ;     //定义int属性,暂不封装    static String country = "中国" ; //使用static声明属性    public Person(String name,int age)    {        this.name = name ;        this.age = age ;    }    public void info()  //声明方法,取得信息    {        System.out.println("姓名:"+this.name+"年龄:"+this.age+"城市:"+country) ;    }}public class StaticDemo01{    public static void main(String[] args)    {        Person per1 = new Person("张三",20) ;        Person per2 = new Person("李四",21) ;        Person per3 = new Person("王五",22) ;        System.out.println("----修改之前-----") ;        per1.info();        per2.info();        per3.info();        per1.country = "美国" ;       //使用一个对象修改static属性        System.out.println("----修改之后----") ;        per1.info() ;        per2.info() ;        per3.info() ;    }}

修改一个对象中的country属性,则其他对象中的country属性也改变,证明此属性是所有对象共享的。
每个对象都拥有各自的堆栈空间,堆内存空间中保存每个对象的各自属性,但是static属性保存在全局数据区中,所有的对象都指向全局数据区中的一个内容,所以,当一个对象的属性修改之后,所有的对象属性都会修改。
JAVA中有多少内存区域:
A,栈内存:可以保存对象的名称(保存访问堆内存的地址)
B,堆内存:保存每个对象的具体属性
C,全局数据区:保存static类型的属性
D,全局代码区:保存所有方法的定义
一般在调用static属性的时候最好使用类名称直接调用,采用”类名称.属性”的形式调用。

class Person{    String name ; //定义String属性,暂不疯转    int age ;     //定义int属性,暂不封装    static String country = "中国" ; //使用static声明属性    public Person(String name,int age)    {        this.name = name ;        this.age = age ;    }    public void info()  //声明方法,取得信息    {        System.out.println("姓名:"+this.name+"年龄:"+this.age+"城市:"+country) ;    }}public class StaticDemo01{    public static void main(String[] args)    {        Person per1 = new Person("张三",20) ;        Person per2 = new Person("李四",21) ;        Person per3 = new Person("王五",22) ;        System.out.println("----修改之前-----") ;        per1.info();        per2.info();        per3.info();        Person.country = "美国" ;     //使用一个类名称.属性修改属性        System.out.println("----修改之后----") ;        per1.info() ;        per2.info() ;        per3.info() ;    }}

1.2 使用static关键字声明方法

如果一个方法使用static关键字声明,则此方法可以直接使用类名称直接调用

class Person{    private String name ; //定义String属性,暂不疯转    private int age ;     //定义int属性,暂不封装    private static String country = "中国" ; //使用static声明属性    public static void setCountry(String c)  //此方法可以直接由类名称直接调用    {        country = c ;    }    public static String getCountry()    {        return country ;    }    public Person(String name,int age)    {        this.name = name ;        this.age = age ;    }    public void info()  //声明方法,取得信息    {        System.out.println("姓名:"+this.name+"年龄:"+this.age+"城市:"+country) ;    }}public class StaticDemo02{    public static void main(String[] args)    {        Person per1 = new Person("张三",20) ;        Person per2 = new Person("李四",21) ;        Person per3 = new Person("王五",22) ;        System.out.println("----修改之前-----") ;        per1.info();        per2.info();        per3.info();        Person.setCountry("美国" );       //使用一个类名称.属性修改属性        System.out.println("----修改之后----") ;        per1.info() ;        per2.info() ;        per3.info() ;    }}

注意点
使用static方法,不能调用非static的属性和方法。

0 0
原创粉丝点击