●生命週期;reference
1.field=>變數
a)local/global=>this
b)物件類=>"new"物件存取=>每個物件都有自己的屬性
c)類別類=>static=>類別直接存取
=>不須"new"物件存取
=>只產生一次,所有物件共同使用的屬性(絕對位址)
*參考程式碼java/11/add1.java
 
2.method=>方法
a)傳值/不傳值(可當值來用)
b)物件類=>需透過"new"物件存取
c)類別類=>static
=>不須透過"new"物件存取
=>類別(class)直接存取
*參考程式碼java/11/add1.java
 
3.內部類別inner class(nested class)
=>一定要全部的class寫在同一個*.java檔案,不能分開寫,但編譯的時候每一個class會自動編譯成獨立的*.class檔
=>主要目的是為了好管理,一個檔案可以管理好幾個class
=>先確定外部類別outer class
=>不是繼承(是獨立的),所以不能結合多型
add2.java
class student2{
class A{
}
}
class add2{
public static void main(String args[]){
}
}
=>A是S2的內部類別,S2是A的外部類別,將add2.java編譯後,會產生三個*.class檔,其中student2$A.class就代表,A是student2的內部class
 
a)物件類內部類別
=>先"new"外部類別物件再"new"內部類別物件
add2.java
class student2{
String name="student2 name";
class A{
     String name="student2_A name";
}
}
class add2{
public static void main(String args[]){
student2 s=new student2();
 //pirnt  student2 name,因為這邊的reference是student2
System.out.println(s.name);
}
}
 
**說明
1)class A也是student2的屬性,和String name同位階,所以在uml圖中也是寫在field區,寫法為A:class,所以存取方式和規則會和String name相同
2)如果要抓的A裡面的name,要先確認外部是否要new(是否為static),再確認內部是否需要new,而java的路徑是用"."來表示,所以寫成 student2.A;
3)如果沒加static,有多少層就要new多少層,因為要先產生student才能找到A,再由A找到自己的內容
 
**錯誤的做法
class add2{
public static void main(String args[]){
student2 s=new student2();
System.out.println(s.name);
student2.A s2=new student2().A;
}
}
=>如果寫成 new student2().A;,會出現錯誤,這邊的做法是把A當作變數了,因為class一定要產生物件
 
**正確的作法:要連續new 2次,也就是左邊有幾層,右邊就要new幾次
class add2{
public static void main(String args[]){
student2 s=new student2();
System.out.println(s.name);
student2.A s2=new student2().new A();
System.out.println(s2);
}
}
=>這裡可以看出透過new了兩層以後,把stack配給A了
 
**所以這時再做System.out.println(s2.name);
=>這邊s2.name的reference就變成student2中的 A class,所以output會變成 student2_A name
 
**多層的做法左邊有幾層,右邊就要new幾次 )
class student2{
String name="student2 name";
class A{
     String name="student2_A name";
     class A_1{
         String name="student2_A_1 name";
     }
}
class B{
String name="student2_B name";
}
}
class add2{
public static void main(String args[]){
student2 s=new student2();
System.out.println(s.name);
student2.A s2=new student2().new A();
System.out.println(s2.name);
student2.B s3=new student2().new B();
System.out.println(s3.name);
student2.A.A_1 s4=new student2().new A().new A_1();
System.out.println(s4.name);
}
}
 
**方法的呼叫
(一)先區別一次產生一個物件,和一次產生兩個物件的做法
 
