java學習次日

java學習次日
第一課
java

面向對象1
         面向對象  思考問題時,以具體的事物爲單位,考慮它的屬性(特徵)及動做(行爲)
數據庫


類與對象
         類   --> 是對一類事物的描述,是抽象的、概念上的定義
         好比人類就是一個抽象的類,由於只是概念上的,而不是實例到哪個人
        
         對象  -->  是實際存在的  該類事物的每一個個體 也稱實例
         好比 張三 屬於人類,而後又具體到哪個人 這就成實例化 也就是對象dom

 

// public  --> 訪問修飾符,類(外部類)的修飾符有:public  跟默認的
//  class  --> 類關鍵字,表示這個是類
// ClassKnow   類名:一、必須符合命名規則,命名習慣
public class ClassKnow
{函數

 //成員變量(屬性)工具

 //【訪問修飾符】【其餘修飾符(final static)】 數據類型  變量名 【=變量值】
 //注意點:變量值,若是沒有賦值,那就是默認值(視數據類型而定)學習

 //訪問修飾符有  public private protected 默認的this

 public int a = 1;spa

 //靜態成員變量   static 修飾
 public static int classStatic = 2;對象

 

 //成員方法
 //【訪問修飾符】【其餘修飾符(final static)】 返回值 方法名(參數列表) 【throw Exception】
 //注意:必定要方法體
 public  int method(String xx, int bb){
  //知識點:
  //普通的成員方法能夠調用普通的方法
  //普通的成員方法中可使用普通的成員變量
  // this   ---> 表示 ClassKnow  new以後的對象
  // this表明當前對象
  //方法裏面能夠聲明 /定義  局部變量
  
  return 0;
 }遞歸

 //靜態成員方法
 public static void main(String[] args){
  //知識點:
  //static 方法 只能調用 static 方法或者 static 成員變量
  //不然報錯:錯誤: 沒法從靜態上下文中引用非靜態 方法 method(String,int)
  //method("", 111);
  
  //錯誤: 沒法從靜態上下文中引用非靜態 變量 classIntAttr
  System.out.println(classIntAttr);
 
 }

 

}

 

對象的產生和使用

                構造方法(函數)  構造方法的名字和類名相同,而且沒有返回值
也不要加void
                構造方法(函數)

                         做用:在建立對象時(new時)自動調用構造方法 而且
初始化對象

                利用關鍵字 new 調用類的構造方法(函數)就能夠建立該類的一個對象

兩種構造方法
            參數構造方法

            默認(隱藏,隱式)構造方法  --> 指不帶參數的構造方法


this 關鍵字
           能夠看做是一個變量,它的值就是當前對象的引用

 

對象的內存圖

 


一、在棧聲明一個對象
二、在堆建立一個該類的對象,裏面有屬性,方法
三、把建立的該類的對象的 內存地址 賦值給 該類的一個對象

 

 

字符串的引用


例子解釋
public class PassValue {
 public void change(int x){
        x = 100;
    }
 
 public void change(Student student1){
  student1.name = "langfeiyes";
    }
 
 public void change(String str){
  str = str + "1510";
  //System.out.println(str);
    }
 
 public String change2(String str){
  return str + "1510";
    }

 public static void main(String[] args) {
  PassValue passValue = new PassValue();
  int x = 10;
  //基本類型當參數賦值給方法,在方法內部改變這個參數時候
  //原先的基本類型沒有變化
  passValue.change(x); 
  System.out.println(x);
  
  
  //引用類型賦值(String 引用類型 除外)
  //引用類型當參數賦值給方法時,在方法內部改變這個參數(屬性或者其餘東西)
  //原先的引用類型對應(屬性或者其餘東西),將發生改變。
  Student student = new Student("隔壁老王");
  passValue.change(student);
  System.out.println(student.name);
  
  
  //String 引用類型傳值
  //字符串類型當參數賦值給方法是,在方法內部改變這個參數
  //原先的字符串類型沒有發生改變
  //緣由:字符串池存在,當字符串發生改變時,會新生產一個字符串對象
  String string = "zhangsan";
  passValue.change(string);
  System.out.println(string);
  
  
  //實際開發中問題:
  //若是想對字符串進行加工,同樣要返回加工後的字符串
  String string1 = "李四";
  string1 = passValue.change2(string1);
  System.out.println(string1);  //另外操做
  
  
  //判斷一個對象是否相等使用  ==  比較的是內存地址
  Student student2 = new Student();
  Student student3 = new Student();
  //經過new出來的對象,都會從新分配內存地址,同類對象賦值是不會改變內存堆棧
  Student student4 = student3;
  //false
  System.out.println("student2 == student3?" + (student2 == student3));
  //true
  System.out.println("student4 == student3?" + (student4 == student3));
  
  //首先去字符串池裏面去找,若是發現有字符跟它內容相等,則直接賦值
  //不然,新建一個字符串對象
  String str1 = "zhansan";
  String str2 = "zhansan";
  
  
  String str3 = new String("zhansan");
  
  //true
  System.out.println("str1 == str2?" + (str1 == str2));
  
  //false  經過new出來的對象,都會從新分配內存地址,同類對象賦值是不會改變內存堆棧
  System.out.println("str1 == str3?" + (str1 == str3));
  
  
 }
 
 


static 的引用

 

例子解釋


public class StaticAtr {
 //知識點1;靜態成員變量,它有專屬的數據區,
 // 類加載進內存的時候,靜態屬性也同時加載到專屬的數據區
 //知識點2:靜態成員變量,是惟一的,它屬於類的,訪問方法   類名.靜態屬性,
 // 因此靜態成員變量稱爲,類變量  類屬性
 static String staticAtr = "靜態成員變量";
 
