java中訪問修飾符_Java中的非訪問修飾符是什么?

java中訪問修飾符

Java非訪問修飾符 (Java non access modifiers)

We have 7 non-access modifiers in Java. The name of these non-access modifiers are given below,

Java中有7個非訪問修飾符 。 這些非訪問修飾符的名稱如下所示:

  1. native

    本機

  2. synchronized

    已同步

  3. transient

    短暫的

  4. volatile

    易揮發的

  5. final

    最后

  6. abstract

    抽象

  7. static

    靜態的

We will learn all the non access modifiers one by one...

我們將一一學習所有非訪問修飾符...

1)本地的 (1) native)

  • "native" is a keyword which is introduced in java.

    “ native”是在Java中引入的關鍵字。

  • "native" is the modifier applicable for methods only but it is not applicable for variable and classes.

    “本機”是僅適用于方法的修飾符,但不適用于變量和類。

  • The native methods are implemented in some other language like C, C++, etc.

    本機方法以其他一些語言(如C,C ++等)實現。

  • The purpose of the native method is to improve the performance of the system.

    本機方法的目的是提高系統性能。

  • We know that the implementation of native methods is available in other languages so we don't need to care about the implementation.

    我們知道本機方法的實現在其他語言中也可用,因此我們不需要關心實現。

Example: We will see the way of writing native methods

示例:我們將看到編寫本機方法的方式

class Native {
static {
// Load Native Library
System.loadLibrary("native library");
}
// Native Method Declaration
public native void display();
}
class Main {
public static void main(String[] args) {
Native native = new Native();
native.display();
}
}

2)同步 (2) synchronized)

  • "synchronized" is the keyword applicable for methods and block.

    “已同步”是適用于方法和塊的關鍵字。

  • "synchronized" keyword is not applicable for classes and variables.

    “ synchronized”關鍵字不適用于類和變量。

  • "synchronized" keyword is useful for multithreading if we declare a method as synchronized then at a time only one thread is allowed to operate on an object.

    如果我們將一個方法聲明為“ synchronized”,那么“ synchronized”關鍵字對于多線程很有用,那么一次只能在一個對象上運行一個線程。

Example:

例:

class SynchronizedDisplay {
public synchronized void display(String msg) {
for (int i = 0; i < 2; ++i) {
System.out.println(msg);
try {
Thread.sleep(500);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
}
class MyThread extends Thread {
SynchronizedDisplay sd;
MyThread(SynchronizedDisplay sd) {
this.sd = sd;
}
public void run() {
sd.display("hi");
}
}
class SynchronizedClass {
public static void main(String[] args) {
SynchronizedDisplay sd1 = new SynchronizedDisplay();
MyThread mt1 = new MyThread(sd1);
mt1.start();
MyThread mt2 = new MyThread(sd1);
mt2.start();
}
}

Output

輸出量

E:\Programs>javac SynchronizedClass.java
E:\Programs>java SynchronizedClass
hi
hi
hi
hi

3)瞬態 (3) transient)

  • "transient" is a keyword introduced in java.

    “ transient”是Java中引入的關鍵字。

  • "transient" is the modifier applicable only for variables.

    “瞬態”是僅適用于變量的修飾符。

  • "transient" is the modifier not applicable for classes and methods.

    “瞬態”是不適用于類和方法的修飾符。

  • "transient" is useful for serialization because at the time of serialization if we don't want to save the value of the variable to meet some security constraints.

    “瞬態”對于序列化很有用,因為在序列化時,如果我們不想保存變量的值以滿足某些安全性約束。

Example:

例:

Let suppose we have a class named Transient in that class we have two-three data member fname (first name), lname (last name) and address, so the address member declared as transient so its values will not be serialized (i.e. in case of deserialization of an object we will get the default value of address variable and its defined value will be erased).

假設我們有一個名為Transient的類,在該類中,我們有2-3個數據成員fname (名字), lname (姓氏)和address ,因此地址成員聲明為transient,因此其值將不被序列化(例如,對對象進行反序列化后,我們將獲取地址變量的默認值,并將其定義的值刪除)。

import java.io.*;
class Serialization implements Serializable {
public String fname, lname;
transient String address;
public Serialization(String fname, String lname, String address) {
this.fname = fname;
this.lname = lname;
this.address = address;
}
}
public class Deserialization {
public static void main(String[] args) {
Serialization serialize = new Serialization("Ronit", "Jain", "Mayur vihar 1");
try {
FileOutputStream fos = new FileOutputStream("E:\\Programs\\myjava.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serialize);
oos.close();
fos.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
serialize = null;
try {
FileInputStream fis = new FileInputStream("E:\\Programs\\myjava.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
serialize = (Serialization) ois.readObject();
ois.close();
fis.close();
System.out.println("His full name and address is :" + serialize.fname + " " + serialize.lname + " " + serialize.address);
} catch (IOException ex) {
System.out.println(ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
}

Output

輸出量

E:\Programs>javac Deserialization.java
E:\Programs>java Deserialization
His full name and address is :Ronit Jain null

4)易揮發 (4) volatile)

  • "volatile" is a keyword which is introduced in java.

    “ volatile”是在Java中引入的關鍵字。

  • "volatile" is the modifier applicable only for variables but not for methods and classes.

    “ volatile”是僅適用于變量的修飾符,不適用于方法和類。

  • If the value of variable keep on changing such type of variable we have to declare with the volatile modifier.

    如果變量的值繼續更改此類變量,則必須使用volatile修飾符進行聲明。

  • Any intermediate operation will be performed in local copy instead of the final copy.

    任何中間操作都將在本地副本而不是最終副本中執行。

Example:

例:

class VolatileVariable {
// volatile keyword here makes sure that
// the changes made in one class are 
// immediately reflect in other class
static volatile int volatile_var = 10;
}
class Main {
public static void main(String[] args) {
System.out.println("The previous value of volatile variable in one class is " + VolatileVariable.volatile_var);
VolatileVariable.volatile_var++;
System.out.println("The value changes made to the volatile variable in other class is" + VolatileVariable.volatile_var);
}
}

Output

輸出量

E:\Programs>javac Main.java
E:\Programs>java Main
The previous value of volatile variable in one class is 10
The value changes made to the volatile variable in other class is 11

5)最終 (5) final)

  • "final" is a keyword which is introduced in java.

    “ final”是在Java中引入的關鍵字。

  • "final" is the modifier applicable for methods, classes, and variables.

    “最終”是適用于方法,類和變量的修飾符。

  • We cannot override in child class.

    我們不能在子類中重寫。

Example: Declare class as "final" and variable as final and method as final

示例:將類聲明為“ final”,將變量聲明為final,將方法聲明為final

final class Final {
final String str = "we are accessible final variable";
final void printMethod() {
System.out.println("we are in final method");
}
public static void main(String[] args) {
Final f = new Final();
System.out.println("final variable :" + f.str);
f.printMethod();
}
}

Output

輸出量

E:\Programs>javac Final.java
E:\Programs>java Final
final variable :we are accessible final variable
we are in final method.

6)摘要 (6) abstract)

  • "abstract" is a keyword which is introduced in java.

    “抽象”是在Java中引入的關鍵字。

  • "abstract" is the modifier applicable for classes and methods.

    “抽象”是適用于類和方法的修飾語。

  • If a class is abstract then we need to implement all methods of abstract class in our class.

    如果一個類是抽象的,那么我們需要在我們的類中實現所有抽象類的方法。

Example:

例:

abstract class AbstractClass {
abstract void printMethod();
}
public class AbstractImplementation {
public static void main(String[] args) {
AbstractClass ac = new AbstractClass() {
void printMethod() {
System.out.println("Hi, We are in abstract class");
}
};
ac.printMethod();
}
}

Output

輸出量

E:\Programs>javac AbstractImplementation.java
E:\Programs>java AbstractImplementation
Hi, We are in abstract class

7)靜態 (7) static)

  • "static" is a keyword introduced in java.

    “ static”是Java中引入的關鍵字。

  • "static" member create one copy of the whole program and share it with other objects of the same program.

    “靜態”成員創建整個程序的一個副本,并與同一程序的其他對象共享。

  • "static" can access only static methods.

    “靜態”只能訪問靜態方法。

Example:

例:

class StaticClass {
public static int div(int a, int b) {
return a / b;
}
}
class Main {
public static void main(String[] args) {
int p = 20, q = 10;
int div = StaticClass.div(p, q);
System.out.println("The div of p , q is" + div);
}
}