class add2{
public static void main(String args[]){
//只產生一個物件
student2 s =new student2();
s.show();
s.show2()
//產生兩個物件
new student2().show();
new student2().show2();
}
用記憶體來看,前兩個的記憶體位址相同,後面兩個記憶體位址不同
 
(二)要使用A_1中的方法
class student2{
String name="student2 name";
void show(){
     System.out.println("hello student2--1");
}
void show2(){
     System.out.println("hello student2--2");
}
 
class A{
String name="student2_A name";
class A_1{
String name="student2_A_1 name";
void show(){
     System.out.println("hello student2.A.A_1--1");
}
void show2(){
     System.out.println("hello student2.A.A_1--2");
}
     }
}
class B{
     String name="student2_B name";
}
}
 
class add2{
public static void main(String args[]){
student2.A.A_1 s4=new student2().new A().new A_1();
s4.show();
s4.show2();
new student2().new A().new A_1().show();
}
}
=>如果兩個方法都要使用,就new了以後指定給s4,但如果只需要用其中一個功能,那就不需要指定給s4,可以new完直接呼叫
 
b)類別類內部類別=>外部類別物件不用"new"
(一)使用內部static class的非static method
**如何判斷是否要new:class A有static,代表要跟它的外部class(student3)說,sudent3要new,所以class A是否要new是由它裡面的show2()來決定
class student3{
static String name="student3";
static void show(){
     System.out.println("hello student3--1");
}
void show2(){
     System.out.println("hello student3--2");
}
static class A{
void show2(){     //沒有static所以A要new
     System.out.println("hello student3.A--2");
}
}
}
**錯誤寫法:
執行時,如果寫成student3.A s=studnet3().new A();會有錯誤,因為刪除是要刪student3右邊的()而非左邊的new(student3()就變成一個建構式了,要刪除建構式要先刪除括號)
 
**正確寫法:
class add3{
public static void main(String args[]){
     student3.A s=new student3.A();   
     s.show2();
}
}
=>這樣才是new student3中的A物件
 
**注意:如果只能到單獨的內部*.class檔是不能用的,一定要連外部的*.class都拿到才能用
 
 
 
 
(二)使用內部static class的static method
class student3{
static String name="student3";
static void show(){
     System.out.println("hello student3--1");
}
void show2(){
     System.out.println("hello student3--2");
}
static class A{
void show2(){
     System.out.println("hello student3.A--2");
}
static void show(){
     System.out.println("hello student3.A--1");
}
}
}
class add3{
public static void main(String args[]){
     student3.A.show();
}
}
=>可以直接使用,因為static void show()告訴A不需要再new
 
(三) 使用內部非static class的static method
class student3{
static String name="student3";
static void show(){
     System.out.println("hello student3--1");
}
void show2(){
     System.out.println("hello student3--2");
}
static class A{
void show2(){
     System.out.println("hello student3.A--2");
}
static void show(){
     System.out.println("hello student3.A--1");
}
}
class B{
String name="student2_B name";
static void show(){
     System.out.println("hello student3.A--1");
}
}
}
=>出現error,因為只要class沒有加static,裡面的method就不能加static
 
**另外,student3前面也不能加上static,因為static是告訴他的外部class是否需要new,但student3已經是最外層沒有更外部了
 
c)匿名類別(沒有名字的子類別)
=>繼承"interface"類別
=>沒有"class"名稱
=>借用"介面"名字來"new"物件
=>主要定義(宣告)在"main"程式裡
 
(一)先複習interface
add4.java
interface student4{
double pi=3.14;
void show();
}
class sa implements student4{
public void show(){
     System.out.println("hello sa");
}
}
class add4{
public static void main(String args[]){
sa s =new sa();
s.show();
}
}
 
**說明
1.interface只能放常數和抽象方法
2.繼承的子類別要注意權限,並且一定要override抽象方法
3.一定要透過子類別來new interface
 
(二)匿名類別的做法,就是把原本拉出來獨立的子類別class sa放到main中藏起來,實際真正繼承interface是在main 裡面繼承
add4.java
interface student4{
     double pi=3.14;
     void show();
}
/*class sa implements student4{
public void show(){
System.out.println("hello sa");
}
}*/
class add4{
public static void main(String args[]){
student4 s =new student4(){
     public void show(){
         System.out.println("hello sa");
     }
};
     s.show();
}
}
**
1.new student4實際上是在new student4()後面{}中的內容,這就是一個沒有名字的子類別,這個子類別等於就是把class sa中的內容拉進來
2.此時s是指匿名class而非interface student4
2.所以裡面的規則其實和class sa一樣,假設沒有寫內容只有 new student4(){ },出現錯誤訊息表示子類別沒有做override
3.如果寫了內容但沒有加public,會出現錯誤訊息表示子類別的權限太弱
4.編譯後會產生一個檔案如下,是一個沒有名稱放在add4下面的類別,不能單獨使用
 
(三)匿名子類別的簡潔寫法
add4.java
class add4{
public static void main(String args[]){
new student4(){
public void show(){
     System.out.println("hello sa");
}
public void show2(){
     System.out.println("hello2 sa");
}
}.show();
}
}
 
