Java超詳細(xì)講解類變量和類方法
1.static靜態(tài)變量
1.靜態(tài)變量被同一個(gè)類的所有對象共享
2.static類變量在類加載的時(shí)候就生成使用
static保存在class實(shí)例的尾部,在堆中
3.和C語言C++有點(diǎn)像
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+"個(gè)小孩加入了游戲");
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;
//定義一個(gè)變量count,是一個(gè)類變量(靜態(tài)變量)
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.類變量(靜態(tài)變量的訪問)
靜態(tài)變量的訪問權(quán)限和范圍和普通屬性是一樣的
package com.demo.static_;
import java.sql.SQLOutput;
public class visit_Static {
public static void main(String[] args) {
//1.類名.類變量名
//static類變量在類加載的時(shí)候就生成使用
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,則主函數(shù)中無法訪問,需借助類函數(shù)訪問
3.類方法
1.類方法也叫靜態(tài)方法
2.訪問修飾符+static+數(shù)據(jù)返回類型(){}
3.static+訪問修飾符+數(shù)據(jù)返回類型(){}
4.調(diào)用方法和類方法相同
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);
//輸出當(dāng)前收到的總學(xué)費(fèi)
stu.showfee();
}
}
class stu{
private String name;//普通成員
//定義一個(gè)靜態(tài)變量來累計(jì)學(xué)生的學(xué)費(fèi)
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;
}
//當(dāng)方法使用了static修飾后,該方法就是靜態(tài)方法
//靜態(tài)方法就可以訪問靜態(tài)變量
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("總學(xué)費(fèi)="+stu.fee);
}
}
detail
1.靜態(tài)方法只能訪問靜態(tài)成員,this/super都不允許在類方法使用
2.非靜態(tài)方法,可以訪問靜態(tài)成員和非靜態(tài)成員
3.都遵守訪問權(quán)限
到此這篇關(guān)于Java超詳細(xì)講解類變量和類方法的文章就介紹到這了,更多相關(guān)Java變量與方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Java8 parallelStream并發(fā)安全的深入講解
這篇文章主要給大家介紹了關(guān)于Java8 parallelStream并發(fā)安全的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
java中JsonObject與JsonArray轉(zhuǎn)換方法實(shí)例
在項(xiàng)目日常開發(fā)中常常會(huì)遇到JSONArray和JSONObject的轉(zhuǎn)換,很多公司剛?cè)肼毜男∶刃聲?huì)卡在這里,下面這篇文章主要給大家介紹了關(guān)于java中JsonObject與JsonArray轉(zhuǎn)換方法的相關(guān)資料,需要的朋友可以參考下2023-04-04
Eclipse中導(dǎo)出碼云上的項(xiàng)目方法(圖文教程)
下面小編就為大家?guī)硪黄狤clipse中導(dǎo)出碼云上的項(xiàng)目方法(圖文教程)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
使用Flyway進(jìn)行Java數(shù)據(jù)庫版本控制的操作指南
今天我們將深入探討如何使用Flyway進(jìn)行Java數(shù)據(jù)庫版本控制,Flyway是一個(gè)流行的數(shù)據(jù)庫遷移工具,用于管理和自動(dòng)化數(shù)據(jù)庫模式的演變,文中通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07
springboot實(shí)現(xiàn)string轉(zhuǎn)json json里面帶數(shù)組
這篇文章主要介紹了springboot實(shí)現(xiàn)string轉(zhuǎn)json json里面帶數(shù)組,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
淺談web服務(wù)器項(xiàng)目中靜態(tài)請求和動(dòng)態(tài)請求處理
這篇文章主要介紹了淺談web服務(wù)器項(xiàng)目中靜態(tài)請求和動(dòng)態(tài)請求處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
SpringBoot或SpringAI對接DeepSeek大模型的詳細(xì)步驟
這篇文章主要介紹了DeepSeek智能助手的使用方法和步驟,包括引入庫、配置環(huán)境變量和配置,文章詳細(xì)描述了流式請求和非流式請求的實(shí)現(xiàn)方式,需要的朋友可以參考下2025-02-02
springboot使用之多個(gè)filter的執(zhí)行順序以及配置方式
這篇文章主要介紹了springboot使用之多個(gè)filter的執(zhí)行順序以及配置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