Output

輸出量

E:\Programs>javac Main.java
E:\Programs>java Main
The div of p , q is2

翻譯自: https://www.includehelp.com/java/what-are-the-non-access-modifiers-in-java.aspx

java中訪問修飾符

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378687.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378687.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378687.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mui實現分享功能_MUI 分享功能(微信、QQ 、朋友圈)

配置文件&#xff1a;manifest.jsonplus ->plugins 下邊"share": {/*配置應用使用分享功能&#xff0c;參考http://ask.dcloud.net.cn/article/27*/"qq": {"appid": "",/*騰訊QQ開放平臺申請應用的AppID值*/"description"…

Java 注解學習筆記

轉自&#xff1a;http://wanqiufeng.blog.51cto.com/409430/458883 一、什么是java注解 注解&#xff0c;顧名思義&#xff0c;注解,就是對某一事物進行添加注釋說明&#xff0c;會存放一些信息&#xff0c;這些信息可能對以后某個時段來說是很有用處的。 Java注解又叫java標注…

Prime Palindromes

博客園速度非常不穩定&#xff0c;可能要考慮換地方了。雖然我非常喜歡博客園的模板和氣氛。 這個題早就知道是怎么做的了。先求出回文數在再判斷是不是素數。關鍵是不知道區間&#xff0c;那就把所有的全部求出來。雖然可能會超時&#xff0c;但是如果使用點技巧的話還是沒問題…

Opencv——DFT變換(實現兩個Mat的卷積以及顯示Mat的頻域圖像)

DFT原理&#xff1a;&#xff08;單變量離散傅里葉變換&#xff09; 數學基礎&#xff1a; 任何一個函數都可以轉換成無數個正弦和余弦函數的和的形式。 通常觀察傅里葉變換后的頻域函數可以獲得兩個重要的信息&#xff1a;幅頻曲線和相頻曲線。 在數字圖像處理中的作用&#…

python方法items_Python字典items()方法與示例

python方法items字典items()方法 (Dictionary items() Method) items() method is used to get the all items as a view object, the view object represents the key-value pair of the dictionary. items()方法用于獲取所有項目作為視圖對象&#xff0c;該視圖對象表示字典的…

基于(Python下的OpenCV)圖像處理的噴墨墨滴形狀規范檢測

通過圖像處理&#xff0c;分析數碼印花的噴頭所噴出來的墨滴形狀&#xff0c;與標準墨滴形狀對比分析&#xff0c;來判斷墨水及其噴頭設備的狀態&#xff0c;由兩部分構成 PS&#xff1a;獲取墨滴形狀照片和標準墨滴形狀照片都是手繪的&#xff0c;將就的看吧&#xff0c;主要…

const_iterator,const 迭代器

const 迭代器:是迭代器產量&#xff0c;該迭代器的值不能被修改&#xff0c;且需要初始化&#xff0c;初始化之后不能指向其他元素。const_iterator:當我們對const_iterator類型解引用時&#xff0c;返回一個const值&#xff0c;所以只能讀&#xff0c;不能寫。它是一種迭代器…

臨時禁止令:諾西購摩托羅拉面臨流產窘境?

近日&#xff0c;美國伊利諾伊州北區法院就中國華為起訴摩托羅拉公司和諾西一案作出初步裁決&#xff0c;禁止摩托羅拉解決方案公司(Motorola Solutions)向諾西披露華為的機密資料。此判決一出&#xff0c;各方評論紛沓而來。筆者認為&#xff0c;從諾西12以美元并購摩托羅拉部…

mysql replace into 語法_mysql Replace into與Insert update

Replace intoreplace into 跟 insert 功能類似&#xff0c;不同點在于&#xff1a;replace into 首先嘗試插入數據到表中&#xff0c;1. 如果發現表中已經有此行數據(根據主鍵或者唯一索引判斷)則先刪除此行數據&#xff0c;然后插入新的數據。2. 否則&#xff0c;直接插入新數…

微機原理——指令系統——傳送類指令(MOV、LEA、LDS、LES、LAHF、SAHF、XCHG、XLAT、PUSH、POP、PUSHF、POPF)

