您現在的位置是:首頁 > 人文
《Java面向物件程式設計》——類(1)
面向物件程式設計是什麼
今天繼續為大家帶來《Java面向物件程式設計》的學習。今天的主要內容是類,類的宣告,類體,成員變數和區域性變數。
Today we continue to bring you the study of Java Object Oriented Programming。 Today‘s main topics are classes, class declarations, class bodies, member variables and local variables。
01
面嚮物件語言
面向物件程式設計主要體現三個特性。
1。封裝性:將資料和對資料的操作封裝在一起,透過抽象,即從具體的例項中抽取共同的性質形成一般的概念,比如類的概念。
2。繼承性:子類可以繼承父類的屬性和功能,既繼承了父類具有的資料和資料上的操作,同時又可以增添子類獨有的資料和資料上的操作。
3。多型性:有兩種意義上的多型,一種是操作名稱的多型,即有多個操作具有相同的名字,但這些操作所接受的訊息型別必須不同。另一種是多型是和繼承有關的多型,是指同一個操作被不同型別物件呼叫時可能產生不同的行為。
Object-oriented programming embodies three main characteristics。
1。 Encapsulation: data and operations on data encapsulated together, through abstraction, that is, from the concrete examples of common properties to form a general concept, such as the concept of class。
2。 inheritance: subclasses can inherit the properties and functions of the parent class, both inherit the data and operations on the data that the parent class has, and at the same time can add the data and operations on the data unique to the subclass。
3。 Polymorphism: There are two senses of polymorphism, one is the polymorphism of operation names, that is, there are multiple operations with the same name, but the types of messages accepted by these operations must be different。 The other is polymorphism is polymorphism related to inheritance, which means that the same operation may produce different behaviors when called by different types of objects。
02
類
1。類
類是組成Java程式的基本要素,類封裝了一類物件的狀態和方法,類是用來定義物件的模板。
2。類的宣告
class People和class動物被稱為類的宣告,People和動物分別是類名。類的名字要符合識別符號的規定。
在給類命名時,要遵守以下程式設計風格:
(1)如果類名使用拉丁字母,那麼名字的首字母使用大寫字母,如Hello,Time和Dog等。
(2)類名最好容易識別,見名知意。當類名由幾個“單詞”複合而成時,每個單詞的首字母使用大寫,如Beijing和AmericanGame。
1。 Classes
Classes are the basic elements of Java programs, classes encapsulate the state and methods of a class of objects, classes are used to define the object template。
2。 Class declarations
class People and class animals are called class declarations, People and animals are class names respectively。 The name of the class has to conform to the identifier。
When naming classes, the following programming style is observed。
(1) If the class name uses Latin letters, then the first letter of the name is in uppercase, such as Hello, Time and Dog。
(2) The class name is best easily recognized by its name。 When the class name is a composite of several “words”, use capital letters for the first letter of each word, such as Beijing and AmericanGame。
3。類體
寫類的目的是為了描述一類事物共有的屬性和功能,描述過程由類體實現。類體的內容由兩部分構成:一部分是變數的宣告,用來刻畫屬性;另一部分是方法的定義,用來刻畫功能。
4。成員變數和區域性變數
類體分為兩部分:變數的宣告部分和方法的定義。在變數宣告部分宣告的變數被稱為類的成員變數,在方法體中宣告的變數和方法的引數被稱為區域性變數。
(1)變數的型別
成員變數和區域性變數的型別可以是Java中的任何一種資料型別。
(2)變數的有效範圍
成員變數在整個類內部有效,區域性變數只在宣告它的方法內有效。方法引數在整個方法內有效,方法內的區域性變數從宣告它的位置之後開始有效。
(3)例項變數和類變數
成員變數又分為例項變數和類變數。在宣告成員變數時,用關鍵字static給予修飾的變數稱為類變數,否則稱為例項變數(類變數也稱為static變數,靜態變數)。
3。 Class body
The purpose of writing a class is to describe the properties and functions common to a class of things, and the description process is realized by the class body。 The content of the class body consists of two parts: one is the declaration of variables, which is used to portray the properties; the other is the definition of methods, which is used to portray the functions。
4。 Member variables and local variables
The class body is divided into two parts: the declaration part of the variables and the definition of the methods。 The variables declared in the variable declaration part are called member variables of the class, and the variables declared in the method body and the parameters of the method are called local variables。
(1) Types of variables
The types of member variables and local variables can be any of the data types in Java。
(2) Valid scope of variables
Member variables are valid within the entire class, and local variables are valid only within the method in which they are declared。 Method parameters are valid within the entire method, and local variables within the method are valid from the location where it is declared。
(3) Instance variables and class variables
Member variables are further divided into instance variables and class variables。 When declaring a member variable, a variable modified with the keyword static is called a class variable, otherwise it is called an instance variable (class variables are also called static variables, static variables)。
參考資料:文字:百度;圖片:微博;翻譯:百度翻譯
本文由LearningYard新學苑原創,部分圖片文字來自網路,如有侵權請聯絡。
推薦文章
- 死於尼泊爾空難的女副駕:丈夫墜機身亡她接力飛行夢,離當機長僅一步之遙
據尼泊爾媒體報道,客機的副駕駛是名叫Anju的女飛行員,17年前她的飛行員丈夫死於空難後,她選擇成為一名飛行員,繼續丈夫的夢想,她此次飛行結束後即將成為一名機長,然而就在她作為副駕的最後一次飛行中,只差10秒就能著陸時,飛機卻意外墜毀,令人...
- 首次釋出!210個航空城市排行
新華社記者 任超 攝2015年以來,中國民航大學“航空城市發展指數研究”課題組全面梳理過去50年裡國際民航組織(ICAO)等國內外、行業內外同行的實踐成果,根據民航局、國家統計局、交通運輸部和地方政府建國以來的公開資料,採用機器學習等大資料...
- 朱一龍擊敗徐崢等人拿獎被質疑,吳京陪坐被送黑熱搜,禍水東引?
不料,吳京也沒有逃脫,正當網友還在質疑朱一龍不配的時候,一個“吳京手機”的詞條登上熱搜,這一幕原本很暖心,拍的是吳京對劉德華流露出迷弟的一面...