溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

初始化與清理 & 訪問(wèn)權(quán)限 & 組合與繼承 & final、static

發(fā)布時(shí)間:2020-08-11 14:30:38 來(lái)源:ITPUB博客 閱讀:171 作者:PengSY_b 欄目:編程語(yǔ)言

                   一.內(nèi)容簡(jiǎn)介
1.初始化與清理
2.訪問(wèn)權(quán)限
3.繼承概要
4.final,static
5.練習(xí)一

二.內(nèi)容梳理
1.初始化與清理

1.1構(gòu)造函數(shù)
構(gòu)造函數(shù):用來(lái)初始化成員變量的函數(shù),沒(méi)有返回值,函數(shù)名與類(lèi)名一致。
默認(rèn)構(gòu)造器:沒(méi)有參數(shù)的構(gòu)造函數(shù)。類(lèi)中如果沒(méi)有構(gòu)造器,編譯器會(huì)自動(dòng)創(chuàng)建 默認(rèn)構(gòu)造 器。
構(gòu)造函數(shù)的嵌套:可以在構(gòu)造器中調(diào)用被重載的其他構(gòu)造器,用 this(parameter )調(diào)用。且只可以調(diào)用一個(gè)并放在起始處。
繼承中的構(gòu)造函數(shù):構(gòu)造函數(shù)不會(huì)被繼承,子類(lèi)的構(gòu)造函數(shù)自動(dòng)調(diào)用父類(lèi)的默認(rèn)構(gòu)造函數(shù),如果父類(lèi)沒(méi)有默認(rèn)構(gòu)造函數(shù),則手動(dòng)調(diào)用父類(lèi)的構(gòu)造函數(shù)。super(parameter).


1.2重載
重載: 方法名相同,參數(shù)列表不同。與返回值無(wú)關(guān),返回值無(wú)法區(qū)分,如果返回值不同,方法名相同,參數(shù)列表相同,編譯出錯(cuò)。
重載的參數(shù) 匹配:對(duì)應(yīng)類(lèi)型對(duì)應(yīng)匹配。如果匹配不到: (常數(shù)與char )當(dāng)做int用來(lái) 匹配,其他的數(shù)據(jù)類(lèi)型逐漸提升數(shù)據(jù)類(lèi)型來(lái)匹配
重寫(xiě)的返回類(lèi)型必須一樣或協(xié)變,否則編譯錯(cuò)誤。


1.3初始化
局部變量不初始化會(huì)報(bào)錯(cuò),成員變量有默認(rèn)的初始化。
初始化順序:靜態(tài)優(yōu)于非靜態(tài),父類(lèi)優(yōu)先子類(lèi),變量?jī)?yōu)先于代碼塊優(yōu)先于構(gòu)造器。

  • 初始化與清理 & 訪問(wèn)權(quán)限 & 組合與繼承 & final、static

靜態(tài)數(shù)據(jù)的初始化:靜態(tài)數(shù)據(jù)屬于類(lèi),只占用一份存儲(chǔ)區(qū)域,有標(biāo)準(zhǔn)的初值。靜態(tài)數(shù)據(jù)的初始化時(shí)間是(靜態(tài)數(shù)據(jù)被訪問(wèn)或?qū)ο蟊粍?chuàng)建時(shí))


1.4清理
垃圾回收機(jī)制只釋放new的內(nèi)存
類(lèi)內(nèi)定義finalize()方法,當(dāng)垃圾回收發(fā)生時(shí),finalize()被調(diào)用


(5)可變參數(shù)列表
例子:

點(diǎn)擊( 此處 )折疊或打開(kāi)

  1. static void printArray(Object[] args){


  2. }

  3. printArray(1, 2, 3);

  4. printArray((Object[])new integer[](1, 2, 3));


——構(gòu)造函數(shù)使用可變參數(shù)列表會(huì)與默認(rèn)構(gòu)造函數(shù)弄混


2.訪問(wèn)權(quán)限

1.1

類(lèi)內(nèi)

包內(nèi)(子類(lèi)與非子類(lèi))

包外子類(lèi)

包外非子類(lèi)

Public

√    

Protected

×

默認(rèn)

×

×

private

×

×

×

1.2修飾類(lèi)只有public和默認(rèn)
3.繼承概要
extends指明繼承關(guān)系,未說(shuō)明隱式繼承Object.
向上轉(zhuǎn)型:子類(lèi)可以作為實(shí)參傳入父類(lèi)作為形參的方法。
this.value先找自己的成員,找不到去父類(lèi)找的.
構(gòu)造方法不會(huì)被繼承


4.final,static
1.1 final強(qiáng)調(diào)不變。static強(qiáng)調(diào)跟隨類(lèi)。
1.2final
類(lèi)不希望被繼承
方法不希望被重寫(xiě)
變量不希望被改變
final修飾的值不一定是常量還是變量,DJ反編譯看。

