Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of Java Class variables and Class methods

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

本篇内容介绍了"Java类变量和类方法实例分析"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1.static静态变量

1.静态变量被同一个类的所有对象共享

2.static类变量在类加载的时候就生成使用

static保存在class实例的尾部,在堆中

3.和C语言C++有点像

package com.demo.static_;import java.sql.SQLOutput;public class childgeme { public static void main(String[] args) { Child ch01=new Child("牛牛牛"); ch01.join(); ch01.count++; Child ch02=new Child("马马马"); ch02.join(); ch02.count++; Child ch03=new Child("猪猪猪"); ch03.join(); ch03.count++; System.out.println("共有"+Child.count+"个小孩加入了游戏"); System.out.println("ch01.count="+ch01.count); System.out.println("ch02.count="+ch02.count); System.out.println("ch03.count="+ch03.count); }}class Child{ private String name; //定义一个变量count,是一个类变量(静态变量) public static int count=0; public Child(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public static int getCount() { return count; } public static void setCount(int count) { Child.count = count; } public void join(){ System.out.println(this.name+"加入了游戏"); }}

2.类变量(静态变量的访问)

静态变量的访问权限和范围和普通属性是一样的

package com.demo.static_;import java.sql.SQLOutput;public class visit_Static { public static void main(String[] args) { //1.类名.类变量名 //static类变量在类加载的时候就生成使用 System.out.println("A.name="+A.name); System.out.println("A.getAge()="+A.getAge()); //2.类对象.类变量名 A a=new A(); System.out.println("a.name="+a.name); System.out.println("a.getAge()="+a.getAge()); }}class A{ //类变量 public static String name="Demo龙"; private static int age=99; public static int getAge() { return age; } public static void setAge(int age) { A.age = age; }}

若类变量是private,则主函数中无法访问,需借助类函数访问

3.类方法

1.类方法也叫静态方法

2.访问修饰符+static+数据返回类型(){}

3.static+访问修饰符+数据返回类型(){}

4.调用方法和类方法相同

package com.demo.static_;public class static_method { public static void main(String[] args) { stu a01=new stu("小虎"); //stu.sumfee(); a01.sumfee(150); stu a02=new stu("小龙"); a02.sumfee(250); //stu.sumfee(); stu a03=new stu("小猪"); stu.sumfee(199); //输出当前收到的总学费 stu.showfee(); }}class stu{ private String name;//普通成员 //定义一个静态变量来累计学生的学费 private static double fee=0; public stu(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } //当方法使用了static修饰后,该方法就是静态方法 //静态方法就可以访问静态变量 public static double getFee() { return fee; } public static void setFee(double fee) { stu.fee = fee; } public static void sumfee(double fee){ stu.fee+=fee; } public static void showfee(){ System.out.println("总学费="+stu.fee); }}

detail

1.静态方法只能访问静态成员,this/super都不允许在类方法使用

2.非静态方法,可以访问静态成员和非静态成员

3.都遵守访问权限

"Java类变量和类方法实例分析"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report