JavaEE學習筆記-(1)java基礎

**

1.前言

JaveEE要學好,主要包括這麼幾個大部分:

**

  • java基礎編程
    基礎編程
    面向對象
    集合
    IO
    線程
  • MySQL或者其他數據庫
  • Web前端
  • JavaWeb
    jsp+servlet
  • SSH
    Struts
    Hibernate
    Springs
  • SSM
    Mybatis
    Spring
    SpringMvc
  • 項目實戰

2.說明:本篇整理了基礎java的13個重要知識點,詳細內容請參考其他網站


話不多說,開始第一部分:

@13個基礎的重點知識點

  • 推薦使用的編輯工具:IDEA

1.java中的數據類型:數據的單位

基本數據類型:程序裏面自帶的數據類型
布爾類型:boolean *:true/false
字符類型:char    *: Unciode編碼 -》 不區分中英文
		統一的都是2個字節 = 16個位
		取值範圍:0-65535
		char x = 'a';
		char y = '男';
		char z = 55;//範圍:0-65535
整數類型:byte short int long -> int*  
浮點類型:float double -> 小數  double*

	重點:boolean  char  int/long double
	boolean -> 判斷
	char -> 一個字、一個字母
	int/long -> 整數
	double -> 小數

2.基本數據類型之間的轉換:

取值範圍:小 -》 大
      char
byte  short  int  long  float  double
小的變大的 直接轉變
	int x = 45;
	double y = x;
大的變小的 需要強轉:
	double x = 45.5;
	int y = (int)x;

3.引用數據類型 程序員自己開發的數據類型

main:
創建對象:
Student stu = new Student();
對屬性賦值:
stu.name = "張三";
stu.gender = '男';
stu.stuId = 12345;
stu.study();//學習
//char x = stu.showGender();
System.out.println(stu.showGender());

main:
Student stu = new Student();
System.out.println(stu.name.length());

class Student{
	String name;//null
	char gender;// 
	int stuId;//0
	
	public void study(){
		System.out.println("學習");
	}
	public char showGender(){
		int x = 45;
		return gender;
	}
}

4.java中的變量:
分類:
成員變量 屬性 實例變量:定義在類體裏面
局部變量 自動變量:定義在方法體裏面

區別:
1:定義的位置不同
2:作用範圍不同
	成員變量:依賴於對象存在
	Student stu = new Student();//name gender stuId創建
	局部變量:依賴於方法存在
	stu.XXX();裏面的局部變量創建
3:默認值不同:
	成員變量:即使不賦值 也有默認值
	局部變量:沒有默認值 要求在使用之前必須先賦值

5.java中的分支/判斷:
if else:*****

	if(boolean判斷){
		執行語句;
	}else if(boolean判斷){
		執行語句;
	}else if(boolean判斷){
		執行語句;
	}

	if(x % 2 == 0){
		return "偶數";
	}else{
		return "奇數";
	}
	-》
	if(x % 2 == 0)
		return "偶數";
	return "奇數";

	if(x % 2 == 0){
		return true;
	}else{
		return false;
	}	
	->
	return x % 2 == 0;
	
	只要兩個學生的姓名一樣就是爲相等對象
	public boolean equals(Object obj){
		if(obj == null)return false;
		if(!(obj instanceof Student))return false;
		if(obj == this)return true;

		return this.name.equals(((Student)obj).name));	
	}
	

=====================================
switch case:
	switch(參數){
		case XX : 執行語句;
		case YY : 執行語句;
		default : 執行語句;
	}

	參數只能傳某幾種數據類型:
		jdk1.0 char byte short int
		jdk5.0 enum
		jdk7.0 String
	
	System.out.println("請選擇A/B/C/D");
	Scanner sc = new Scanner(System.in);
	String ABCD = sc.next();
	
	switch(ABCD){
		case "A" : 語法操作1;break;
		case "B" : 語法操作1;break;
		case "C" : 語法操作1;break;
		case "D" : 語法操作1;break;
		default : 語法操作1;
	}

6.java中的循環:

for:
	for(變量初始化;循環條件;循環之後的變化){
				循環執行的代碼
	}
	for(int x = 1;x <= 100;x++){
		if(x % 8 == 0){
			System.out.println(x);
		}
	}
while:
	變量初始化;
	while(循環條件){
		執行語句;
		循環之後的變化;
	}
	int x = 1;
	while(x <= 100){
		if(x % 8 == 0){
			System.out.println(x);
		}
		x++;
	}

do while:
	int x = 1;
	do{
		if(x % 8 == 0){
			System.out.println(x);
		}
		x++;
	}while(x <= 100);