 //非靜態屬性
 String noStaticAtr = "非靜態成員變量";
 
 
 //靜態方法
 //知識點1:靜態成員方法,它有專屬的方法區,類加載進內存的時候,
 // 靜態方法也同時加載到專屬的方法區
 //知識點2:靜態成員變量,是惟一的,它屬於類的,訪問方法   類名.靜態方法,
 // 因此靜態成員方法稱爲,類方法
 public static void info(){
  System.out.println("靜態方法:StaticAtr.info()");
 }
 
 
 //靜態方法調用非靜態屬性
 public static void method(){
  //在靜態方法裏面沒法使用this這個關鍵字
  //Cannot use this in a static context
  
  //在靜態方法裏面沒法調用非靜態屬性
  //緣由:非靜態屬性,它屬於對象,只有new 對象,給它進行賦值,這樣調用時候,才具備實際意義
  //而,靜態方法是能夠經過 類名.靜態方法 進行訪問,即便沒有new這個對象也是能夠訪問
  //Cannot make a static reference to the non-static field noStaticAtr
  //System.out.println("非靜態的屬性:" + noStaticAtr);
  
  System.out.println("StaticAtr.method");
  //靜態方法不可訪問非靜態方法,可是,反過來,非靜態方法能夠訪問靜態方法
  //noStaticMethod();
 }
 
 public  void noStaticMethod(){
  method();
 }
 
 public static void main(String[] args) {
  StaticAtr staticAtr1 = new StaticAtr();
  StaticAtr staticAtr2 = new StaticAtr();
  
  staticAtr1.noStaticAtr = "zhangsan"; //修改非靜態屬性
  System.out.println(staticAtr1.noStaticAtr);  //對象一
  
  System.out.println(staticAtr2.noStaticAtr); //對象二
  System.out.println("----修改靜態屬性-------------------");
  
  staticAtr1.staticAtr = "改變了靜態屬性";
  System.out.println(staticAtr1.staticAtr);   //對象1
  System.out.println(staticAtr2.staticAtr); //對象二
  
  
  
  
  System.out.println(StaticAtr.staticAtr);   //類名.靜態屬性
  
  
  System.out.println("對象對用靜態方法-------------------");
  
  //The static method info() from the type StaticAtr
  //should be accessed in a static way
  staticAtr1.info();
  StaticAtr.info();
  
 }
 
}

 

 


構造方法和this的使用


/*
 一、構造方法的名字和類名相同,而且沒有返回值,而且不要加void。
 二、分類:自定義的 默認的(無參的) 自定義跟默認2者只能存其一
 三、構造方法的做用在於構造並初始化對象。
 四、構造方法的重載 關注參數列表    具體跟方法的重載一致
 五、this.屬性/方法   表示調用當前屬性或方法
    this(xxxx);   構造函數的調用, 注意點:this(xxx) 必須是第一行
*/


public class UserTwo

 String username;
 String password;
 int age;


 //構造方法的名字和類名相同,而且沒有返回值,而且不要加void。
 //構造方法的做用在於構造並初始化對象。
 //[訪問修飾符]  注意:沒有[static final]修飾符
 //注意:若是類中自定義的構造方法,java會自動給你默認添加一個無參數的構造方法
 //若是你類中定義一個或一個以上的構造方法[構造器](不論是否帶參數),java一概不提供
 //默認的構造方法
 //UserTwo(){
 //}
 
