2019年4月20日 星期六

java - 抽象

抽象父類別 (抽象類別介面)
1) 與一般class 一樣也具有 fild contors , mothed

2)關鍵 ->  抽象方法
a) 只 宣告名稱  沒有內容   [ X ]  <不放入內容
b) abstrat    => 語法 :   abstrat  類型    方法名 (引數) :

3.所有繼承的子類別,要求: (條件2選一)
a) 抽象子類別
b) 如不是抽象的子類別,則一定要將抽象方法  Ouerride => 又稱實作抽象方法內容

○轉成抽象類別  就不能使用 " new"
繼承子類別 要實做出來
變成抽象類 無法實體化  無法" new "

一. instanceof
=> 檢查使用的物件是 是否有此class 類型 is - a

二.java lang .Objet
=> 所有 class 的 共同父類

三.  final  <=  只能用 不能繼承
a. field => 常數
b.method = > 不允許  ouerride
c.class => 不允許繼承
d.空find 需搭配建構式


.interface 介面
1.本身 即為抽象的父類別
2.自己無法" new " 物件,只能提供繼承或 "多型應用中宣告陣列變數用

3.組成元素
a) 常數
=> public   final  static 類型    常數名   = 初始值;

b) 抽象方法
=> public  abstract   類型 方法名 (引數);

c) 預設方法 => 有 {  }  body的內容
=>public default  類型  方法名  (引數) {...............};

d)類別類方法
=> public    static  類型 方法名(引數){..................};

c跟d  java 8 才有

4.implements
=> 繼承 實作

介面 => 多個 Super (父類別)

列子一:

/* abstract  */ class school
// 改成抽象方法 要加上 abstract 關鍵字
{
private String name;
final double pi=3.14;


school(String name)
{
this.name=name;
}

String show()
{
//pi=5.6;  以是 常數 不能給值
return "名:"+name;
}

void setName(String name)
{
this.name = name;
}
void setChi(int chi){}


void setEng(int eng){}


void setMath(int math){}




/*
abstract void setChi(int chi);
// 改成抽象方法 要加上 abstract 關鍵字

abstract void setEng(int eng);


abstract void setMath(int math);

     */
}

class add
{
public static void  main (String args[])
{
school [][] s=
{
{
new A("a1",74),
new A("a2",74)

}
,
{
new B("b1",86),
new B("b2",86),
new B("b3",86)

}
,
{
new C("c1",98),
new C("c2",98),
new C("c3",98)

}

};

for(int i=0;i<s.length;i++)
{
for(int j=0;j<s[i].length;j++)
{
System.out.println("s["+i+"]["+j+"]="+s[i][j]+"\t"+s[i][j].show());

}
System.out.println();
}
System.out.println("=====================================");
s[2][0].setName("q1");
s[0][1].setChi(85);
s[1][0].setEng(85);
s[2][1].setMath(85);


for(int i=0;i<s.length;i++)
{
for(int j=0;j<s[i].length;j++)
{
System.out.println("s["+i+"]["+j+"]="+s[i][j]+"\t"+s[i][j].show());

}
System.out.println();

}
System.out.println("=====================================");

System.out.println(s[0][0] instanceof A);
System.out.println(s[0][0] instanceof school);
System.out.println(s[0][0] instanceof B);
System.out.println("=====================================");

System.out.println(s[0][0]);
System.out.println(s[1][0]);
System.out.println(s[0][0].equals(s[1][0]));
System.out.println(s[0][0].getClass());
//.getClass 查找在哪一個class


System.out.println("=====================================");

s[0][0]=s[1][0];
System.out.println(s[0][0]);
System.out.println(s[1][0]);
System.out.println(s[0][0].equals(s[1][0]));
//判斷兩邊是否相同 equals

}

}

class A extends school
{
private int chi;

A(String name,int chi)
{
super(name);
this.chi=chi;

}
String show()
{

return super.show()+"\t國文:"+chi;
}
   

void setChi(int chi)
{
this.chi = chi;
}

void setEng(int eng)
{

}

 
     void setMath(int  math)
{

}
}


class B extends school
{
private int eng;

B(String name,int eng)
{
super(name);
this.eng =eng;

}
String show()
{

return super.show()+"\t英文:"+eng;
}


     void setEng(int eng)
{
this.eng = eng;
}
}


class C extends school
{
private int math;

C(String name,int math)
{
super(name);
this.math = math;

}
String show()
{

return super.show()+"\t數學:"+ math;
}

     void setMath(int  math)
{
this.math =  math;
}
}


class test
{
public static void main(String args[])
{

//bookstore b1=new bookstore();
book b1 = new  bookstore() ;
//更常搭配此種方法

System.out.println(b1.title);
System.out.println(b1.bookprice(50));
System.out.println(b1.bookprice2(50));
//System.out.println(b1.bookprice3(50));
//System.out.println(bookstore.bookprice3(50));
System.out.println(book.bookprice3(50));
//用父介面去呼叫,子介面不能去呼叫它



member m1=new member("abc","def");

m1.name="eee";
m1.username="tttt";

//book x=new book(); //不能NEW 因為抽象類型

}

}

class  member
{
final String username;
String name;
//不給值 只能使用建構式] 只能二選一

member(String username,String name)
{
this.username=username;
this.name=name;
//username 是常數不能給值
}

}

interface book
{
//抽象類使用


  /*public final static */ String title="GJUN";//要給一個值
  /*public abstract*/ int bookprice(int price);//前面沒寫 也會自己補上


  // 第八版  才有此方法   前面要加上 default
   /*public */default double bookprice2(double price)
{

return price*10;
}



//public  可以省略  使用 static   就不要使用 default
//只要選擇一種就好
/*public*/ static double bookprice3(double price)
{

return price*15;
}
}

class bookstore implements /*使用implements繼承*/ book
//父類別也是抽象的
{
    int bookprice(int price)
{
return price*150;
}
     // 編譯不會過 因為  上面 int bookprice  第三種預設權限
// 但是 book 的 介面 都是 public 權限  所以無法訪問

}



例子二.
interface book2
{


}


class bookstore extends book3 implements book,book2
/*上面為 可繼承多個父類別,實體要先寫
class bookstore extends  只能一個不能使用多個
*/
{
public int bookprice(int price)
{
return price*150;
}

}


class book3
{


}

interface book
{
/*public final static */ String title="GJUN";
/*public abstract*/ int bookprice(int price);

/*public */default double bookprice2(double price)
{

return price*10;
}

/*public*/ static double bookprice3(double price)
{

return price*15;
}

}

例子:









java - 設計訂單系統

設計訂單系統

1.商品資訊
29元
35元
.需求
    a) 新增表單
    b) 查詢表單


MVC  (Model view Coutrol)

MVM (Model view Model) (類似Flash)


1.M => Data Buse = >class\
2.V=>  介面
3.C=>  操作
            找尋流程


M => 1.DateBase => Tuble => 欄位

id
name
ruler
pen
sum

name => string   其他格式INT
















======================================