您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java訪問權(quán)限原理與用法分析”,在日常操作中,相信很多人在Java訪問權(quán)限原理與用法分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java訪問權(quán)限原理與用法分析”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
進行初始化,解決了多個構(gòu)造器重載,構(gòu)造器參數(shù)過多記不住的情況。
package day7;//聲明一個程序包class Employee{ private String name; private int no; private int age; private String sex; private String address; //alt + shift + s public int getNo() { return no; }/* public Employee() { }*/ public Employee setNo(int no) { this.no = no; return this; } public String getName() { return name; } public Employee setName(String name) { this.name = name; return this; } public int getAge() { return age; } public Employee setAge(int age) { this.age = age; return this; } public String getSex() { return sex; } public Employee setSex(String sex) { this.sex = sex; return this; } public String getAddress() { return address; } public Employee setAddress(String address) { this.address = address; return this; }/* public Employee(String name, int no, int age, String sex, String address) { this.name = name; this.no = no; this.age = age; this.sex = sex; this.address = address; }*/ public void show() { System.out.println(no+","+name+","+age+","+sex+","+address); }}public class TestEmployee { public static void main(String[] args) {/* Employee tom = new Employee("Tom",12,33,"男","上海"); tom.show();*/ /*構(gòu)造者模式思想 :進行 初始化。解決了 多個構(gòu)造器重載 ,構(gòu)造器 參數(shù)過多 記不住的情況*/ Employee tom = new Employee().setNo(11).setAddress("北京").setAge(33).setSex("男").setName("Tom"); tom.show(); }}
package day7;//聲明一個程序包,必須放在文件的第一行
package 父包[.子包.······];包名:小寫字母;通常是域名反轉(zhuǎn).部門名.項目名
1.管理類和接口2.防止命名沖突3.封裝,通過權(quán)限的控制,更好的
1.導入程序包
import b.Exam2;//導入b包下的類型Exam2import b.*;//不能導入子包import b.c.Exam3;//導入子包下的類型
2.用完全限定命名的方式
b.Exam2 e2 = new b.Exam2();
注意:兩個包下有相同的類型,必須用完全限定命名的方式進行。
Java將類成員的可見度分為四個種類:
創(chuàng)建類的時候只有兩種:public和默認
static是一個修飾符應用:可以用于修飾屬性,方法,塊,類靜態(tài)變量
class 類名{//靜態(tài)成員變量,類變量public static 數(shù)據(jù)類型 變量名;}
package day7;class Child{static int count;}public class TestChild {public static void main(String[] args) {Child a = new Child();Child b = new Child();//count被所有對象共享a.count ++;b.count ++;System.out.println(a.count);System.out.println(b.count);}}
靜態(tài)變量隨著類的創(chuàng)建的而存在,優(yōu)先于對象存在。
屬于類的,被所有對象所共享,優(yōu)先于對象而存在的。使用
類名.靜態(tài)變量名對象名.靜態(tài)變量名//少用,容易混淆
1.靜態(tài):類加載的時候就加載了,就創(chuàng)建了,就分配空間默認初始化了
實例:對象創(chuàng)建的時候,才能創(chuàng)建;2.靜態(tài):屬于類的,存在于方法區(qū)中的。實例:屬于對象。存在于堆中。3.靜態(tài):聲明周期很長,類銷毀的時候,才回釋放。實例:對象銷毀,會釋放。
當數(shù)據(jù)共享時,使用。當不需要對象,或無法創(chuàng)建對象時使用。
在類中定義
static{ 作用:初始化類的,給類變量初始化,靜態(tài)變量}
定義:在方法中定義
public void show(){ { 局部代碼塊 作用:用來控制局部變量生命周期和使用范圍。 }}
靜態(tài)方法中只能訪問靜態(tài)成員。靜態(tài)方法中不能使用this,super關(guān)鍵字。super可能訪問到非靜態(tài)的成員。
1.靜態(tài):屬于類,用類名直接調(diào)用實例: 屬于對象調(diào)用。2.靜態(tài):只能直接訪問靜態(tài)成員(靜態(tài)變量,靜態(tài)方法)實例:可以直接訪問靜態(tài)的和非靜態(tài)的3.靜態(tài):不能使用this,super。實例:可以使用this,super。
當不用創(chuàng)建對象訪問,形式簡單或者不能創(chuàng)建對象,那么就要用靜態(tài)的方法了
導入的是類中的靜態(tài)成員,導入之后可以直接使用。
格式
import static 包名.類名.靜態(tài)變量名(方法);
對類只能創(chuàng)建一個對象
類加載時,靜態(tài)變量就存儲了一個對象
package day7;class Window{ private static Window win = new Window(); private Window() { } public static Window getInstance() { return win; }}public class TestWindow { public static void main(String[] args) { Window win = Window.getInstance(); Window win1 = Window.getInstance(); System.out.println(win); System.out.println(win1); }}
輸出結(jié)果
day7.Window@7852e922day7.Window@7852e922
類加載時,沒有指定對象,只有在應用的時候才去創(chuàng)建對象,多線程的環(huán)境時,推薦使用餓漢式,因為是線程安全的。
package day7;class Window{ private static Window win = null; private Window() {}public static Window getInstance() { if(win == null) { win = new Window(); } return win; }}public class TestWindow {public static void main(String[] args) {Window win = Window.getInstance();Window win1 = Window.getInstance();System.out.println(win);System.out.println(win1);}}
返回結(jié)果
day7.Window@7852e922day7.Window@7852e922
常用方法
package day7;public class TestMath {public static void main(String[] args) {// MathSystem.out.println(Math.abs(‐33.4));//33.4//大于等于44.6的最小整數(shù)‐》doubleSystem.out.println(Math.ceil(44.6));//45.0//小于等于44.6的最大整數(shù)‐》doubleSystem.out.println(Math.floor(44.6));//44.0//四舍五入為一個long數(shù)字System.out.println(Math.round(44.6));//45//求幾次方的值System.out.println(Math.pow(3,2));//9.0//double [0.0,1.0)double n = Math.random();System.out.println(n);//1‐10//[最小值,最大值]//(int)(Math.random()*(最大值‐最小值+1)+最小值)int num = (int)(Math.random()*(10‐1+1)+1);System.out.println(num);}}
Random rand1 = new Random(11);//11為隨機種子System.out.println(rand1.nextDouble());Random rand2 = new Random(11);//System.out.println(rand2.nextDouble());
隨機種子相同時,相同隨機次數(shù)輸出結(jié)果相同。
Random rand3 = new Random(11);//int范圍內(nèi)的整數(shù)System.out.println(rand3.nextInt());//[0,5)System.out.println(rand3.nextInt(5));
到此,關(guān)于“Java訪問權(quán)限原理與用法分析”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。