Static关键字

一.Static关键字:代表静态的,可用于修饰 属性,方法,代码块,内部类
1.static修饰的属性(静态变量或类变量)
①随着类的加载而加载,随着类的消失而消失。(生命周期最长)
②static 修饰的属性被该类所有的对象所共享
③一旦某一个对象修改了该属性值,其他对象的该属性值也会随之改变

④静态变量的存在是优先于对象的
⑤可以通过"类名.类变量"的方式使用

  2.static修饰的方法(静态方法或类方法)
①随着类的加载而加载
②静态方法的存在优先于对象
③可用通过"类名.类方法"的方式调用
④静态方法中不能使用非静态成员,非静态方法中可以使用静态成员
(静态方法中能使用静态成员)
⑤静态方法中不能使用this和super

public class StaticTest {
    public static void main(String[] args) {
       /* Chinese p1=new Chinese("张三",15,"中国");

        Chinese p2=new Chinese("李四",20,"中国");
        p1.setNation("唐朝");//一旦某一个对象修改了该属性值,其他对象的该属性值也会随之改变
        System.out.println(p1);
        System.out.println(p2);*/
        
    }
}
class Chinese{
    private String name;
    private int age;

    private static String nation;//描述国籍
    public Chinese() {
    }
    public Chinese(String name, int age, String nation) {
        this.name = name;
        this.age = age;
        this.nation = nation;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }

    public String getNation() {
        return nation;
    }

    @Override
    public String toString() {
        return "Chinese{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", nation='" + nation + '\'' +
                '}';
    }
}

 

 static 修饰的属性被该类所有的对象所共享

 一旦某一个对象修改了该属性值,其他对象的该属性值也会随之改变

 2.static修饰的方法(静态方法或类方法)
①随着类的加载而加载
②静态方法的存在优先于对象
③可用通过"类名.类方法"的方式调用
④静态方法中不能使用非静态成员,非静态方法中可以使用静态成员
(静态方法中能使用静态成员)
⑤静态方法中不能使用this和super

public class StaticTest {
    public static void main(String[] args) {
       /* Chinese p1=new Chinese("张三",15,"中国");

        Chinese p2=new Chinese("李四",20,"中国");
        p1.setNation("唐朝");//一旦某一个对象修改了该属性值,其他对象的该属性值也会随之改变
        System.out.println(p1);
        System.out.println(p2);*/

        Chinese.nation="中国";
        System.out.println(Chinese.nation);
        Chinese.show();
    }
}
class Chinese{
    private String name;
    private int age;

    /*private*/ static String nation;//描述国籍
    public Chinese() {
    }
    public Chinese(String name, int age, String nation) {
        this.name = name;
        this.age = age;
        this.nation = nation;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }

    public String getNation() {
        return nation;
    }
    public static void show(){
        System.out.println("这是一个静态方法"+nation);
    }

    @Override
    public String toString() {
        return "Chinese{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", nation='" + nation + '\'' +
                '}';
    }
}

 

 

Exer:

package Static;

public class TestFrock {
    public static void main(String[] args) {
        /*System.out.println(Frock.getNextNum());
        System.out.println(Frock.getNextNum());*/
        Frock f1=new Frock();
        Frock f2=new Frock();
        Frock f3 =new Frock();
        System.out.println(f1.getSerialNumber());
        System.out.println(f2.getSerialNumber());
        System.out.println(f3.getSerialNumber());
    }
}
class Frock{
    private static int currentNum=100000;
    private int serialNumber;//序列号
    public Frock(){//构造器
        this.serialNumber=getNextNum();//自动生成衣服的唯一序列号
    }

    public static int getNextNum(){
        return currentNum+=100;
    }

    public void setSerialNumber(int serialNumber) {
        this.serialNumber = serialNumber;
    }
    public int getSerialNumber() {
        return serialNumber;
    }
}

 

  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值