1.構造方法Constructor概述及做用java
1.1做用設計模式
給對象的數據(屬性)進行初始化數組
class Demo1_Constructor { //Constructor構造 public static void main(String[] args) { Person p = new Person(); //在一建立對象的時候,系統就幫我調用了構造方法 //p.Person(); //構造方法不能用對象調用 p.show(); Person p2 = new Person(); //再次建立對象 p2.show(); } } /* * A:構造方法概述和做用 * 給對象的數據(屬性)進行初始化 * B:構造方法格式特色 * a:方法名與類名相同(大小也要與類名一致) * b:沒有返回值類型,連void都沒有 * c:沒有具體的返回值return; */ class Person { private String name; private int age; //構造方法 public Person() { //System.out.println("Hello World!"); //return; //構造方法也是有return語句的,格式是return; name = "張三"; age = 23; } public void show() { System.out.println(name + "..." + age); } }
1.2格式特色dom
方法名與類名相同(大小也要與類名一致)jvm
沒有返回值類型,連void都沒有函數
沒有具體的返回值return;工具
2.構造方法的重載及注意事項學習
class Demo2_Person { public static void main(String[] args) { //對象名.方法名(); //構造方法 不能單獨調用 ,只能在建立對象的時候調用 //我怎麼區分我調用的是有參的構造方法,仍是無參的構造方法? //你傳了對應的參數,就調用了對應的構造方法,若是你不傳任何參數,那會無參的構造方法!! //Person(); Person p1 = new Person(); p1.show(); System.out.println("---------------------"); Person p2 = new Person("張三",23); p2.show(); System.out.println("---------------------"); Person p3 = new Person("李四",24); p3.show(); } } /* * A:案例演示 * 構造方法的重載 * 重載:方法名相同,與返回值類型無關(構造方法沒有返回值),只看參數列表 * B:構造方法注意事項 * a:若是咱們沒有給出構造方法,系統將自動提供一個無參構造方法。 * b:若是咱們給出了構造方法,系統將再也不提供默認的無參構造方法。 * 注意:這個時候,若是咱們還想使用無參構造方法,就必須本身給出。建議永遠本身給出無參構造方法 */ class Person { private String name; //姓名 private int age; //年齡 public Person() { //空參構造 System.out.println("空參的構造"); } public Person(String name,int age) { this.name = name; this.age = age; System.out.println("有參的構造"); } public void show() { System.out.println(name + "..." + age); } }
2.1構造方法的重載this
方法名相同,與返回值類型無關(構造方法沒有返回值),只看參數列表.net
2.2注意事項
若是咱們沒有給出構造方法,系統將自動提供一個無參構造方法。
若是咱們給出了構造方法,系統將再也不提供默認的無參構造方法。
注意:這個時候,若是咱們還想使用無參構造方法,就必須本身給出。建議永遠本身給出無參構造方法
3.給成員變量賦值的兩種方式的區別
class Demo3_Person { public static void main(String[] args) { Person p1 = new Person("張三",23); //p1 = new Person("張天一",23); //這種方式看運行結果貌似是更名了,實際上是將原對象變成垃圾 System.out.println(p1.getName() + "..." + p1.getAge()); System.out.println("--------------------"); Person p2 = new Person(); //空參構造建立對象 p2.setName("李四"); p2.setAge(24); p2.setName("李鬼"); System.out.println(p2.getName() + "..." + p2.getAge()); } } /* 構造方法 給屬性進行初始化 setXxx方法 修改屬性值 這兩種方式,在開發中用setXxx更多一些,由於比較靈活 */ class Person { private String name; //姓名 private int age; //年齡 public Person() { //空參構造 } public Person(String name,int age) {//有參構造 this.name = name; this.age = age; } public void setName(String name) { //設置姓名 this.name = name; } public String getName() { //獲取姓名 return name; } public void setAge(int age) { //設置年齡 this.age = age; } public int getAge() { //獲取年齡 return age; } }
3.1setXxx()方法
修改屬性值
開發中用setXxx更多一些,由於比較靈活
class Demo4_Student { public static void main(String[] args) { Student s1 = new Student(); //使用空參構造 s1.setName("張三"); //設置姓名 s1.setAge(23); //設置年齡 System.out.println("個人姓名是:" + s1.getName() + ",個人年齡是:" + s1.getAge()); //getXxx()獲取屬性值,能夠打印,也能夠賦值給其餘的變量,作其餘的操做 Student s2 = new Student("李四",24); s2.show(); //只是爲了顯示屬性值 } } /* * A:案例演示 * 學生類: * 成員變量: * name,age * 構造方法: * 無參,帶兩個參 * 成員方法: * getXxx()/setXxx() * show():輸出該類的全部成員變量值 * B:給成員變量賦值: * a:setXxx()方法 * b:構造方法 * C:輸出成員變量值的方式: * a:經過getXxx()分別獲取而後拼接 * b:經過調用show()方法搞定 */ class Student { private String name; //姓名 private int age; //年齡 public Student(){} //空參構造 public Student(String name,int age) { //有參構造 //賦值 this.name = name; this.age = age; } public void setName(String name) { //設置姓名 this.name = name; } public String getName() { //獲取姓名 return name; } public void setAge(int age) { //設置年齡 this.age = age; } public int getAge() { //獲取年齡 return age; } public void show() { System.out.println("個人姓名是:" + name + ",個人年齡是:" + age); } }
3.2構造方法
給對象中屬性進行初始化
4.一個對象的建立過程作了哪些事情?
5.static關鍵字
5.1帶有static的內存圖
5.2 static關鍵字的特色
class Demo1_Static { public static void main(String[] args) { Person p3 = new Person(); p3.name = "福原愛老師"; //調用姓名屬性並賦值 p3.country = "臺灣"; //調用國籍屬性並賦值 p3.speak(); Person p1 = new Person(); //建立對象 p1.name = "蒼老師"; //調用姓名屬性並賦值 p1.country = "日本"; //調用國籍屬性並賦值 p1.speak(); Person p2 = new Person(); p2.name = "小澤老師"; //調用姓名屬性並賦值 // p2.country = "日本"; //調用國籍屬性並賦值 p2.speak(); Person p4 = new Person(); p4.name = "黃老師"; //調用姓名屬性並賦值 // p2.country = "日本"; //調用國籍屬性並賦值 p4.speak(); //Person.country = "日本"; //靜態多了一種調用方式,能夠經過類名. //System.out.println(Person.country); } } class Person { String name; //姓名 static String country; //國籍 public void speak() { //說話的方法 System.out.println(name + "..." + country); } }
5.3 static的注意事項
class Demo2_Static { public static void main(String[] args) { //Demo d = new Demo(); //d.print1(); //Demo d = new Demo(); //System.out.println(d.num1); System.out.println(Demo.num1); //Demo.print2(); } } /* * A:static的注意事項 * a:在靜態方法中是沒有this關鍵字的 * 如何理解呢? * 靜態是隨着類的加載而加載,this是隨着對象的建立而存在。 * 靜態比對象先存在。 * b:靜態方法只能訪問靜態的成員變量和靜態的成員方法 * 靜態方法: * 成員變量:只能訪問靜態變量 * 成員方法:只能訪問靜態成員方法 * 非靜態方法: * 成員變量:能夠是靜態的,也能夠是非靜態的 * 成員方法:但是是靜態的成員方法,也能夠是非靜態的成員方法。 * 簡單記: * 靜態只能訪問靜態。 */ class Demo { int num1 = 10; //非靜態的成員變量 static int num2 = 20; //靜態的成員變量 /*public void print1() { //非靜態的成員方法,既能夠訪問靜態的成員也能夠訪問非靜態的 System.out.println(num1); System.out.println(num2); }*/ public static void print2() { //靜態的成員方法 //System.out.println(this.num1);//靜態的成員方法不能訪問非靜態的,錯誤: 沒法從靜態上下文中引用非靜態 變量 num1 System.out.println(num2); } }
5.4 靜態變量和成員變量的區別
###07.14_面向對象(工具類中使用靜態)(瞭解)
/** 這是一個數組工具類,裏面封裝了查找數組最大值,打印數組,數組反轉的方法 @author fengjia @version v1.0 */ public class ArrayTool { //若是一個類中全部的方法都是靜態的,須要再多作一步,私有構造方法,目的是不讓其餘類建立本類對象 //直接用類名.調用便可 /** 私有構造方法 */ private ArrayTool(){} //設計模式:單例模式 //1,獲取最大值 /** 這是獲取數組中最大值的方法 @param arr 接收一個int類型數組 @return 返回數組中最大值 */ public static int getMax(int[] arr) { int max = arr[0]; //記錄第一個元素 for (int i = 1;i < arr.length ;i++ ) { //從第二個元素開始遍歷 if (max < arr[i]) { //max與數組中其餘的元素比較 max = arr[i]; //記錄住較大的 } } return max; //將最大值返回 } //2,數組的遍歷 /** 這是遍歷數組的方法 @param arr 接收一個int類型數組 */ public static void print(int[] arr) { for (int i = 0;i < arr.length ;i++ ) { //遍歷數組 System.out.print(arr[i] + " "); } } //3,數組的反轉 /** 這是數組反轉的方法 @param arr 接收一個int類型數組 */ public static void revArray(int[] arr) { for (int i = 0;i < arr.length / 2 ;i++ ) { //循環次數是元素個數的一半 /* arr[0]與arr[arr.length-1-0] 交換 arr[1]與arr[arr.length-1-1] 交換 arr[2]與arr[arr.length-1-2] 交換 */ int temp = arr[i]; arr[i] = arr[arr.length-1-i]; arr[arr.length-1-i] = temp; } } }
###07.15_面向對象(說明書的製做過程)(瞭解)
###07.16_面向對象(如何使用JDK提供的幫助文檔)(瞭解)
###07.17_面向對象(學習Math類的隨機數功能)(瞭解)
class Demo2_Math { public static void main(String[] args) { //double d = Math.random(); //System.out.println(d); //Math.random()會生成大於等於0.0而且小於1.0的僞隨機數 for (int i = 0;i < 10 ;i++ ) { System.out.println(Math.random()); } //生成1-100的隨機數 //Math.random()0.0000000 - 0.999999999 //Math.random() * 100 ====> 0.00000 - 99.999999999 //(int)(Math.random() * 100) ====> 0 - 99 //(int)(Math.random() * 100) + 1 for (int i = 0;i < 10 ;i++ ) { System.out.println((int)(Math.random() * 100) + 1); } } }
###07.18_面向對象(猜數字小遊戲案例)(瞭解)