(四)java8以後不限定一定要interface才能用匿名子類別繼承,一般class也能用匿名class來繼承,且同時會把父類別的其他東西也一起繼承下來,不過主要應用還是用在interface上(因為遇到static時會有問題要處理,目前不提)
=>如果在add2.java的student2中加入void abc(){System.out.println("一般class匿名類別測試");} 
 
add4.java
new student2(){
public void show(){
System.out.println("hello sa");
}
public void show2(){
System.out.println("hello2 sa");
}
}.abc();
 
**說明
1.outputs為 一般class匿名類別測試
2.show()和show2()都做override
3.abc()是繼承了student2
 
 
d)enum=>列與值(列舉)=>具public static final=>沒有名字的陣列
**values()=>顯示 enum 的記憶體位址
/*
class bookName {
public static final String book1="JAVA";
public static final String book2="android";
public static final String book3="WEB";
}
*/
enum bookName{
     Java,Android,WEB
};
class add5{
     public static void main(String args[]){
     }
}
**說明
1.enum是一個清單的概念,裡面只能放常數(全部都是值)
2.當使用class打很多值的資料如class bookName,很浪費時間,可以用enum來做
3.enum bookName的 bookName指的是{}的reference,裡面有一個沒有名字的陣列,就是{}中的內容,所以每一個值背後都有編號[0][1][2]....
4.都是以存String值為主,數字類在enum中無法儲存(裡面內容規則和變數名稱的命名規則一樣)
5.所以不用加String,他本身內容具有三個特性public static final
6.但是卻不能定義一個 String 變數放進去,因為它的reference(or類型)是bookName而非String
=>enum背後代表的是一個沒有名字但是類型為bookName的陣列:bookName[]   ={JAVA,android,WEB};
7.System.out.println(bookName.values());去查enum的記憶體位置會顯示如下:
=>[:表示是一個陣列,reference為bookName
 
要把bookName中的內容變成String的作法
enum bookName{
     Java,Android,WEB
};
class add5{
public static void main(String args[]){
System.out.println(bookName.values());
bookName[] x=bookName.values();
for(int i=0;i<x.length;i++){
     System.out.println(x[i]);
}
}
}
**說明:
1.寫一個類型也是bookName的陣列x,並且把bookName.values()指給它,但這邊不是傳址,實際上是做copy的動作,把值copy給陣列x
2.因為enum中的值預設是final的,如果是傳址,則當x[0]要做改變會有問題,因為final不能改,所以不是在傳址
 
eumu的用法
class book{
enum bookName{
Java,Android,WEB
}
}
 
class add5{
public static void main(String args[]){
book.bookName[] x=book.bookName.values();
for(int i=0;i<x.length;i++){
     System.out.println(x[i]);
}
}
}
**說明:
1.eunm可以當作內部類別,因為本身是static可以直接使用,通常用來存放專有名稱
2.作法,設定一個book的bookName類型的陣列x然後把 book.bookName 位址內的資料傳給他
3.去查x和bookName的位址,確實不一樣
System.out.println(bookName.values());
System.out.println(x);
 
●MVC
modeling=>模組化(寫class/method)
view=>ui介面;操作畫面
controller=>控制流程
 
●netbeans實作
=>做一個輸入畫面,要輸入:姓名、國文、英文,一個確定按鈕
主控台輸出:(這邊用println來做)
你的名字:
國文分數:
英文分數:
總分:
 
1.container:panel(是一個分組的概念)/label/text field/button
2.先把使用者介面做好
3.命名變數名稱:name/chi/eng/ok
4.設定ok button的滑鼠事件=>mouseclicked
5.java.lang中的包覆類別:字串轉intInterger.parseInt(String);
=>輸入程式碼如下:
 System.out.println("名:"+name.getText()+
  "\n國文:"+chi.getText()+
  "\n英文:"+eng.getText()+
"\n總分:" +(Integer.parseInt(chi.getText())+Integer.parseInt(eng.getText()))
  );
arrow
arrow
    創作者介紹
    創作者 muchone 的頭像
    muchone

    簡單。生活。享受

    muchone 發表在 痞客邦 留言(0) 人氣()