7.數組:
基本概念:數組是容器 裝元素 類型相同 存儲空間連續

int[] data = new int[5];//0 0 0 0 0
int[] data = new int[]{45,77,82,19,90};
int[] data = {56,77,28,99};

得到某一個元素:data[下標]  從0開始
得到數組大小:data.length
	     str.length()
	     list.size()
遍歷數組:
for(int x = 0;x < data.length - 1;x++){
	System.out.println(data[x]);
}

for(int x : data){
	System.out.println(x);
}

數組的複製1:
int[] data = {56,77,28,99};
往數組裏面添加元素:
int[] temp = new int[data.length+1];//0 0 0 0 0 0
System.arraycopy(data,0,temp,0,4);//56 77 28 99 0
temp[temp.length - 1] = 66;
data = temp;

數組的複製2:
int[] data = {44,55,66,77,88};
data = Arrays.copyOf(data,data.length+1);

數組排序:
自動排序:Arrays.sort(數組對象);
          只能升序   import java.util.*;

手動排序 冒泡排序:
	int[] data = {56,88,37,40,99};

	for(int x = 0;x < data.length - 1;x++){//次數
		for(int y = 0;y < data.length - 1;y++){//下標
			if(data[y] > daya[y + 1]){
				int z = data[y];
				data[y] = data[y + 1];
				data[y + 1] = z;
			}
		}
	}

8.封裝:用private修飾屬性和方法

main:
Student stu = new Student();
//stu.name = "";
stu.setName("張三");
System.out.println(stu.getName());

class Student{
	private String name;
	//setter方法修改值
	public void setName(String name){
		this.name = name;
	}

	//getter方法得到值:
	public String getName(){
		return name;
	}
}

9.繼承:用extends實現兩個類之間的is a的關係

共享代碼
class Person{
	String name;
	int age;
	char gender;
}

class Student extends Person{}

10多態:一個對象總有不同的類型去定義它

1:創建對象
       Person x = new Student();

2:放在參數裏面解耦
   public void wc(Student stu){XXX}
   public void wc(Teacher tea){XXX}
   public void wc(Doctor x){XXX}

   ->
   public void wc(Person x){}

   -> Object類裏面有一個方法判斷兩個對象能不能視爲相等對象
      1.equals(Object obj)

11.方法重載 Overload:

條件?
是否發生在同一個類體中
方法名是否一模一樣
參數是否不同【類型不同 個數不同 順序不同】

String:
substring(int 下標):該下標截取到最後
substring(int x,int y):從下標x截取到下標y-1

ArrayList:
remove(int 下標);
remove(Object 元素)

作用
同時滿足用戶的不同需求

12方法覆蓋 Override

條件?
子類在繼承得到父類的某些方法之後 不滿意 在子類裏面重新實現一下

子類權限 >= 父類權限
返回類型
	jdk5.0之前 一模一樣
	jdk5.0開始 斜變返回類型
方法簽名		一模一樣
異常	<= 父類的異常

父類:
protected Object clone(){XXX}
子類:
public Object/Object子類 clone(){}

13.構造方法

每個類都有構造方法 我們不寫 系統提供一個默認構造方法
//Student stu = new Student();

Student stu = new Student("張三",34);
stu.setAge(45);

class Student{
	String name;
	int age;
	public Student(String name,int age){
		this.name = name;
		this.age = age;
	}
	//public Student(){}
}

構造方法的首行:
super():表示要執行本構造方法之前 先去執行父類的構造方法
class Father{
	String name;
	int age;
	public Father(String name,int age){
		this.name = name;
		this.age = age;
	}

	public void showDay(){
		System.out.println("喫飯");
		System.out.println("看電視");
		System.out.println("砍樹");
	}
}

class Son extends Father{
	//name  age
	int stuId;
	
	public Son(String name,int age,int stuId){
		super(name,age);//只能放在構造方法的首行 共享父類構造方法的代碼
		this.stuId = stuId;
	}
	
	@Override
	public void showDay(){
		super.showDay();//放在普通方法裏面任何一行 共享父類普通方法的代碼
		System.out.println("打遊戲");

	}
}

this():要執行本構造方法之前 先去執行本類的其他的構造方法
class Student{
	String name;
	int age;
	int stuId;//成員變量
	
	public Student(String name,int age){
		this.name = name;
		this.age = age;
	}
	
	public Student(String name,int age,int stuId){
		this(name,age);//放在構造方法首行 共享本類其他構造方法的代碼
		this.stuId = stuId;
	}

	public void show(){
		int stuId = 12345;//局部變量
		System.out.println(stuId);
		//this.表示當前調用該方法的對象 沒有位置限制
		System.out.println(this.stuId);
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章