 //不是構造方法,這是普通成員方法
 //void UserTwo(String username){
 //}
 
 UserTwo(){ //無參構造函數
  //this("");  注意:this 調用構造器時,要注意避免 遞歸構造器調用,進而陷入死循環
  System.out.println("無參構造函數");
 }
 
 //構造方法的重載
 UserTwo(String username){ //有1個參數構造函數
  //this();
  System.out.println("有1個參數構造函數");
 }

 //並初始化對象
 UserTwo(String username, String password){
  System.out.println("有2個參數構造函數");
  //this表示當前對象
  this.username = username;
  this.password = password;
  

 }

 UserTwo(String username, String password, int age){
  //this();  
  
  //this的第二個用法,
  //注意:一、錯誤: 對this的調用必須是構造器中的第一個語句
  this(username, password); 
  this.age = age; 

  System.out.println("有3個參數構造函數");
 }

 /*
  爲啥要有帶參數的構造方法:
  一、爲了初始化對象一些屬性
  二、建立方便,減小量
  
  構造方法使用技巧:
  一、若是屬性很是多,把必要,使用頻率高的屬性當作構造方法的屬性
  二、this調用構造函數
 */
 public static void main(String[] args){
  //實例化對象時候,其本質,就是調用構造方法
  //調用構造方法,跟調用普通方法同樣
  //關注: 參數列表
  //UserTwo  user = new UserTwo("");  //調用有參構造方法

  UserTwo user = new UserTwo();
  user.username = "yaoming";
  user.password = "123";
  
  //等價
  //UserTwo user = new UserTwo("yaoming", "123");

  System.out.println("username:" + user.username);
  System.out.println("password:" + user.password);


  System.out.println("-----------------------");
  //
  UserTwo user3 = new UserTwo("lisi", "123", 19);
  System.out.println("username:" + user3.username);
  System.out.println("password:" + user3.password);
  System.out.println("age:" + user3.age);
 }
}

 


包的知識點:

一、包基本上隱藏了類並避免了名稱上的衝突。
二、實際項目中,能夠經過分包進行業務區分 
三、 jar若是使用默認包,沒法引用


分包技巧

實際開發中,必定要遵照項目已經制定好的分包規則
一、根據業務進去區分    好比:登陸 login   聊天chat
二、根據類的功能區分  好比:實體  domain   工具包 util

包的命名
域名的反寫  www.sun.com
        com.sun
這樣命名的緣由:類名稱上的衝突

要導入某個包中的全部類能夠用
                            包名.*

 

 

 

//靜態代碼  非靜態代碼public class StaticBlock { public static int  staticField;  //靜態成員屬性 public int noStaticField;   //非靜態成員屬性  //靜態代碼塊 //靜態代碼塊,在類加載的時候,開始執行,執行惟一的一次 //靜態代碼塊先於構造函數執行 //注意:使用靜態static關鍵修飾的方法或屬性或代碼塊都是按聲明的順序加載入內存 //因此,在調用過程當中必定要注意順序。 非靜態,沒有這個限制 //運用場景: // 一、數據庫的操做 // 二、程序運行參數的加載、環境初始化  // 類進行操做的時候,必須知足必定的條件,並且, //   這個條件一但設置好了就不須要進行修改 static{  //Cannot make a static reference to the non-static field noStaticField  //靜態代碼塊中,不能訪問非靜態屬性,能夠訪問靜態屬性,或者靜態方法  //System.out.println(noStaticField);  System.out.println("執行了StaticBlock 靜態代碼塊");  System.out.println("靜態屬性:"+staticField); }  //類加載後其餘模塊執行順序 //static 代碼塊執行   -- > 非靜態代碼塊   ---- > 構造函數 // 每次 new 對象的時候都執行, 緣由:非靜態代碼塊是屬於對象 //非靜態代碼塊(全局代碼塊) {  System.out.println("執行了StaticBlock 非靜態代碼塊"); }    StaticBlock(){  this.noStaticField = 10;  System.out.println("StaticBlock 類的構造方法。。。。"); }   public static void main(String[] args) {  //局部代碼塊  //裏面定義的變量只能在裏面使用,超出範圍後,報錯  //真實開發中,不建議這麼使用  {   int a = 0;  }  //a cannot be resolved to a variable  //System.out.println(a);  int a = 2;  System.out.println(a);  StaticBlock block = new StaticBlock();  StaticBlock block2 = new StaticBlock();   }   }

相關文章
相關標籤/搜索