Java马士兵
一、內存解析
public class Birthday {private int day;private int month;private int year;public Birthday(int d,int m,int y){day = d;month = m;year = y;}public void setDay(int d){this.day = d;}public void setMonth(int m){this.month = m;}public void setYear(int y){this.year = y;}public int getDay(){return this.day;}public int getMonth(){return this.month;}public int getYear(){return this.year;}public void display(){System.out.println(day + "-" + month + "-"+year);} }public class Test {public static void main(String[] args) {//存放在堆中//棧中d1,d2指針指向該數據堆的地址Birthday d1 = new Birthday(7, 7, 1970);Birthday d2 = new Birthday(1, 1, 2000);//存放在堆內存中,棧中指針指向堆的地址Test test = new Test();//在棧中int date = 9;test.change1(date);System.out.println("date = " + date);// d1是birthday的一個引用test.change2(d1);d1.display();test.change3(d2);d2.display();}/*** * @param i 形參,在棧內存中存放 i = 9* 形參是值傳遞!!!*/public void change1(int i){i = 1234;}/*** * @param b引用和d1都是指向同一塊堆地址* 棧中的內存,程序調用之后就會消失* b在堆中,由gc處理*/public void change2(Birthday b){b = new Birthday(22,2,2004);}public void change3(Birthday b){//真真正正改變了堆中數據的值!!!b.setDay(20);} }二、方法的重載
定義:相同函數名,相同返回值,參數個數、類型不一樣
public class Test {void max(int a,int b){System.out.println(a > b ? a : b);}/*** 返回值不同不構成重載,編譯器解析有爭議* 如果調用test.max(1,2),有爭議*///int max(int a,int b){// System.out.println(a > b ? a : b);//}void max(float a,float b){System.out.println(a > b ? a : b);}public static void main(String[] args) {Test test = new Test();test.max(1,2);test.max(1.0f,2.0f);} } public class Test {void max(int a,int b){System.out.println(a > b ? a : b);}/*** 返回值不同不構成重載,編譯器解析有爭議* 如果調用test.max(1,2),有爭議*///int max(int a,int b){// System.out.println(a > b ? a : b);//}void max(float a,float b){System.out.println(a > b ? a : b);}public static void main(String[] args) {Test test = new Test();test.max(1,2);test.max(1.0f,2.0f);} }對象的創建和使用
- 必須使用new關鍵字創建對象
- 使用對象引用.成員變量
- 使用 對象引用.方法 來調用對象的方法
- 同一類的每個對象有不同的成員變量存儲空間
- 同一類的每個對象共享該類的方法
- 非靜態方法是針對每個對象進行調用
三、this關鍵字
this可以看做是一個變量,他的值是當前對象的引用
public class Leaf {//成員變量int i = 0;Leaf(){}Leaf(int i){this.i = i;}Leaf increament(){i++;return this; //仍指向當前對象}void print(){System.out.println("i = " + i);}public static void main(String[] args) {Leaf leaf = new Leaf(100);leaf.increament().increament().print();Leaf leaf1 = new Leaf();leaf1.print();} }四、static 關鍵字
靜態成員變量,它為該類的公共變量,在第一次使用時被初始化,對于該類的所有對象來說,static成員變量只有一份
用static聲明的方法為靜態方法,調用該方法時,不會將對象的引用傳遞給他,所以在static方法中不可以訪問非static的成員
靜態方法不針對某個對象調用,所以不能訪問非靜態成員
可以通過對象引用或者類名(不需要實例化)訪問靜態成員
靜態變量在數據區!!!
public class Cat {private static int sid = 0;private String name;int id;Cat(String name){this.name = name;id = sid++;}public void info(){System.out.println("My name is " + name + " No. " + id);}public static void main(String[] args) {Cat.sid = 100;Cat mimi = new Cat("mimi");Cat pipi = new Cat("pipi");mimi.info();pipi.info();} }五、package和import語句
包:解決類名的沖突問題
package語句是java源文件的第一條語句,指明該文件中定義的類所在的包
訪問同一個包中的類不需要引入
必須calss文件的最上層包的父目錄位于classpath下?
…
六、繼承與權限控制
對于class的權限修飾只可以用 public 和 default
- public類可以在任意地方被訪問
- default類只可以被同一個包內部的類訪問
七、方法的重寫
在子類中可以根據需要對從基類繼承的方法進行重寫
重寫方法必須和被重寫方法具有相同方法名稱、參數列表和返回類型
重寫方法不能使用比被重寫方法更嚴格的訪問權限
父類繼承的方法不滿意
public class Test {public static void main(String[] args) {Student student = new Student();Person person = new Person();person.setName("none");person.setAge(100);student.setName("John");student.setAge(18);student.setSchool("USTC");System.out.println(person.getInfo());System.out.println(student.getInfo());} } class Person{private String name;private int 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 String getInfo(){return "Name: " + name + "\n" + "age: " + age;} } class Student extends Person{private String school;public String getSchool() {return school;}public void setSchool(String school) {this.school = school;}@Overridepublic String getInfo() {return "Name: " + this.getName() + "\n" + "age: " + this.getAge()+ " school " + this.getSchool();} }八、super關鍵字
public class TestInherit {public static void main(String[] args) {ChildClass childClass = new ChildClass();childClass.f();} } class FatherClass{//默認為0public int value;public void f(){System.out.println("FatherClass.value="+value);} } class ChildClass extends FatherClass{public int value;public void f(){//調用父類方法super.f();//調用本類valuevalue = 200;System.out.println("ChildClass.value="+value); // 0System.out.println(value); // 200super.value = 300;System.out.println(super.value); //300} }子類構造方法必須調用父類的構造方法
super關鍵字寫在子類構造函數的第一行
如果子類的構造方法中沒有顯示地調用基類構造方法,則系統默認調用基類無參的構造方法
不寫任何構造函數,編譯器會自動加上一個無參構造方法
如果寫了一個構造方法,那么編譯器不會自動加上無參構造方法
子類的構造方法中必須調用super父類的構造方法,如果不寫super,系統默認調用父類無參構造方法!!!
若父類沒有無參構造函數,子類構造方法又沒有調用super方法,則編譯器會報錯!!!
九、Object類的toString()方法
System.out.lprintln(“info”+person),自動調用該對象類的toString方法
public class TestToString {public static void main(String[] args) {Dog dog = new Dog();//com.kcl.g_toString.Dog@3930015a//return getClass().getName() + "@" + Integer.toHexString(hashCode());System.out.println(dog);//相當于//推薦所有類重寫toString方法System.out.println(dog.toString());//959447386System.out.println(dog.hashCode());//class com.kcl.g_toString.DogSystem.out.println(dog.getClass());} }// "根類"默認繼承Object類 class Dog {}十、hashcode
十一、equals方法
public class TestEquals{public static void main(String args[]){Cat c1 = new Cat(1,2,3);Cat c2 = new Cat(1,2,3);// falseSystem.out.println(c1.equals(c2));} } class Cat{int color;int height,weight;public Cat(int color,int height,int weight){this.color = color;this.height = height;this.weight = weight;} }十二、多態
/*** 動態綁定是值在執行期間,非編譯期間,判斷所引用對象的實際類型,根據其實際的類型調用相應的方法*/class Animal{private String name;Animal(String name){this.name = name;}public void enjoy(){System.out.println("叫聲......");}} class Cat extends Animal{private String eyesColor;Cat(String n,String c){super(n);this.eyesColor = c;}@Overridepublic void enjoy() {System.out.println("貓叫聲......");} } class Dog extends Animal {private String furColor;Dog(String n, String c) {super(n);this.furColor = c;}@Overridepublic void enjoy() {System.out.println("狗叫聲......");} } class Lady{private String name;private Animal pet;Lady(String name,Animal pet){this.name = name;this.pet = pet;}public void myPetEnjoy(){pet.enjoy();} } public class Test {public static void main(String[] args) {Cat c = new Cat("catname", "blue");Dog d = new Dog("dogname", "black");Lady l1 = new Lady("l1", c);Lady l2 = new Lady("l2", d);l1.myPetEnjoy();l2.myPetEnjoy();} }
代碼段
根據Lady對象的成員變量pet所引用的不同的實際類型而調用響應的enjoy方法
3個必要條件
當你調用父類里面被重寫的方法時,實際當中new的哪個子類對象,就調用哪個子類對象的方法
十三、抽象類
- 用abstract關鍵字修飾的類叫抽象類,用abstract修飾的方法 就做抽象方法。
- 含有抽象方法的類必須被聲明為抽象類,抽象類必須被繼承,抽象方法必須被重寫!
- 抽象類不能被實例化
- 抽象方法只需聲明,不能實現
十四、final關鍵字
- final的方法不能被重寫
- final的類不能被繼承
- final的變量不能被改變(final成員變量、final局部變量)
final不想被別人繼承
abstract就是想給人繼承
十五、interface 接口
-
多個無關的類可以實現同一個接口
-
一個類可以實現多個無關的接口
-
與繼承關系類似,接口與實現類之間存在多態性
-
接口interface是抽象方法和常量值的定義的集合
-
本質上來說,接口是一種特殊的抽象類,這種抽象類中只包含常量和方法的
定義而沒有變量和方法的實現
接口中聲明的成員變量屬性默認為public static final的;也只能是 public static final的
接口中只能定義抽象方法,而且這些方法默認為public abstract的、也只能是public abstract的
接口可以繼承其他接口,并添加新的屬性和抽象方法
十六、一維數組
Java數組存放在堆中,C++可以存放在棧中。
數組必須先分配空間后,才可以裝值
十七、二維數組
public class Test {public static void main(String[] args) {// 先分配第一維int [][] a = new int[3][];// 再分配第二維a[0] = new int[2];a[1] = new int[4];a[2] = new int[3];} } public class Test {public static void main(String[] args) {int [][] a = {{1,2},{3,4,5,6},{7,8,9}};for(int i = 0; i < a.length; i++){for(int j = 0; j < a[i].length; j++){System.out.print(a[i][j] + " ");}System.out.println();}} }十八、String
解析:
public class Test {public static void main(String[] args) {Person p1 = new Person(1);Person p2 = new Person(2);Person p3 = p1;System.out.println(p1 == p2); //falseSystem.out.println(p3 == p1);//trueSystem.out.println(p1.equals(p2)); // falseSystem.out.println(p1.equals(p3)); // true// public boolean equals(Object obj) { // return (this == obj); // }} }class Person{int id;public Person(int id){this.id = id;} }
十九、 String Split
public static void main(String[] args) {int j = 1234567;String number = String.valueOf(j);System.out.println("j 是" + number.length() + "位數. ");String s = "Mary,F,1976";String[] split = s.split(",");for(String t : split){System.out.println(t);}}二十、 StringBuffer類
StringBuffer()創建一個不包含字符序列的“空”的StringBuffer對象
StringBuffer(String str)創建一個StringBuffer對象,包含與String對象str相同的字符序列
append()方法調用后仍然返回當前字符串的引用
二十二、Math類
二十三、Enum枚舉類型
- 只能取特定值中的一個
- 使用enum關鍵字
- 是java.lang.Enum類型
二十四、Collection容器
- map是一對一對往里裝,左邊的都是一個一個往里裝
重寫equals() 方法一定要重寫hashCode()方法
系統先找hashCode判斷是否相等,然后判斷equals方法!!!
二十五、Iterator接口
接口的繼承
有繼承
有重寫方法
父類引用指向子類對象
暴露這三個方法即可
二十六、List接口
List l1 = new LinkedList<>();for(int i = 0; i <= 5; i++) l1.add("a"+i);System.out.println(l1); //[a0, a1, a2, a3, a4, a5]l1.add(3,"a100");System.out.println(l1);//[a0, a1, a2, a100, a3, a4, a5]l1.set(6,"a200");System.out.println(l1);//[a0, a1, a2, a100, a3, a4, a200]String s =(String) l1.get(2);System.out.println(s); //a2System.out.println(l1.indexOf("a3")); //4l1.remove(1);System.out.println(l1);//[a0, a2, a100, a3, a4, a200] public static void main(String[] args) {List l1 = new LinkedList();List l2 = new LinkedList();for(int i = 0; i < 10; i++) l1.add(i);//隨機排列Collections.shuffle(l1);System.out.println(l1);//[9, 0, 6, 2, 3, 5, 8, 4, 7, 1]//逆序Collections.reverse(l1);System.out.println(l1);//[1, 7, 4, 8, 5, 3, 2, 6, 0, 9]//正序排列Collections.sort(l1);System.out.println(l1);//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]//二分查找System.out.println(Collections.binarySearch(l1,5));//5}二十七、Comparable接口
public class Test {public static void main(String[] args) {Person p1 = new Person("張三", 18, 1);Person p2 = new Person("李四", 16, 2);Person p3 = new Person("王二麻", 22, 3);List list = new LinkedList();list.add(p1);list.add(p2);list.add(p3);Collections.sort(list);System.out.println(list);} } class Person implements Comparable{private String name;private int age;private int id;public Integer getAge() {return age;}public Person(String name, int age, int id) {this.name = name;this.age = age;this.id = id;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", id=" + id +'}';}@Overridepublic int compareTo(Object o) {Person p2 = (Person) o;if(p2.getAge() == this.getAge()) return 0;//向后排,返回正數,向前排,返回負數if(this.getAge() > p2.getAge()) return 1;else return -1;} }這里Comparable參數為null
調用歸并排序?
根據對象的CompareTo方法進行比較
二十八、Map
public static void main(String[] args) {Map m1 = new HashMap();Map m2 = new TreeMap();m1.put("one",1);m1.put("two",2);m1.put("three",3);m2.put("A",1);m2.put("B",2);System.out.println(m1.size()); //3System.out.println(m1.containsKey("one")); //trueSystem.out.println(m2.containsValue(2));//trueMap m3 = new HashMap(m1);m3.putAll(m2);System.out.println(m3);//{A=1, B=2, two=2, three=3, one=1}}二十九、泛型
總結
- 上一篇: lrtimelapse中文教程_LRTi
- 下一篇: 学生成绩管理系统(C语言链表)