1.3static
靜態(tài)變量
靜態(tài)方法: 不能訪問(wèn)非靜態(tài)成員方法和非靜態(tài)成員變量
靜態(tài)代碼塊
static不允許修飾局部變量;



5.練習(xí)一
(1)需求
——汽車(chē)銷(xiāo)售人員銷(xiāo)售汽車(chē),款式有凱越,君威
——汽車(chē)與款式和編號(hào),款式編號(hào)不可修改
——汽車(chē)銷(xiāo)售人員有姓名
——銷(xiāo)售人員按車(chē)型及數(shù)量銷(xiāo)售

(2)代碼
AutoType.java

點(diǎn)擊( 此處 )折疊或打開(kāi)

  1. package  biz ;


  2. public   class  AutoType  {

  3.      public   static   final   String  KAI_YUE  =   "凱越" ;

  4.      public   static   final   String  JUB_WEI  =   "君威" ;

  5. }



Auto.java

點(diǎn)擊( 此處 )折疊或打開(kāi)

  1. package  biz ;


  2. public   class  Auto  {

  3.      private   String  autoType ;      // 款式

  4.      private   String  autoNumber ;      // 編號(hào)

  5.      private   boolean  isSelled ;

  6.     Auto ( String  autoType ,   String  autoNumber ) {

  7.          this . autoType  =  autoType ;

  8.          this . autoNumber  =  autoNumber ;

  9.          this . isSelled  =  false ;      // 默認(rèn)未銷(xiāo)售

  10.      }

  11.      public   String  getAutoType ( )   {

  12.          return  autoType ;

  13.      }

  14.      public   String  getAutoNumber ( )   {

  15.          return  autoNumber ;

  16.      }

  17.      public   boolean  isSelled ( )   {

  18.          return  isSelled ;

  19.      }

  20.      public   void  setSelled ( boolean  isSelled )   {

  21.          this . isSelled  =  isSelled ;

  22.      }

  23.     

  24.     

  25. }



seller.java

點(diǎn)擊( 此處 )折疊或打開(kāi)

  1. package  biz ;


  2. public   class  Seller  {

  3.      private   String   name ;

  4.     

  5.     Seller ( String   name ) {

  6.          this . name   =   name ;

  7.      }

  8.      /**

  9.      * 

  10.      * @param autos 所用的車(chē)

  11.      * @param autoType 要銷(xiāo)售的類(lèi)型

  12.      * @param num 要銷(xiāo)售的數(shù)量

  13.      */

  14.      public   void  sell ( Auto [ ]  autos ,   String  autoType , int  num ) {

  15. //        if(auto == null || autoType == null || num < 0) {

  16. //            

  17. //        }

  18.          int  selledNum  =  0 ;

  19.          for ( Auto curAuto :  autos )   {

  20.              int  i  =  0 ;

  21.             i + + ;

  22.              if ( ! curAuto . isSelled ( )   & &  curAuto . getAutoType ( ) . equals ( autoType )

  23.                      & & selledNum  <  num )   {

  24.                 curAuto . setSelled ( true ) ;

  25.                  System . out . println ( "準(zhǔn)備售出-" +  curAuto . getAutoType ( )   +   "-"

  26.                          + curAuto . getAutoNumber ( ) ) ;

  27.                 selledNum + + ;

  28.              }

  29.          }

  30.          if ( selledNum  <  num )   {

  31.              System . out . println ( "庫(kù)存不夠。"   +   "只可售出"   +  selledNum  +   "輛"   +  autoType ) ;

  32.          }

  33.         

  34.      }

  35.      public   String   getName ( )   {

  36.          return   name ;

  37.      }

  38.      public   void   setName ( String   name )   {

  39.          this . name   =   name ;

  40.      }

  41.      public   static   void  main ( String [ ]  args )   {

  42.         Seller s1  =   new  Seller ( "小明" ) ;

  43.         Auto auto1  =   new  Auto ( AutoType . JUB_WEI ,   "001" ) ;

  44.         Auto auto2  =   new  Auto ( AutoType . JUB_WEI ,   "002" ) ;

  45.         Auto [ ]  autos  =   new  Auto [ 2 ] ;  

  46.         autos [ ]   =  auto1 ;

  47.         autos [ 1 ]   =  auto2 ;

  48.         s1 . sell ( autos ,  AutoType . JUB_WEI ,  3 ) ;

  49.      }

  50.     

  51. }



三.總結(jié)與隨筆
(1)entity層:只有屬性沒(méi)有方法,主要用于數(shù)據(jù)跨層傳輸
(2)同一塊作用域,局部變量?jī)?yōu)先于成員變量。
(3)char 類(lèi)型可以' + ', ' - '
(4)方法傳參:基本類(lèi)型傳值,函數(shù)內(nèi)改變參數(shù)值對(duì)函數(shù)外無(wú)影響。引用類(lèi)型傳引用,函數(shù)內(nèi)改變參數(shù)值對(duì)函數(shù)外有影響。
(5)刪掉set方法,private修飾的成員變量只讀

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI