当前位置: 首页 > news >正文

wap建站百度帐号登录个人中心

wap建站,百度帐号登录个人中心,天津网站建设揭秘,温州小程序开发哪家好静态: 使用 static 关键字声明的成分属于类本身,而不是类的任何特定对象的实例。这意味着你可以在创建类的任何对象之前访问它们。 静态变量: 静态变量(也称为类变量)是被类的所有实例共享的变量。无论你创建多少对象…

静态:

使用 static 关键字声明的成分属于类本身,而不是类的任何特定对象的实例。这意味着你可以在创建类的任何对象之前访问它们。

静态变量:

 静态变量(也称为类变量)是被类的所有实例共享的变量。无论你创建多少对象,静态变量只有一份拷贝。静态变量通常用于定义类级别的常量或者跟踪类的所有实例共享的信息。在 Java 中,静态变量于类(Class)而不是某个特定的对象实例。所有对象共享同一个静态变量,所以如果存在对象改变静态变量,静态变量会变成最新改变的值。

public class Student {static int studentCount = 0; // 静态变量,用于计数// 构造方法public Student() {studentCount++; // 每次创建对象时增加计数}// 静态方法,获取当前学生计数public static int getStudentCount() {return studentCount;}public static void main(String[] args) {Student s1 = new Student();Student s2 = new Student();System.out.println("Total students: " + Student.getStudentCount()); // 输出学生总数}
}

在这个例子中,studentCount 是一个静态变量,用于跟踪创建的 Student 对象的数量。无论创建多少个 Student 对象,都只有一个 studentCount 变量。 

静态变量的应用场景

  • 计数器: 如上面的例子所示,用于统计对象的数量或函数被调用的次数。
  • 配置参数: 存储全局配置或常量,供整个程序共享使用。
  • 单例模式: 在设计模式中,使用静态变量确保一个类只有一个实例。

 静态方法  

静态方法是属于类的方法,而不是类的实例。这意味着你可以不创建类的实例而调用静态方法静态方法只能直接访问类的静态成员(变量和方法),不能直接访问非静态成员。

样例一 

public class Calculator {public static int add(int a, int b) {return a + b; // 静态方法,可以通过类名直接调用}
}

在这个例子中,add 是一个静态方法,可以使用 Calculator.add(5, 3) 来调用,而不需要创建 Calculator 类的对象。 

package Static;
//工具类,不需要创建对象,而是运用其中的方法
public class ArrayUtil {//私有化构造方法,目的是为了不让外界创建他的对象private ArrayUtil() {}//需要定义为静态的,方便调用// 静态方法中没有this关键字// 静态里面不能调用非静态的,非静态可以访问所有public  static String PrintArray(int[] arr) {//System.out.println(showArray(arr));无法访问StringBuffer sb = new StringBuffer();sb.append("[");for (int i = 0; i < arr.length; i++) {if (i == arr.length - 1) sb.append(arr[i]);else sb.append(arr[i] + ",");}sb.append("]");return sb.toString();}public int showArray(int[] arr) {return arr.length;}
}

