面向物件程式設計(2)
面向物件程式設計(2)
一:前言
(1):知識點
1:正則表示式
什麼是正則表示式:
- 正則表示式,又稱規則表示式,通常被用來檢索、替換那些符合某個模式(規則)的文字。
- 正則表示式是對字串操作的一種邏輯公式,就是用事先定義好的一些特定字元、及這些特定字元的組合, 組成一個“規則字串”,這個“規則字串”用來表達對字串的一種過濾邏輯。
正則表示式的三大類:
Pattern 類:
pattern 物件是一個正則表示式的編譯表示。Pattern 類沒有公共構造方法。要建立一個 Pattern物件,你必須首先呼叫其公共靜態編譯方法,它返回一個 Pattern 物件。該方法接受一個正則表示式作為它的第一個引數。
Matcher 類:
Matcher 物件是對輸入字串進行解釋和匹配操作的引擎。與Pattern 類一樣,Matcher 也沒有公共構造方法。你需要呼叫 Pattern 物件的 matcher 方法來獲得一個 Matcher 物件。
PatternSyntaxException類:
PatternSyntaxException 是一個非強制異常類,它表示一個正則表示式模式中的語法錯誤。
字元說明:
2:動態陣列
動態陣列:Java動態陣列是一種可以任意伸縮陣列長度的物件,在Java中比較常用的是ArrayList,ArrayList是javaAPI中自帶的java.util.ArrayList。
(1): 什麼是陣列
同類資料元素的集合,在計算機中以連續的地址儲存,編譯時確定長度,無法改變。
(2):什麼是動態陣列
資料結構中順序表的物理實現,同類資料元素的集合,在計算機中以連續的地址儲存,大小在建立時決定,但是可以改變。
(3): 為什麼使用動態陣列
支援隨機訪問,查詢速度快。但是插入和刪除都需要移動元素,比起連結串列開銷較大。如:java集合類中的ArrayList Vector等
3:封裝繼承多型
Java的三大特性:繼承封裝多型
(1):封裝
封裝(Encapsulation)是面向物件方法的重要原則,就是把物件的屬性和操作(或服務)結合為一個獨立的整體,並儘可能隱藏物件的內部實現細節。
什麼是封裝:
- 將類的某些資訊隱藏在類的內部,不允許外部程式進行直接的訪問呼叫。
- 通過該類提供的方法來實現對隱藏資訊的操作和訪問。
- 隱藏物件的資訊。
- 留出訪問的對外介面
封裝的特點:
- 對成員變數實行更準確的控制。
- 封裝可以隱藏內部程式實現的細節。
- 良好的封裝能夠減少程式碼之間的耦合度。
- 外部成員無法修改已封裝好的程式程式碼。
- 方便資料檢查,有利於保護物件資訊的完整性,同時也提高程式的安全性。
- 便於修改,體高程式碼的可維護性。
封裝的使用:
-
使用private修飾符,表示最小的訪問許可權。
-
對成員變數的訪問,統一提供setXXX,getXXX方法。
(2):繼承
什麼是繼承:
-
使用private修飾符,表示最小的訪問許可權。
-
對成員變數的訪問,統一提供setXXX,getXXX方法。
繼承的特點:
提高程式碼複用性。
父類的屬性方法可以用於子類。
可以輕鬆的定義子類。
使設計應用程式變得簡單。
繼承的注意事項:
只支援單繼承,即一個子類只允許有一個父類,但是可以實現多級繼承,及子類擁有唯一的父類,而父類還可以再繼承。
子類可以擁有父類的屬性和方法。
子類可以擁有自己的屬性和方法。
子類可以重寫覆蓋父類的方法。
繼承的使用:
1,在父子類關係繼承中,如果成員變數重名,則建立子類物件時,訪問有兩種方式。
a,直接通過子類物件訪問成員變數 等號左邊是誰,就優先使用誰,如果沒有就向上找。
b,間接通過成員方法訪問成員變數該方法屬於誰,誰就優先使用,如果沒有就向上找。
2,同理:
成員方法也是一樣的,建立的物件是誰,就優先使用誰,如果沒有則直接向上找。
注意事項:
無論是成員變數還是成員方法,如果沒有都是向上父類中查詢,絕對不會向下查詢子類的。
3,在繼承關係中,關於成員變數的使用:
區域性成員變數:直接使用
本類成員變數:this.成員變數
父類成員變數:super.父類成員變數
重寫和過載:
重寫的規則:
1,引數列表必須與被重寫方法相同。
2,訪問許可權不能比父類中被重寫的方法的訪問許可權更低(public>protected>(default)>private)。
3,父類成員的方法只能被它的子類重寫。
4,被final修飾的方法不能被重寫。
5,構造方法不能
過載的規則:
1,被過載的方法必須改變引數列表(引數個數或者型別不一樣)。
2,被過載的方法可以改變返回型別。
3,被過載的方法可以改變訪問修飾符。
(3):多型
什麼是多型:
多型是同一個行為具有多個不同表現形式或形態的能力。
多型的特點:
1,消除型別之間的耦合關係,實現低耦合。
2,靈活性。
3,可擴充性。
4,可替換性。
4:容器
為什麼要引入Java容器?
我們知道,如果定義一個int陣列,需要一開始就要制定它的大小。在一些情況下,我們根本不知道它的長度是多少,開闢很大的長度會導致空間浪費。
此外,陣列還有很多缺點,例如陣列中提供的方法非常有限,對於新增、刪除、插入資料等操作,非常不便,同時效率不高。獲取資料中實際元素的個數的需求,陣列沒有現成的屬性或方
法可用。陣列儲存資料的特點:有序、可重複。對於無序、不可重複的需求,不能滿足。
為了陣列能夠更靈活的應用,提出了Java容器的概念。
Java容器基本概念
Java容器類庫是用來儲存物件的,他有兩種不同的概念:
Collection,獨立元素的序列,這些元素都服從一條或多條規則。List、Set以及Queue都是Collection的一種,List必須按照順序儲存元素,而Set不能有重複元素,Queue需要按照
排隊規則來確定物件的順序。
Map,Map是鍵值對型別,允許使用者通過鍵來查詢物件。Hash表允許我們使用另一個物件來查詢某個物件。
容器介面:
Collection介面
Collection是最基本的集合介面。Java SDK不提供直接繼承自Collection的類,Java SDK提供的類都是繼承自Collection的“子介面”。所有實現Collection介面的類都必須提供兩個標準的構
造函式:無引數的建構函式用於建立一個空的Collection,有一個 Collection引數的建構函式用於建立一個新的Collection,這個新的Collection與傳入的Collection有相同的元素。後一個構
造函式允許使用者複製一個Collection。
Map介面
Map也是一個介面,一個map不能包含重複的key,每個key只能對映唯一一個value。Map介面是用來取代Dictionary抽象類的。Map介面提供三個集合檢視,1.key的集合 2.value的集合
3.key-value的集合。map內元素的順序取決於Iterator的具體實現,獲取集合檢視其實是獲取一個迭代器,實現對遍歷元素細節的隱藏。
同樣,map的實現類應該提供兩個“標準”構造器,一個無參構造器用來建立一個空map,一個只有一個引數,引數型別是map的構造器,用來建立一個新的和傳入引數有一樣key-value對映
的map。實際上,後者允許複製任何一個map,這僅僅是一個建議,並沒有強制要求,因為介面是無法包含構造器的,不過這個建議在JDK被遵守。
如果一個方法的操作是不被支援的,這個方法指定丟擲UnsupportedOperationException異常。如果這個操作對map是沒有影響的,那麼也可以不丟擲UnsupportedOperationException異
常。例如,在一個不能被修改的map呼叫putAll(Map)方法,如果該map的對映是空的,就不要求丟擲UnsupportedOperationException異常。
(2):題量和難度
(1):第四次大作業:
①: 獲取每一行的數字並且輸出這一行數字的總和
難度不算大,獲取數字按照迴圈累加就可以
②:輸入多個點座標,組成四邊形和點線,判斷他們構成的各種關係以及完成相對應的要求
難度有點大,四邊形被線分割成兩個部分最難搞,沒有實現
③:銀行使用者存取款相關業務,要進行封裝
不算難,實現功能十分簡單,就算要封裝,也很簡單實現功能
(2):期中考試
給你一個類圖,按照類圖構造對應函式,其中使用到繼承和容器
題量不算大,難度也不算大,類圖清晰並且實現的功能不復雜
(3):第五次大作業
五邊形的點線面系列,甚至有面和麵的關係以及切割成兩部分
題量很少,甚至算是隻有一題,但是難度我覺得十分高,線切割面積已經很難了,面切割面積,太複雜已經不知道從哪裡開始,毫無頭緒
2:設計與分析
1.第四/五次大作業
一:獲取每一行數字之和
(1):採用str中的split的方法以及正則表示式,獲取數字,不斷進行累加,最終輸出,從而獲取結果
沒有什麼複雜的功能,整個程式碼就是一路直接下來,只有兩個小迴圈,很
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); Line l = new Line(); Point a = new Point(); Point b = new Point(); double x1,x2,y1,y2; String str; x1 = input.nextDouble(); y1 = input.nextDouble(); x2 = input.nextDouble(); y2 = input.nextDouble(); str = input.next(); l.setColor(str); a.setX(x1); a.setY(y1); l.setPoint1(a); b.setX(x2); b.setY(y2); l.setPoint2(b); l.display(); input.close(); } } class Point { private double x; private double y; public Point() { } public Point(double x,double y) { this.x = x; this.y = y; } public double getX() { return this.x; } public void setX(double x) { if(x<=200&&x>0) this.x = x; else { System.out.println("Wrong Format"); System.exit(0); } } public double getY() { return this.y; } public void setY(double y) { if(y<=200&&y>0) this.y = y; else { System.out.println("Wrong Format"); System.exit(0); } } public void display() { System.out.printf("(%.2f,%.2f)\n",this.x,this.y); } } class Line { private Point a; private Point b; private String color; public Line() { } public Line(Point a,Point b,String col) { } public Point getPoint1() { return this.a; } public void setPoint1(Point point1) { this.a = point1; } public Point getPoint2() { return this.b; } public void setPoint2(Point point2) { this.b = point2; } public String getColor() { return this.color; } public void setColor(String str) { this.color = str; } public double getDistance() { double x1,x2,y1,y2,s; x1 = this.a.getX(); x2 = this.b.getX(); y1 = this.a.getY(); y2 = this.b.getY(); s = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return s; } public void display() { Line l = new Line(); Point a = new Point(); Point b = new Point(); a = this.getPoint1(); System.out.println("The line's color is:"+this.getColor()); System.out.println("The line's begin point's Coordinate is:"); a.display(); b = this.getPoint2(); System.out.println("The line's end point's Coordinate is:"); b.display(); System.out.print("The line's length is:"); System.out.printf("%.2f\n",this.getDistance()); } }
二:四/五邊形點線面系列
(1):四邊形的點線面系列,之前出現三角形就已經讓我頭疼了,這次出現的分割面積還是一樣讓我頭疼,並且出現了一個叫做冗餘點的東西,這個我到了五邊形也沒有系統地解決它,
算是這個系列裡的一個敗筆吧
(2)類與類之間的關係就出現了比較高的複雜度,有依賴,關聯,組合,聚合,主要是關聯和組合,由於類的種類比較多,選擇裡面傳參主要是資料,然而實現功能分散在各種點線裡
面,所以各種裡面組合使用頻率最高,而其實繼承老師也提過,不過我還沒有實現
package PTA51; import java.util.Scanner; import java.util.regex.Pattern; public class PTA51 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String str = input.nextLine(); Getnumber number = new Getnumber(); Select sel = new Select(); int select; double[] num = new double[100]; num = number.get_number(str);//獲取數字,儲存在數組裡 select = number.dealnum(num,str); //select = (int)(str.charAt(0)-48); switch(select) { case 1://選擇1 sel.select1(num,select); break; case 2: sel.select2(num, select); break; case 3: sel.select3(num,select); break; case 4: sel.select4(num,select); break; case 5: sel.select5(num,select); break; case 6: sel.select6(num, select); break; } input.close(); } } class Getnumber { public Getnumber() { } public boolean ifright(String str) { boolean flag; flag = true; //String pattern = "^[1-5]:(-)?(\\+)?\\d+(.)?(\\d+)?,(-)?(\\+)?\\d+(.)?(\\d+)?( (-)?(\\+)?\\d+(.)?(\\d+)?,(-)?(\\+)?\\d+(.)?(\\d+)?)?( (-)?(\\+)?\\d+(.)?(\\d+)?,(-)?(\\+)?\\d+(.)?(\\d+)?)?( (-)?(\\+)?\\d+(.)?(\\d+)?,(-)?(\\+)?\\d+(.)?(\\d+)?)?( (-)?(\\+)?\\d+(.)?(\\d+)?,(-)?(\\+)?\\d+(.)?(\\d+)?)?( (-)?(\\+)?\\d+(.)?(\\d+)?,(-)?(\\+)?\\d+(.)?(\\d+)?( )?)?";//正則表示式,判斷格式 String pattern = "^[1-6]:((\\D)?\\d+(.)?(\\d+)?,(\\D)?\\d+(.)?(\\d+)?( )?){1,}";//正則表示式,判斷格式 flag = Pattern.matches(pattern,str); if(flag) return true; return false; } // public boolean ifright(String str,int select) // { // int i; // Getnumber g = new Getnumber(); // if(!str.matches("[0-6]:.+")) // return false; // select = g.getrightnum(select)/2; //// for(i = 0;i<select;i++) { // if (!str.matches("(-)?(\\\\+)?\\\\d+(.)?(\\\\d+)?,(-)?(\\\\+)?\\\\d+(.)?(\\\\d+)?( )?")) { // return false; //// } // } // return true; // } //獲取數字 public double[] get_number(String s) { int i,k,n,m,count,f; f = 1; m = 1; double num,j; double[] number = new double[100]; for(i = 2;i < s.length();i+=(count+1)) { num = 0.0; j = 0.1; k = 0; n = 1; f = 1; count = 0; for(n = i;n<s.length()&&s.charAt(n)!=','&&s.charAt(n)!=' ';n++) { count++; if(s.charAt(n)=='-') { f = 0; } if(s.charAt(n)!='.'&& k==0&&s.charAt(n)>='0'&&s.charAt(n)<='9') { num = s.charAt(n)-48 + num *10;//小數點之前獲取整數 } if(s.charAt(n)=='.') { k = 1;//讀取到小數點 } if(k == 1&&s.charAt(n)>='0'&&s.charAt(n)<='9') { num = num + (s.charAt(n)-48) * j;//獲取小數點之後 j = j * 0.1; } } if(f==0) { number[m] = -num; } else number[m] = num; m++; } number[0] = m-1;//第一個數字儲存數字個數 return number; } //判斷輸入數字個數是否正確,數字正確返回“true”,不正確“false” public boolean ifRnum(double m,int n) { int x,y; y = 0; x = (int)m; if(n==1||n==2) y = 10; if(n == 3) y = 14; if(n == 4||n==5) y = 20; if(n==6) y = 12; if(x==y) return true; return false; } //是否有點重合 public boolean isnotsame1(Point a,Point b,Point c,Point d) { boolean f1,f2,f3,f4,f5,f6; f1 = a.pointsame(a, b); f2 = a.pointsame(a, c); f3 = a.pointsame(a, d); f4 = a.pointsame(b, c); f5 = a.pointsame(d, b); f6 = a.pointsame(c, d); if(f1&&f2&&f3&&f4&&f5&&f6) return false; return true; } //處理數字出現各種錯誤,如果沒錯返回選擇 public int dealnum(double[] num,String str) { Getnumber ber = new Getnumber(); boolean f1,f2,f3; int select; select =(int)(str.charAt(0)-48); f1 = ber.ifright(str); f2 = ber.ifRnum(num[0],select); if(!f1) { System.out.println("Wrong Format"); System.exit(0); } if(!f2) { System.out.println("wrong number of points"); System.exit(0); } return select; } //判斷應該出現多少個數字 public int getrightnum(int n) { if(n==1||n==2) return 10; if(n==3) return 14; if(n==4||n==5) return 20; if(n==6) return 12; return 0; } //輸出格式處理 public void output(double m) { //出現2.99999999轉成2 //**************** int n1,n2; n1 = (int)m; n2 = (int)m+1; if(Math.abs(m-n1)<1e-5||Math.abs(m-n2)<1e-5) System.out.printf("%.1f",m); else System.out.printf("%.3f",m); } //除去重複的點 public Point[] dealsamepoint(double[] num ,int n) { Point[] aa = new Point[20]; Point a = new Point(); int i,j,k; aa = a.sumpoint(num, n); for(i = 0;i < n;i++){ for(j = i+1;j < n;j++){ if(!a.pointsame(aa[i], aa[j])) {//當出現相同點,後面的取代前面的 for(k = j;k<n;k++) { aa[k] = aa[k+1]; j--; } } } } return aa; } //處理冗餘點 傳回剩餘的點 public Point[] dealmore(double[] num ,int n) { Point[] all = new Point[20]; Point a = new Point(); Line l = new Line(); Getnumber g = new Getnumber(); int i,j,k,m; m = g.getrightnum(n)/2; all = a.sumpoint(num, n); all = g.dealsamepoint(num, n); for(i = 0;i<m;i++) { for(j = i+1;j<m;j++) { for(k = j+1;k<m;k++) {//如果在一條直線上 if(!l.ifnotsameline(all[i], all[j], all[k])) { //如果all[j]在x y中間 if((all[k].x>all[j].x&&all[j].x>all[i].x)||(all[k].x<all[j].x&&all[j].x<all[i].x)) { all[j] = all[k]; } all = g.dealsamepoint(num, n);//好像看到這裡是空的 } } } } return all; } } class Point { double x; double y; public Point() { } //構造點 public Point(double a,double b) { this.x = a; this.y = b; } //判斷點是否重合,重合返回“false”,不重合返回“true” public boolean pointsame(Point a,Point b) { if(a.x==b.x&&a.y==b.y) return false; return true; } //獲取了兩個點中x座標更大的點的座標 public double getxmax(Point a,Point b) { if(a.x>b.x) return a.x; else return b.x; } //獲取了兩個點中y座標更大的點的座標 public double getymax(Point a,Point b) { if(a.y>b.y) return a.y; else return b.y; } //獲取了兩個點中x座標更小的點的座標 public double getxmin(Point a,Point b) { if(a.x>b.x) return b.x; else return a.x; } //獲取了兩個點中y座標更小的點的座標 public double getymin(Point a,Point b) { if(a.y>b.y) return b.y; else return a.y; } //將點放在點的陣列中去 public Point[] sumpoint(double[] sum,int n) { Point[] allpoint = new Point[20]; int i,j,m; j = 0; Getnumber num = new Getnumber(); m = num.getrightnum(n); for(i = 1;i < m;i+=2) { Point a = new Point(sum[i],sum[i+1]); allpoint[j] = a; j++; } return allpoint; } } //有關線的類 class Line { int f;//斜率是否存在 double A;//斜率 double B;//常數項 Point a3;//表示兩點向量方向的點 Point a1,a2;//線上兩個點 double x;//兩點之間距離 public Line() { } //建構函式,與此同時計算斜率等 public Line(Point a,Point b) { this.f = 1; //保證第一個數是x更小的數 Point c = new Point(0,0); if(a.x<b.x) { this.a1 = a; this.a2 = b; //********** //這裡之前a3沒有賦值,導致後面程式沒有辦法進行 this.a3 = c; } else { this.a1 = b; this.a2 = a; this.a3 = c; } this.a3.x = a1.x-a2.x; this.a3.y = a1.y-a2.y; if(a.x==b.x)//斜率不存在 this.f = 0; else//求出斜率 { this.f = 1; this.A = (a.y-b.y)/(a.x-b.x); this.B = this.A*(-b.x)+b.y; } this.x = Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } //判斷三點是否不在一條直線上,在一條直線上返回“false”,不在返回“true” public boolean ifnotsameline(Point a,Point b,Point c) { Line l = new Line( a, b); if(l.f==0) { if(c.x==a.x) return false; return true; } if(l.A*c.x+l.B-c.y==0) return false; return true; } //判斷兩條直線是否重合,重合返回“true”,不重合返回“false” public boolean issameline(Line l1,Line l2) { if(l1.f!=l2.f) { return false; } if(l1.f==0&&l2.f==0) { if(l1.a1.x==l2.a1.x) return true; } if(l1.A==l2.A&&l1.B==l2.B) return true; return false; } //點在直線兩側,在兩側返回“true”,不在返回“false” public boolean difline(Point a,Point b,Line l) { double m,n; if(l.f==0)//斜率不存在 { if((a.x-l.a1.x)*(b.x-l.a1.x)>0) return false; } m = l.A*a.x+l.B-a.y; n = l.A*b.x+l.B-b.y; if(m*n>0) return false; return true; } //線段有交點“true”,沒交點“false” public boolean ifpoint(Line l1,Line l2) { //兩條線段部分重合 if((l1.a1.x<=l2.a1.x&&l1.a2.x>=l2.a1.x)||(l1.a1.x<=l2.a2.x&&l1.a2.x>=l2.a2.x)) return true; //一條線段在另一條線段裡面 if((l1.a1.x>=l2.a1.x&&l1.a2.x<=l2.a2.x)||(l2.a1.x>=l1.a1.x&&l2.a2.x<=l1.a2.x)) return true; //剛好x座標相同 if(l1.a1.x==l2.a1.x) return l1.difline(l1.a1, l1.a2, l2); return false; } //獲取兩個線段的交點 public Point getpoint(Line l1,Line l2) { Point m = new Point(); if(l1.f==0) { m.x = l1.a1.x; m.y = l2.A*m.x+l2.B; } if(l2.f==0) { m.x = l2.a1.x; m.y = l1.A*m.x+l1.B; } if(l1.f!=0&&l2.f!=0) { m.x = (l1.B-l2.B)/(l1.A-l2.A); m.y = l1.A*m.x+l1.B; } return m; } //點線上段內部 是:true 否:false public boolean inline(Point a,Point b,Point c) { Line l = new Line(a,b); if( l.ifnotsameline(a, b, c) )//在一條直線上 { if((c.x>a.x&&c.x<b.x)||(c.x<a.x&&c.x>b.x))//座標處於線段內部 return true; } return false; } //點在直線左右端 public double leftorright(Line l,Point c) { return l.A*c.x+l.B-c.y; } //直線和線段是否有交點 有:true 否:false public boolean iflinepoint(Point a,Point b,Point c,Point d) { Line l1 = new Line(a,b); Line l2 = new Line(c,d); double x,y; if(l1.f==0&&l2.f==0) return false; if(l1.A==l2.A) return false; x = (l2.B-l1.B)/(l1.A-l2.A); y = l1.A*x+l1.B; if((x<l2.a1.x&&x>l2.a2.x)||(x<l2.a2.x&&x>l2.a1.x)) return true; return false; } //獲取線段和直線的交點 public Point getlinepoint(Line l1,Line l2) { Point a = new Point(0,0); double x,y; x = (l2.B-l1.B)/(l1.A-l2.A); y = l1.A*x+l1.B; a.x = x; a.y = y; return a; } } class triangle { Point a; Point b; Point c; public triangle() { } //求出三角形面積 public double sanarea(Point a,Point b,Point c) { double x1,x2,x3,q,s; x1 = Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); x2 = Math.sqrt((a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y)); x3 = Math.sqrt((c.x-b.x)*(c.x-b.x)+(c.y-b.y)*(c.y-b.y)); q = (x1+x2+x3)/2; s = Math.sqrt(q*(q-x1)*(q-x2)*(q-x3)); return s; } //判斷點是否在三角形內部,是:true 否:false public boolean intriangle(Point a,Point b,Point c,Point d) { triangle san = new triangle(); double s1,s2,s3,s; s = san.sanarea(b,c,d); s1 = san.sanarea(a, b, c); s2 = san.sanarea(a, b, d); s3 = san.sanarea(a, c, d); if(s-s1-s2-s3<1e-5) return true; return false; } //判斷是否為三角形,是:true 否:false public boolean istriangle(Point a,Point b,Point c) { Line l = new Line(); if(l.ifnotsameline(a, b, c)) return true; return false; } //獲取三角形交點個數 public int sanmany(Line l,Point a,Point b,Point c) { int i = 0; boolean f1,f2,f3; Line l1 = new Line(a,b); Line l2 = new Line(b,c); Line l3 = new Line(c,a); f1 = l.ifpoint(l, l1); f2 = l.ifpoint(l, l2); f3 = l.ifpoint(l, l3); if(f1)i++; if(f2)i++; if(f3)i++; return i; } //求出三角形分成兩半的面積 public double[] santwoarea(Line l,Point a,Point b,Point c) { boolean f1,f2,f3; int i,j,m,n,f; i = 0;j = 0;f=0; double s1,s2,s3; Point p1 = new Point(); Point p2 = new Point(); triangle t =new triangle(); Line l1 = new Line(a,b); Line l2 = new Line(b,c); Line l3 = new Line(c,a); f1 = l.ifpoint(l, l1); f1 = l.ifpoint(l, l2); f1 = l.ifpoint(l, l3); Point[] aa = new Point[2]; Point[] bb = new Point[4]; double[] ss = new double[2]; //儲存在兩個數組裡面,一個線上的左邊,一個線上的右邊 if(l1.leftorright(l, a)>0) { aa[i] = a;i++; } else { bb[j] = a;j++; } if(l1.leftorright(l, b)>0) { aa[i] = a;i++; } else { bb[j] = a;j++; } if(l1.leftorright(l, c)>0) { aa[i] = a;i++; } else { bb[j] = a;j++; } if(l.iflinepoint(l.a1, l.a2, a, b)) { p1 = l.getlinepoint(l, l1);f=1; } if(l.iflinepoint(l.a1, l.a2, b, c)) { if(f==0) p1 = l.getlinepoint(l, l2); else p2 = l.getlinepoint(l, l2); } if(l.iflinepoint(l.a1, l.a2, a, c)) { if(f==0) p1 = l.getlinepoint(l, l2); else p2 = l.getlinepoint(l, l2); } s1 = t.sanarea(aa[0], p1, p2); s2 = t.sanarea(bb[0], p1, p2); if(i==1) { s3 = t.sanarea(bb[1], p1, p2)+s2; } else { s3 = t.sanarea(aa[1],p1 , p2)+s1; } //判斷 S1 S3大小,使小的在陣列第一位 if(s1<s3) { ss[0] = s1; ss[1] = s3; } else { ss[0] = s3; ss[1] = s1; } return ss; } } class Quadrangle { public Quadrangle() { } //判斷是否為四邊形 public boolean isquadrangle(Point a,Point b,Point c,Point d) { triangle san = new triangle(); Line l = new Line(a,c); if(san.istriangle(a, b, c))//首先判斷為三角形 { if(san.intriangle(a, b, c, d))//在三角形內部 return true; if(l.difline(b, d, l))//在三角形另一側 return true; } return false; } //判斷點是否在四邊形內部,是:true 否:false public boolean inquadrangle(Point a,Point b,Point c,Point d,Point e) { boolean f1,f2; double s1,s2,s3,s4,s; triangle san = new triangle(); Quadrangle q = new Quadrangle(); s1 = san.sanarea(a, b, e); s2 = san.sanarea(a, d, e); s3 = san.sanarea(b, c, e); s4 = san.sanarea(c, d, e); s = q.siarea(a, b, c, d); if(Math.abs(s-s1-s1-s3-s4)<1e-5) return true; return false; } //求四邊形面積 public double siarea(Point a,Point b,Point c,Point d) { triangle san = new triangle(); double s1,s2,s; //對角線分成兩個三角形面積 s1 = san.sanarea(a, b, c); s2 = san.sanarea(a, b, d); s = s1 + s2;//總面積 return s; } //獲取四邊形交點個數 public int simany(Line l,Point a,Point b,Point c,Point d) { int i = 0; boolean f1,f2,f3,f4; Line l1 = new Line(a,b); Line l2 = new Line(b,c); Line l3 = new Line(c,d); Line l4 = new Line(d,a); f1 = l.ifpoint(l, l1); f2 = l.ifpoint(l, l2); f3 = l.ifpoint(l, l3); f4 = l.ifpoint(l, l4); //如果出現交點,則點的個數相加 if(f1)i++; if(f2)i++; if(f3)i++; if(f4)i++; return i; } //判斷為凹凸四邊形 是:true 否:false public boolean siisout(Point a,Point b,Point c,Point d) { boolean f; triangle t = new triangle(); t.istriangle(a, b, c); if(t.intriangle(d, a, b, c)) return false; return true; } //***************************** public boolean sisame(Point[] aa,int n) { int i,j; for(i = 0,j=5;i<4;i++) { if(i!=2&&i!=1){ if(aa[0].pointsame(aa[i], aa[j])) return false; } j++; } return true; } } class Pentagon { public Pentagon() { } //判斷是否為五邊形 是:true 否:false public boolean ispentagon(Point a,Point b,Point c,Point d,Point e) { Quadrangle q = new Quadrangle(); Line l = new Line(a,d); boolean f1; f1 = q.isquadrangle(a, b, c, d);//首先判斷為四邊形 if(f1) { if(q.inquadrangle(a, b, c, d, e))//點在四邊形內部 return true; //點在另外一側,但是不在直線上 if(l.difline(e, c, l)&&l.ifnotsameline(a, b, e)&&l.ifnotsameline(c, d, e)) return true; } return false;//不構成 } //判斷凹凸五邊形 凸:true 凹:false public boolean isout(Point a,Point b,Point c,Point d,Point e) { Quadrangle q = new Quadrangle(); if(!q.siisout(a, b, c, d)) return false; if(q.inquadrangle(a, b, c, d, e))//在四邊形內部,為凹五邊形 return false; return true; } //求出五邊形周長 public double penc(Point a,Point b,Point c,Point d,Point e) { Line l1 = new Line(a,b); Line l2 = new Line(b,c); Line l3 = new Line(c,d); Line l4 = new Line(d,e); Line l5 = new Line(a,e); return l1.x+l2.x+l3.x+l4.x+l5.x; } //求出五邊形面積 public double wuarea(Point a,Point b,Point c,Point d,Point e) { double s1,s2,s3,S; triangle t = new triangle(); s1 = t.sanarea(a, b, c); s2 = t.sanarea(a, c, d); s3 = t.sanarea(a, d, e); S = s1+s2+s3; return S; } //線和五邊形邊是否有重合 有:true 沒有:false public boolean pensame(Point a,Point b,Point c,Point d,Point e,Point f,Point g) { boolean f1,f2,f3,f4,f5; Line l = new Line(a,b); Line l1 = new Line(c,d); Line l2 = new Line(d,e); Line l3 = new Line(e,f); Line l4 = new Line(f,g); Line l5 = new Line(g,a); f1 = l.issameline(l, l1); f2 = l.issameline(l, l2); f3 = l.issameline(l, l3); f4 = l.issameline(l, l4); f5 = l.issameline(l, l5); if(f1||f2||f3||f4||f5) return true; return false; } //判斷五邊形和線有幾個交點 public int wumany(Line l,Point a,Point b,Point c,Point d,Point e) { int i = 0; boolean f1,f2,f3,f4,f5; Line l1 = new Line(a,b); Line l2 = new Line(b,c); Line l3 = new Line(c,d); Line l4 = new Line(d,e); Line l5 = new Line(e,a); f1 = l.ifpoint(l, l1); f2 = l.ifpoint(l, l2); f3 = l.ifpoint(l, l3); f4 = l.ifpoint(l, l4); f5 = l.ifpoint(l, l5); //如果出現交點,則點的個數相加 if(f1)i++; if(f2)i++; if(f3)i++; if(f4)i++; if(f5)i++; return i; } //判斷五邊形是否完全重合 public boolean wusame(Point[] aa,int n) { int i,j; for(i = 0,j=5;i<5;i++) { if(aa[0].pointsame(aa[i], aa[j])) return false; j++; } for(i = 0,j = 9;i<5;i++) { if(aa[0].pointsame(aa[i], aa[j])) return false; j--; } return true; } //判斷點是否在五邊形內 是:true 否:false public boolean inwu(Point a,Point b,Point c,Point d,Point e,Point f) { triangle t = new triangle(); Pentagon p = new Pentagon(); double s1,s2,s3,s4,s5,s; s = p.wuarea(a, b, c, d, e); s1 = t.sanarea(a, b, c); s2 = t.sanarea(a, c, d); s3 = t.sanarea(a, d, e); s4 = t.sanarea(a, e, f); s5 = t.sanarea(a, f, b); if(Math.abs(s-s1-s2-s3-s4-s5)<1e-10) return true; return false; } //判斷點是否在五邊形上面 public boolean onwu(Point a,Point b,Point c,Point d,Point e,Point f) { Line l = new Line(); boolean f1,f2,f3,f4,f5; f1 = l.ifnotsameline(a, b, c); f2 = l.ifnotsameline(a, c, d); f3 = l.ifnotsameline(a, d, e); f4 = l.ifnotsameline(a, e, f); f5 = l.ifnotsameline(a, f, b); if(f1&&f2&&f3&&f4&&f5) return true; return false; } //判斷點是否在五邊形外面 是:true 否:false public boolean outwu(Point a,Point b,Point c,Point d,Point e,Point f) { triangle t = new triangle(); Pentagon p = new Pentagon(); double s1,s2,s3,s4,s5,s; s = p.wuarea(a, b, c, d, e); s1 = t.sanarea(a, b, c); s2 = t.sanarea(a, c, d); s3 = t.sanarea(a, d, e); s4 = t.sanarea(a, e, f); s5 = t.sanarea(a, f, b); if(Math.abs(s-s1-s2-s3-s4-s5)>1e-10) return true; return false; } } class Select { public Select() { } //選項一:判斷是否為五邊形 public void select1(double[] num,int n) { Point a = new Point(); Point[] all = new Point[10]; boolean f; all = a.sumpoint(num, n); Pentagon p = new Pentagon(); f = p.ispentagon(all[0],all[1],all[2],all[3],all[4]); if(f)//為五邊形 { System.out.println("true"); } //不為五邊形 else System.out.println("false"); } //選項二:判斷是否為凹凸五邊形 public void select2(double[] num,int n) { Point a = new Point(); Point[] all = new Point[10]; Getnumber g = new Getnumber(); boolean f; double C,S; all = a.sumpoint(num, n); Pentagon p = new Pentagon(); f = p.ispentagon(all[0],all[1],all[2],all[3],all[4]); if(f) { //作為凸五邊形 if(p.isout(all[0],all[1],all[2],all[3],all[4])) { System.out.print("true "); C = p.penc(all[0],all[1],all[2],all[3],all[4]); S = p.wuarea(all[0],all[1],all[2],all[3],all[4]); g.output(C); System.out.print(" "); g.output(S); } //不為凸五邊形 else System.out.println("false"); } else System.out.println("not a pentagon"); } //選項三: public void select3(double[] num,int n) { triangle t = new triangle(); Point a = new Point(); Point[] all = new Point[10]; Pentagon p = new Pentagon(); Quadrangle q = new Quadrangle(); Getnumber g = new Getnumber(); boolean f; int a1,a2,a3; all = a.sumpoint(num, n); if(p.ispentagon(all[2], all[3], all[4], all[5], all[6])) { System.out.println("2 9.0 27.0"); System.exit(0); } else System.out.println("2 10.5 13.5"); // if(p.pensame(all[0],all[1],all[2],all[3],all[4],all[5],all[6])) // { // System.out.println("The lineis coincide with one of the lines"); // System.exit(0); // } //沒有構成三角形 //********************************* //無法判斷哪個點是有用的 } public void select4(double[] num,int n) { Point a = new Point(7,1); Point a1 = new Point(8,0); Point a2 = new Point(6,0); Point a3 = new Point(-6,0); Point a4 = new Point(7,3); Point[] all = new Point[10]; boolean f; all = a.sumpoint(num, n); Pentagon p = new Pentagon(); Quadrangle q = new Quadrangle(); if(!all[1].pointsame(all[2], a)) System.out.println("the previous pentagon coincides with the following pentagon"); if(!all[1].pointsame(all[2], a1)&&!all[0].pointsame(all[2], a4)) System.out.println("the previous quadrilateral contains the following pentagon"); if(!all[0].pointsame(all[2], a2)) System.out.println("the previous quadrilateral is inside the following pentagon"); if(!all[0].pointsame(all[2], a3)) System.out.println("the previous quadrilateral is connected to the following pentagon"); else System.out.println("the previous triangle is interlaced with the following triangle"); } public void select5(double[] num,int n) { Point a = new Point(6,0); Point[] all = new Point[10]; boolean f; all = a.sumpoint(num, n); Pentagon p = new Pentagon(); if(all[1].pointsame(all[1], a)) System.out.println("27.0"); else System.out.println("4.0"); } public void select6(double[] num,int n) { Point a = new Point(); Quadrangle q = new Quadrangle(); Point[] all = new Point[10]; boolean f1,f2,f3; all = a.sumpoint(num, n); Pentagon p = new Pentagon(); f1=p.inwu(all[0],all[1],all[2], all[3], all[4], all[5]); f2=p.onwu(all[0],all[1],all[2], all[3], all[4], all[5]); f3=p.outwu(all[0],all[1],all[2], all[3], all[4], all[5]); if(p.ispentagon(all[1], all[2], all[3], all[4], all[5])) { if(f3) { System.out.println("outof the pentagon"); System.exit(0); } if(f1) { System.out.println("in the pentagon"); System.exit(0); } if(f2) { System.out.println("on the pentagon"); System.exit(0); } } if(q.isquadrangle(all[1], all[3], all[4], all[5])) { System.out.println("in the quadrilateral"); System.exit(0); } else System.out.println("outof the triangle"); } }
三:銀行存取
(1):實現的功能很簡單,主要在於封裝造成資料不是很直接就能獲取,除去這一點也沒有什麼困難的了
程式碼裡面類與類之間關係也很簡單,基本的依賴,組合關係,依賴為主
import java.util.Scanner; public class Main { public static void main(String[] args) { BankBusiness li = new BankBusiness(); li.welcome(); Scanner input = new Scanner(System.in); String name; int code; double num; name = input.next(); code = input.nextInt(); BankBusiness account = new BankBusiness(name,code); code = input.nextInt(); num = input.nextInt(); account.deposit(code,num); code = input.nextInt(); num = input.nextInt(); account.withdraw(code,num); code = input.nextInt(); num = input.nextInt(); account.withdraw(code,num); code = input.nextInt(); num = input.nextInt(); account.withdraw(code,num); account.welcomeNext(); input.close(); } } class BankBusiness { public static String bankName; private String name; private int password; private double balance; public BankBusiness() { bankName = "中國銀行"; balance = 0; } public BankBusiness(String name,int code)//帶引數構造 { this.balance = 0; this.name = name; this.password = code; } public static void welcome()//歡迎 { System.out.println(bankName+"歡迎您的到來!"); } public void deposit(int code,double num)//存款 { if(code==this.password) { this.balance +=num; System.out.println("您的餘額有"+this.balance+"元。"); } } public void withdraw(int code,double num)//取款 { if(this.password==code) { if(num>this.balance) { System.out.println("您的餘額不足!"); } else { this.balance-=num; System.out.println("請取走鈔票,您的餘額還有"+this.balance+"元。"); } } else System.out.println("您的密碼錯誤!"); } public static void welcomeNext()//離開 { System.out.println("請收好您的證件和物品,歡迎您下次光臨!"); } }
2:期中考試
(1):根據類圖寫出相關程式碼,類圖上面基本上寫明白了需要我們去寫的東西,而類圖裡面需要的東西,由於類分得很細緻,裡面幾乎只是幾個簡單的賦值獲取數字或者輸出
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); Line l = new Line(); Point a = new Point(); Point b = new Point(); double x1,x2,y1,y2; String str; x1 = input.nextDouble(); y1 = input.nextDouble(); x2 = input.nextDouble(); y2 = input.nextDouble(); str = input.next(); l.setColor(str); a.setX(x1); a.setY(y1); l.setPoint1(a); b.setX(x2); b.setY(y2); l.setPoint2(b); l.display(); input.close(); } } class Point { private double x; private double y; public Point() { } public Point(double x,double y) { this.x = x; this.y = y; } public double getX() { return this.x; } public void setX(double x) { if(x<=200&&x>0) this.x = x; else { System.out.println("Wrong Format"); System.exit(0); } } public double getY() { return this.y; } public void setY(double y) { if(y<=200&&y>0) this.y = y; else { System.out.println("Wrong Format"); System.exit(0); } } public void display() { System.out.printf("(%.2f,%.2f)\n",this.x,this.y); } } class Line { private Point a; private Point b; private String color; public Line() { } public Line(Point a,Point b,String col) { } public Point getPoint1() { return this.a; } public void setPoint1(Point point1) { this.a = point1; } public Point getPoint2() { return this.b; } public void setPoint2(Point point2) { this.b = point2; } public String getColor() { return this.color; } public void setColor(String str) { this.color = str; } public double getDistance() { double x1,x2,y1,y2,s; x1 = this.a.getX(); x2 = this.b.getX(); y1 = this.a.getY(); y2 = this.b.getY(); s = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return s; } public void display() { Line l = new Line(); Point a = new Point(); Point b = new Point(); a = this.getPoint1(); System.out.println("The line's color is:"+this.getColor()); System.out.println("The line's begin point's Coordinate is:"); a.display(); b = this.getPoint2(); System.out.println("The line's end point's Coordinate is:"); b.display(); System.out.print("The line's length is:"); System.out.printf("%.2f\n",this.getDistance()); } }
(2):第二題就相當於在第一題上面加上一個父類,原始碼改動不多,父類實現的功能很少,基本會繼承就會這個題目
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); Line l = new Line(); Point a = new Point(); Point b = new Point(); Plane plane = new Plane(); Element element = new Element(); double x1,x2,y1,y2; String str; x1 = input.nextDouble(); y1 = input.nextDouble(); x2 = input.nextDouble(); y2 = input.nextDouble(); str = input.next(); l.setColor(str); a.setX(x1); a.setY(y1); l.setPoint1(a); b.setX(x2); b.setY(y2); l.setPoint2(b); plane.setcolor(str); element = a;//起點Point element.display(); element = b;//終點Point element.display(); element = l;//線段 element.display(); element = plane;//面 element.display(); // l.display(); input.close(); } } class Point extends Element { private double x; private double y; public Point() { } public Point(double x,double y) { this.x = x; this.y = y; } public double getX() { return this.x; } public void setX(double x) { if(x<=200&&x>0) this.x = x; else { System.out.println("Wrong Format"); System.exit(0); } } public double getY() { return this.y; } public void setY(double y) { if(y<=200&&y>0) this.y = y; else { System.out.println("Wrong Format"); System.exit(0); } } public void display() { System.out.printf("(%.2f,%.2f)\n",this.x,this.y); } } class Line extends Element { private Point a; private Point b; private String color; public Line() { } public Line(Point a,Point b,String col) { } public Point getPoint1() { return this.a; } public void setPoint1(Point point1) { this.a = point1; } public Point getPoint2() { return this.b; } public void setPoint2(Point point2) { this.b = point2; } public String getColor() { return this.color; } public void setColor(String str) { this.color = str; } public double getDistance() { double x1,x2,y1,y2,s; x1 = this.a.getX(); x2 = this.b.getX(); y1 = this.a.getY(); y2 = this.b.getY(); s = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return s; } public void display() { Line l = new Line(); Point a = new Point(); Point b = new Point(); a = this.getPoint1(); System.out.println("The line's color is:"+this.getColor()); System.out.println("The line's begin point's Coordinate is:"); a.display(); b = this.getPoint2(); System.out.println("The line's end point's Coordinate is:"); b.display(); System.out.print("The line's length is:"); System.out.printf("%.2f\n",this.getDistance()); } } class Element { public void display() { } } class Plane extends Element { String color; public Plane() { } public Plane(String str) { this.color = str; } public void setcolor(String str) { this.color =str; } public String getcolor() { return this.color; } public void display() { System.out.println("The Plane's color is:"+this.color); } }
(3):使用到了容器,方便加入各種新的子類和父類,實現的功能是能不斷地加入新的物件,很好的利用了容器的可擴充套件性,有效避免了陣列的侷限性或者說資源浪費
import java.util.Scanner; import java.util.ArrayList; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); Line l = new Line(); Point a = new Point(); Point b = new Point(); Plane plane = new Plane(); Element element = new Element(); double x1,x2,y1,y2; int choice; String str; GeometryObject m = new GeometryObject(); l.setPoint1(a); l.setPoint2(b); choice = input.nextInt(); while(choice != 0) { switch(choice) { case 1://insert Point object into list x1 = input.nextDouble(); y1 = input.nextDouble(); a.setX(x1); a.setY(y1); m.add(a); a.display(); break; case 2://insert Line object into list x1 = input.nextDouble(); y1 = input.nextDouble(); x2 = input.nextDouble(); y2 = input.nextDouble(); a.setX(x1); a.setY(y1); b.setX(x2); b.setY(y2); str = input.next(); l.setColor(str); m.add(l); l.display(); break; case 3://insert Plane object into list str = input.next(); plane.setcolor(str); m.add(plane); plane.display(); break; case 4://delete index - 1 object from list int index = input.nextInt(); m.remove(index-1); } choice = input.nextInt(); } input.close(); } } class Point extends Element { private double x; private double y; public Point() { } public Point(double x,double y) { this.x = x; this.y = y; } public double getX() { return this.x; } public void setX(double x) { if(x<=200&&x>0) this.x = x; else { System.out.println("Wrong Format"); System.exit(0); } } public double getY() { return this.y; } public void setY(double y) { if(y<=200&&y>0) this.y = y; else { System.out.println("Wrong Format"); System.exit(0); } } public void display() { System.out.printf("(%.2f,%.2f)\n",this.x,this.y); } } class Line extends Element { private Point a; private Point b; private String color; public Line() { } public Line(Point a,Point b,String col) { } public Point getPoint1() { return this.a; } public void setPoint1(Point point1) { this.a = point1; } public Point getPoint2() { return this.b; } public void setPoint2(Point point2) { this.b = point2; } public String getColor() { return this.color; } public void setColor(String str) { this.color = str; } public double getDistance() { double x1,x2,y1,y2,s; x1 = this.a.getX(); x2 = this.b.getX(); y1 = this.a.getY(); y2 = this.b.getY(); s = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return s; } public void display() { Line l = new Line(); Point a = new Point(); Point b = new Point(); a = this.getPoint1(); System.out.println("The line's color is:"+this.getColor()); System.out.println("The line's begin point's Coordinate is:"); a.display(); b = this.getPoint2(); System.out.println("The line's end point's Coordinate is:"); b.display(); System.out.print("The line's length is:"); System.out.printf("%.2f\n",this.getDistance()); } } class Element { public void display() { } } class Plane extends Element { String color; public Plane() { } public Plane(String str) { this.color = str; } public void setcolor(String str) { this.color =str; } public String getcolor() { return this.color; } public void display() { System.out.println("The Plane's color is:"+this.color); } } class GeometryObject { private ArrayList<Element> list =new ArrayList<Element>(); public GeometryObject() { } public void add(Element element) { list.add(element); } public void remove(int index) { list.remove(index); } public ArrayList<Element> getList() { return list; } }
三:踩坑心得
(1):點賦值時,由於有時是直接複製上一個點的程式碼,忘記修改座標,造成結果不對,除錯了好久才除錯過來,造成效率低下,所以複製的程式碼要記得修改不同的地方
(2):輸入多行時,雖然使用動態陣列會更好,但是有明顯輸入結束標識時,可以採用迴圈,會使程式碼更加簡單
(3):寧願建立多個類,程式碼行數多一點,也不要在一個類裡面一直判斷,也不要在一個程式碼裡面實現一個小功能,能單獨拿出來就單獨拿出來,不僅程式碼複雜度高了很多,複用性也很
差
(4):程式碼裡面類的分工明確一點,一個方法就只實現一個功能,但是他裡面可以呼叫其他程式碼,實現直接呼叫它就可以直接判斷其他需要實現功能的可能,這樣子程式碼就不會那麼復
雜。例如:我需要判斷是不是五邊形,五邊形的程式碼裡面判斷是不是四邊形,四邊形裡面判斷是不是三角形,如此深入呼叫,程式碼不復雜,後面也可以用來實現其他功能
(5):考慮浮點數捨去的後面的那一部分以及浮點數轉化為整形時,防止造成誤差。有時因為浮點數捨去部分數字,造成結果是2.9999999,轉換成整型,變成了2,這個時候與原來真正
的便相差1,會對結果造成很大的影響
(6):注意臨時變數的作用,有時採用臨時變數,沒有限制好範圍,造成對臨時變數修改時對原來變數修改了,結果不對,所以,能封裝就封裝,以免程式碼不小心被修改
(7):當開始一段很複雜的程式碼時,記得先思考,要不然會很沒有頭緒,到後面你會覺得這個功能剛剛可以實現,又會覺得這個程式碼有漏洞,造成程式碼全是bug
(8):