|NO.Z.00042|——————————|BigDataEnd|——|Java&static关键字继承.V02|——|Java.v02|static关键字|基本概念|编程使用|

一、static关键字基本概念
### --- static关键字基本概念

~~~     ——>        使用static关键字修饰成员变量表示静态的含义,
~~~     ——>        此时成员变量由对象层级提升为类层级,也就是整个类只有一份并被所有对象共享,
~~~     ——>        该成员变量随着类的加载准备就绪,与是否创建对象无关。
~~~     ——>        static关键字修饰的成员可以使用引用.的方式访问,但推荐类名.的方式。
二、static关键字的作用
二、编程代码:验证static关键字修饰的静态成员(类成员) 是否被所有对象共享
### --- 编程代码

/*
    编程实现People类的封装
 */
public class People {
    
    // 1.私有化成员变量,使用private关键字修饰
    private String name;
    private int age;
    //private String country; // 隶属于对象层级,也就是每个对象都拥有独立的一份
    //public static String country; // 隶属于类层级,也就是整个类只有一份并且被所有对象共享
    private static String country;
    
    // 3.在构造方法中调用set方法进行合理值的判断
    public People() {}
    public People(String name, int age/*, String country*/) {
        setName(name);
        setAge(age);
        //setCountry(country);
    }
    
    // 2.提供公有的get和set方法,并在方法体中进行合理值的判断
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        if(age > 0 && age < 150) {
            this.age = age;
        } else {
            System.out.println("年龄不合理哦!!!");
        }
    }
    public static String getCountry() {
        return country;
    }
    public static void setCountry(String country) {
        //this.country = country;
        People.country = country;
    }
    
    public void show() {
        System.out.println("我是" + getName() + ",今年" + getAge() + "岁了,来自" + getCountry());
    }
}
三、编程代码
### --- 编程代码

/*
    编程实现People类的测试
 */
public class PeopleTest {
    
    public static void main(String[] args) {
        
        // 3.验证static关键字修饰的静态成员(类成员)是否与创建对象无关  类名.的方式 => 无关
        //System.out.println("获取到的国籍信息是:" + People.country); // null
        System.out.println("获取到的国籍信息是:" + People.getCountry()); // null
        
        // 1.使用有参方式构造两个People类型的对象并打印特征
        People p1 = new People("zhangfei", 30/*, "China"*/);
        p1.show(); // zhangfei 30 China
        
        People p2 = new People("guanyu", 35/*, "China"*/);
        p2.show(); // guanyu 35 China

        System.out.println("--------------------------------------------");
        // 2.验证static关键字修饰的静态成员(类成员) 是否被所有对象共享  => 共享
        //p1.country = "蜀国";
        p1.setCountry("蜀国");
        //System.out.println("第一个对象的国籍是:" + p1.country); // 蜀国
        //System.out.println("第二个对象的国籍是:" + p2.country); // 蜀国
        System.out.println("第一个对象的国籍是:" + p1.getCountry()); // 蜀国
        System.out.println("第二个对象的国籍是:" + p2.getCountry()); // 蜀国
        
        People p3 = new People();
        //System.out.println("第三个对象的国籍是:" + p3.country); // 蜀国
        System.out.println("第三个对象的国籍是:" + p3.getCountry()); // 蜀国
    }
}
四、编译打印
C:\Users\Administrator\Desktop>javac PeopleTest.java

C:\Users\Administrator\Desktop>java PeopleTest
获取到的国籍信息是:null
我是zhangfei,今年30岁了,来自null
我是guanyu,今年35岁了,来自null
--------------------------------------------
第一个对象的国籍是:蜀国
第二个对象的国籍是:蜀国
第三个对象的国籍是:蜀国

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(23)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示