企業(yè)做網(wǎng)站的注意什么百度用戶服務(wù)中心官網(wǎng)
1.java學(xué)習(xí)記錄
1.方法的重載
重載換而言之其實(shí)就是函數(shù)名不變,但是其中的參數(shù)需要改變,可以三個(gè)方面改變(參數(shù)類型,參數(shù)順序,參數(shù)個(gè)數(shù)這三個(gè)方面入手,這樣可以運(yùn)用的)
但是:注意訪問修飾符或返回值設(shè)置不同,而參數(shù)設(shè)置相同時(shí),無法構(gòu)成方法的重載。
public class Test {public Test(){System.out.println("無參構(gòu)造方法");}public Test(int a){System.out.println("單參構(gòu)造方法 a:"+a);}public Test(int a, String b){System.out.println("無參構(gòu)造方法 a:"+a +"b:"+b);}public static void main(String[] args) {new Test();new Test(1);new Test(1,"1");}
}
2.封裝
4.1 什么是封裝
? ? ? ? 當(dāng)我們需要安全地訪問對(duì)象時(shí),例如限制給對(duì)象賦值的范圍(避免數(shù)據(jù)類型的不同或者數(shù)據(jù)范圍超出預(yù)計(jì)),我們就需要使用封裝技術(shù)。
? ? ? ? 封裝就是將類的某些信息隱藏在類內(nèi)部,不允許外部程序直接訪問,而是通過該類提供的方法(如set或者get)來實(shí)現(xiàn)對(duì)隱藏信息的操作和訪問。
方法封裝的主要優(yōu)點(diǎn)包括:
代碼抽象: 將復(fù)雜的操作和算法封裝在方法中,使得調(diào)用者只需要關(guān)注方法的用途,而無需關(guān)心具體的實(shí)現(xiàn)細(xì)節(jié)。
隱藏實(shí)現(xiàn)細(xì)節(jié): 封裝隱藏了方法的具體實(shí)現(xiàn),從而防止外部代碼直接訪問或修改內(nèi)部邏輯,提高了信息隱藏和安全性。
提高可讀性: 使用有意義的方法名和參數(shù),使代碼更易于理解和閱讀,降低了理解和維護(hù)代碼的難度。
提高可維護(hù)性: 修改方法的實(shí)現(xiàn)只需要在方法內(nèi)部進(jìn)行,不影響外部調(diào)用者的代碼,從而減少了代碼變更的影響范圍。
代碼重用: 封裝的方法可以在不同的地方重復(fù)使用,避免了代碼的重復(fù)編寫,提高了代碼的重用性。
用封裝的方法,計(jì)算圓的面積
class Circle {private double radius;public Circle(double r) {radius = r;}// 封裝的方法,計(jì)算圓的面積public double calculateArea() {return Math.PI * radius * radius;}
}public class Main {public static void main(String[] args) {Circle circle = new Circle(5.0);// 調(diào)用封裝的方法,計(jì)算圓的面積double area = circle.calculateArea();System.out.println("Circle Area: " + area);}
}
2.每日一題
最短路計(jì)數(shù)
#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
long long dis[1000006];
int vis[1000005];
int head[1000005];
long long ans[1000005];
struct edge
{int to;int w;int next;
}e[5000005];
struct node
{int dis,pos;bool operator <(const node &a)const{return a.dis>dis;}
};priority_queue <node> q;void addedge(int x,int y,int z)
{cnt++;e[cnt].to=y;e[cnt].w=z;e[cnt].next=head[x];head[x]=cnt;
};int main()
{cin>>n>>m;for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f;dis[1]=0;ans[1]=1;for(int i=1;i<=m;i++){int x,y;cin>>x>>y;addedge(x,y,1);addedge(y,x,1);}q.push((node){0-dis[1],1});while(!q.empty()){node tmp=q.top();q.pop();int pos=tmp.pos;int d=tmp.dis;if(vis[pos]==1)continue;vis[pos]==1;for(int i=head[pos];i!=0;i=e[i].next){if(dis[e[i].to]>dis[pos]+e[i].w){dis[e[i].to]=dis[pos]+e[i].w;ans[e[i].to]=ans[pos];q.push((node){0-dis[e[i].to],e[i].to});}else if(dis[e[i].to]==dis[pos]+e[i].w){ans[e[i].to]+=ans[pos];ans[e[i].to]%=100003;}}}for(int i=1;i<=n;i++){printf("%lld\n",ans[i]);}return 0;
}
?