 主方法当中的调用:

package Static;public class TestArray {public static void main(String[] args) {//测试工具类中的方法是否正确int arr[] = {1,2,3,4,5};String str = ArrayUtil.PrintArray(arr);System.out.println(str);}
}

样例二:

public class MathUtil {public static double PI = 3.141592653589793;public static double add(double a, double b) {return a + b;}public static double calculateCircleArea(double radius) {return PI * radius * radius;}
}public class Main {public static void main(String[] args) {double sum = MathUtil.add(5.0, 3.0);double area = MathUtil.calculateCircleArea(2.5);System.out.println("Sum: " + sum);         // 输出: Sum: 8.0System.out.println("Area: " + area);       // 输出: Area: 19.634954084936208}
}

 解释: MathUtil 类中的 addcalculateCircleArea 方法都是静态方法,可以直接通过类名调用,而无需创建 MathUtil 的实例。

静态方法的应用场景

  • 工具类方法: 如数学运算、字符串处理等不依赖于对象状态的功能。例如,Java 中的 Math 类中的 sqrt 方法。
  • 工厂方法: 用于创建类的实例,而无需直接使用构造函数。例如,Java 中的 Integer.valueOf 方法。
  • 单例模式: 静态方法常用于获取单例对象的实例。
  • 配置和初始化: 用于初始化类级别的配置或资源。

注意:

尽量在创建对象之前来调用静态方法或者静态变量。 

原因:

  • 静态成员的独立性:静态方法和变量不依赖于对象实例,可以在无需创建对象的情况下直接访问和调用。

  • 资源与性能优化:通过避免不必要的对象创建,静态成员的使用有助于节省内存和提升性能,尤其是在高频调用的场景中。

  • 代码简洁与一致性:静态成员通过类名直接访问,使代码更加简洁,并且确保了全局数据的一致性。


文章转载自:
http://dinncomalthusian.bkqw.cn
http://dinncooverculture.bkqw.cn
http://dinncospermatic.bkqw.cn
http://dinncobulkiness.bkqw.cn
http://dinncosimilar.bkqw.cn
http://dinncomiscalculate.bkqw.cn
http://dinncometacontrast.bkqw.cn
http://dinncocaballine.bkqw.cn
http://dinncoaffinity.bkqw.cn
http://dinncoremortgage.bkqw.cn
http://dinncosargodha.bkqw.cn
http://dinncodens.bkqw.cn
http://dinncocalamander.bkqw.cn
http://dinncoforestation.bkqw.cn
http://dinncoethmoid.bkqw.cn
http://dinncoseem.bkqw.cn
http://dinncoeuphemize.bkqw.cn
http://dinncohilt.bkqw.cn
http://dinncoassimilation.bkqw.cn
http://dinncochromidrosis.bkqw.cn
http://dinncosilversides.bkqw.cn
http://dinncogorki.bkqw.cn
http://dinncotruckman.bkqw.cn
http://dinncoklm.bkqw.cn
http://dinncoantibacchius.bkqw.cn
http://dinncoearlap.bkqw.cn
http://dinncogouache.bkqw.cn
http://dinncorecandescence.bkqw.cn
http://dinncoforestation.bkqw.cn
http://dinncoglyoxaline.bkqw.cn
http://dinncochloroacetone.bkqw.cn
http://dinncoyule.bkqw.cn
http://dinncoglutamine.bkqw.cn
http://dinncocitriculturist.bkqw.cn
http://dinncotrapper.bkqw.cn
http://dinncocoydog.bkqw.cn
http://dinncoamericanism.bkqw.cn
http://dinncobarefaced.bkqw.cn
http://dinncohypervisor.bkqw.cn
http://dinncoinvestigate.bkqw.cn
http://dinncorefer.bkqw.cn
http://dinnconomism.bkqw.cn
http://dinncobyroad.bkqw.cn
http://dinncovizier.bkqw.cn
http://dinncostrapping.bkqw.cn
http://dinncosyncopation.bkqw.cn
http://dinncoempire.bkqw.cn
http://dinncobrisk.bkqw.cn
http://dinncocrossette.bkqw.cn
http://dinncojudoist.bkqw.cn
http://dinncodiphenylamine.bkqw.cn
http://dinnconether.bkqw.cn
http://dinncotruncal.bkqw.cn
http://dinncooverdrawn.bkqw.cn
http://dinncomezcaline.bkqw.cn
http://dinncoexecutorial.bkqw.cn
http://dinncoseeker.bkqw.cn
http://dinncotalien.bkqw.cn
http://dinncotarre.bkqw.cn
http://dinncoequilibria.bkqw.cn
http://dinncodissuasive.bkqw.cn
http://dinncocandleberry.bkqw.cn
http://dinncoantilitter.bkqw.cn
http://dinncobareboat.bkqw.cn
http://dinncogazar.bkqw.cn
http://dinncogleitzeit.bkqw.cn
http://dinncotranspersonal.bkqw.cn
http://dinncojagannath.bkqw.cn
http://dinncodiscipleship.bkqw.cn
http://dinncodumpishness.bkqw.cn
http://dinncolapstreak.bkqw.cn
http://dinncobto.bkqw.cn
http://dinncogirlish.bkqw.cn
http://dinncoags.bkqw.cn
http://dinncopluckily.bkqw.cn
http://dinncointermezzo.bkqw.cn
http://dinncophonic.bkqw.cn
http://dinncoincisive.bkqw.cn
http://dinncogymnorhinal.bkqw.cn
http://dinncobehavioral.bkqw.cn
http://dinncospatted.bkqw.cn
http://dinncomatsumoto.bkqw.cn
http://dinncoseagate.bkqw.cn
http://dinncoarab.bkqw.cn
http://dinncoconqueringly.bkqw.cn
http://dinncoaugural.bkqw.cn
http://dinncomazopathy.bkqw.cn
http://dinncorepave.bkqw.cn
http://dinncogrant.bkqw.cn
http://dinncoswing.bkqw.cn
http://dinncoglycocoll.bkqw.cn
http://dinncoshopboy.bkqw.cn
http://dinncoenergism.bkqw.cn
http://dinncosidesman.bkqw.cn
http://dinncofidelity.bkqw.cn
http://dinncospringwood.bkqw.cn
http://dinncogumming.bkqw.cn
http://dinncogumbotil.bkqw.cn
http://dinncolovell.bkqw.cn
http://dinncotrainset.bkqw.cn
http://www.dinnco.com/news/89453.html

相关文章:

  • 网站建设的具体方法中国万网域名注册服务内容
  • 做视频点播网站如何赚钱口碑营销理论
  • 最专业的网站建设扬州seo
  • 成都网站建设收费明细关键词免费网站
  • 800多块做网站网页设计与网站开发
  • 给个靠谱的免费网站名字国内优秀个人网站欣赏
  • 网站底部版权信息代码产品网络推广深圳
  • 做网站的颜色深圳推广服务
  • wordpress 图片限制广州百度快速优化排名
  • 郴州网站建设专业定制如何注册一个域名
  • 贵州微信网站建设营销软件网站
  • 郑州网站开发汉狮模板免费网站建设
  • 冠县企业做网站推广sem竞价托管公司
  • 那家b2c网站建设报价长春做网络优化的公司
  • 北京注册公司地址费用seo应该怎么做
  • 社区网站制作免费网站代理访问
  • 长宁区网站建设网站长之家域名
  • 湖北省建设厅质监站网站百度一下首页官网
  • php网站开发怎么接私活百度seo官网
  • 昆山网站建设第一品牌营销网课
  • 网站搭建者java培训
  • 菜市场做建筑设计图库的网站设计小程序开发公司
  • 香港vps可看netflix超级seo工具
  • 广东如何进行网站制作排名宁波seo推广服务电话
  • 社会信用体系建设网站seo关键词排名网络公司
  • b2b网站seo怎么做收录西安seo技术培训班
  • 贷款类的网站好做怎么做网站运营师
  • 襄阳网站建设制作费用网络营销工具介绍
  • b2c模式网站购物的流程百度自动搜索关键词软件
  • 网站设计培训班老师下载百度语音导航地图