博主聯系方式&#xff1a; QQ:1540984562 QQ交流群&#xff1a;892023501 群里會有往屆的smarters和電賽選手&#xff0c;群里也會不時分享一些有用的資料&#xff0c;有問題可以在群里多問問。 【沒事兒可以到我主頁看看】https://blog.csdn.net/qq_42604176 傳送類指令1&…

lastindexof方法_Java Vector lastIndexOf()方法與示例

lastindexof方法向量類別的lastIndexOf()方法 (Vector Class lastIndexOf() method) Syntax: 句法&#xff1a; public int lastIndexOf (Object ob);public int lastIndexOf (Object ob, int indices);lastIndexOf() method is available in java.util package. lastIndexOf(…

李開復:微博的價值在哪里

導讀&#xff1a;微博可以改變社會現象&#xff0c;可以傳播信息&#xff0c;可以幫助你成長&#xff0c;可以發出你的聲音。它讓我們能夠人人成為記者&#xff0c;讓每一個轉發的人都變成了一個編輯 很多人問微博是搶了誰的生意&#xff0c;開心網還是人人網&#xff1f;其實它…

mysql 任務計劃 /etc/cron.d_Linux /etc/cron.d增加定時任務

一般情況下我們添加計劃任務時&#xff0c;都是直接修改/etc/crontab。但是&#xff0c;不建議這樣做&#xff0c;/etc/cron.d目錄就是為了分項目設置計劃任務而創建的。例如&#xff0c;增加一項定時的備份任務&#xff0c;我們可以這樣處理&#xff1a;在/etc/cron.d目錄下新…

19-Harris角點檢測

角點檢測顧名思義&#xff0c;就是對類似頂點的檢測&#xff0c;與邊緣有所區別 邊緣可能在某一方向上變化不是特別明顯&#xff0c;但角點在任何方向上變換都很明顯 cv2.cornerHarris(img,blockSize,ksize,k) cv2.cornerHarris(gray,2,3,0.04) 參數一&#xff1a;img&#xff…

微機原理——指令系統——算數運算指令(ADD、ADC、SUB、SBB、INC、DEC、NEG、CMP、MUL、IMUL、DIV、IDIV、CBW、CWD、BCD調整)

博主聯系方式&#xff1a; QQ:1540984562 QQ交流群&#xff1a;892023501 群里會有往屆的smarters和電賽選手&#xff0c;群里也會不時分享一些有用的資料&#xff0c;有問題可以在群里多問問。 算數運算指令1、加減法指令ADD、ADC 、SUB 、SBB 和增量減量指令INC、DEC、NEGADD…

linux系統出現Too many open files 錯誤、linux too many open files

故障一、linux too many open files linux系統出現Too many open files 錯誤&#xff0c;這是因為文件描述符大小不夠&#xff0c;或者有不正常的網絡連接(Socket也是一種特殊的文件)、文件IO沒有關閉并釋放出文件描述符&#xff08;文件句柄&#xff0c;File Operator&#xf…

精通init ramfs構建

一、init ramfs是什么   在2.6版本的linux內核中&#xff0c;都包含一個壓縮過的cpio格式的打包文件。當內核啟動時&#xff0c;會 從這個打包文件中導出文件到內核的rootfs文件系統&#xff0c;然后內核檢查rootfs中是否包含有init文件&#xff0c;如果有則執行它&#xff0…

python 示例_帶有示例的Python date isocalendar()方法

python 示例Python date.isocalendar()方法 (Python date.isocalendar() Method) date.isocalendar() method is used to manipulate objects of date class of module datetime. date.isocalendar()方法用于操作模塊datetime的日期類的對象。 It uses a date class object a…

mysql 函數重載_[賦值]函數,變量,重載 ,_第1頁_169IT

[java/j2ee] java實現簡單的給sql語句賦值的示例代碼本身很簡單。拼接sql的時候&#xff1f;不好數&#xff0c;簡單的用來賦值。代碼如下:/** * TODO 循環賦值,缺少的類型可隨時添加 * author Lucius * param pt * param list * throws SQLException */ public static…

20-SIFT算法

import cv2 import numpy as np from matplotlib import pyplot as pltdef show_photo(name,picture):#圖像顯示函數cv2.imshow(name,picture)cv2.waitKey(0)cv2.destroyAllWindows()img cv2.imread(E:\Jupyter_workspace\study\data/cfx.png) gray cv2.cvtColor(img,cv2.COL…