// InterfaceMember.java interface Surfacing{ double pi=3.14159; double area(); } class Shap{ protected double x, y; Shap(double x, double y){ this.x = x; this.y = y; } public String toString(){ return "圖形原點:("+ x +", " + y + ")"; } } class Ccircle extends Shap implements Surfacing{ private double r; public Ccircle(double x, double y, double r){ super(x,y); this.r = r; } public double area(){ return pi * r * r; } public String toString(){ return "圓心:(" + x + "," + y + "),半徑:" + r + ",面積:" + area(); } } public class InterfaceMember{ public static void main(String args[]){ Ccircle c = new Ccircle(3, 5, 6); System.out.println(c.toString()); System.out.println("圓周率:" + c.pi); System.out.println("圓周率:" + Surfacing.pi); // interface不需建立實體也能執行 } }/*******************************************************************************************************/
姓名中翻英:http://c2e.ezbox.idv.tw/name.php
Kuan-Ju Huang
地址中翻英:http://www.post.gov.tw/post/internet/SearchZone/index.jsp?ID=130112
6F., No.89, Liuzhang St., Sanchong Dist., New Taipei City 241, Taiwan (R.O.C.)
VUE 辦帳號:http://www.vue.com/oracle/
Oracle 辦帳號:https://login.oracle.com/mysso/signon.jsp
/*******************************************************************************************************/
◎ 抽象類別:
是為了讓方法的使用更多樣化,若父類別是一個抽象類別,
其所產生的抽象方法都必須由子類別來加以實作,
如果沒有全部實作則子類別也要宣告成抽象類別。
◎ abstract class 類別名稱
抽象方法的宣告(不可以是 static)
abstract 傳回值 方法名稱(參數列);
存取權限不可以是 private,沒有抽象建構子和屬性。
◎如何實作抽象類別
利用 extends 關鍵字
子類別透過 override 父類別的方法
/*******************************************************************************************************/
◎ 介面
只能有無實作的方法
void show();
◎ 抽象類別
可以有一般的方法和抽象方法
abstract void show();
void show(){System.out.println();};
◎ 一般類別
只能有一般方法
void show(){System.out.println();};
/*******************************************************************************************************/
// LineShow.java abstract class LineDemo { //宣告抽象類別 private int length; LineDemo(int length){ this.length = length; } abstract double area(); //抽象方法 int getlength(){ //一般方法 return length; } } public class LineShow extends LineDemo{ LineShow(int length){ super(length); } public static void main(String args[]){ LineShow s = new LineShow(10); System.out.println("area= "+s.area()); //因為 length 是 private 權限,要印出必須是透過 getlength() System.out.println("length= "+s.getlength()); } public double area(){ //實作 LineDemo 的 area() return Math.pow(getlength(),2); //Math.pow(n,a) --> n^a } }
/*******************************************************************************************************/
◎ enum 列舉
列舉就十南一種類別,可以單獨撰寫,也可以在類別內當成內部類別。
存取權限 (static) enum 列舉名稱{
A, B, …..}
列舉的內容值都是經過 public, static, final 的修飾。
列舉的內容一經初始給定後就無法更改。
public enum week{…}
/*******************************************************************************************************/
※ 列舉練習,分兩個檔案來寫:Week.java、WeekEnum.java
// Week.java public enum Week{ Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
// WeekEnum.java public class WeekEnum{ public static void play(Week week){ switch(week){ case Sunday: System.out.println("Sunday"); break; case Monday: System.out.println("Monday"); break; case Tuesday: System.out.println("Tuesday"); break; case Wednesday: System.out.println("Wednesday"); break; case Thursday: System.out.println("Thursday"); break; case Friday: System.out.println("Friday"); break; case Saturday: System.out.println("Saturday"); break; default: System.out.println("DEFAULT"); } } public static void main(String args[]){ play(Week.Sunday); play(Week.Monday); } }
/*******************************************************************************************************/
◎取得列舉內容的方式
1.透過-運算子
2.透過-參照
Week week = new Week();
System.out.println(week.Sunday);
3.透過 value()
Week[] week2 = Week.values();
for(int i=0; i<week2.length;i++){
System.out.println(week2[i] + “, ”);
// 或
for(Week w:Week Values())
System.out.println(w + “, “);
4.利用 valueOf(String s)來取得
System.out.println(Week.valueOf(“Sunday”));
/*******************************************************************************************************/
// 實作練習 Enum 各取得內容方法 // 需要保留前一練習的檔案 Week.java // WeekEnum2.java public class WeekEnum2{ public static void main(String args[]){ System.out.println("=========方法2:透過參照========="); Week week = Week.Sunday; System.out.println(week.Sunday); System.out.println("=========方法3-1:透過 value() / for 迴圈========="); Week[] week2 = Week.values(); for(int i=0; i < week2.length; i++) System.out.println(week2[i] + ", "); System.out.println("=========方法3-2:透過 value() / for-each========="); for(Week w:Week.values()) System.out.println(w + ", "); System.out.println("=======方法4-1:利用 valueOf(String s)來取得======="); System.out.println(Week.valueOf("Sunday")); System.out.println("=======方法4-2:利用 valueOf(String s)來取得======="); System.out.println(Week.valueOf(Week.class, "Sunday")); } }
/*******************************************************************************************************/
◎內部類別 InnerClass
1. 一般內部類別
2. 方法內部類別
3. 匿名內部類別
4. 靜態內部類別
MyOuter$MyInner$MyInnerAgain.class
( $ 代表階層概念 )
class MyOuter{
class MyInner{ //內部類別
class MyinnerAgain{ foo(){} // 次內部類別
}
}
class MyInner2{ //內部類別
… }
class MyInner3{ //內部類別
… }
}
/*******************************************************************************************************/
// 練習 InnerClass:MyOuterDemo.java class MyOuter{ class MyInner{ public void foo(){ System.out.println("MyInner foo()"); } } } public class MyOuterDemo{ public static void main(String args[]){ System.out.println("===== 方法一 ====="); MyOuter t = new MyOuter(); MyOuter.MyInner t1 = t.new MyInner(); t1.foo(); System.out.println("===== 方法二 ====="); MyOuter.MyInner t3 = new MyOuter().new MyInner(); t3.foo(); System.out.println("===== 方法三 ====="); new MyOuter().new MyInner().foo(); } }
// InnerClassDemo.java class MyOuter{ private static int sx=9; private int x=7; class MyInner{ private int x=77; public void foo(){ int x=777; System.out.println("Local x =" + x); //無法印出 //System.out.println("MyInner x=" + MyInner().x); System.out.println("MyInner x =" + this.x); System.out.println("MyOuter x =" + MyOuter.this.x); //無法印出 //System.out.println("MyOuter x=" + MyOuter.x); System.out.println("MyOuter sx =" + sx); System.out.println("MyOuter sx =" + MyOuter.sx); } } } public class InnerClassDemo{ public static void main(String args[]){ new MyOuter().new MyInner().foo(); } }
// CboxDemo.java class Cbox{ private int height, width, length; private Color cr; Cbox(int height, int width, int length, String str){ this.height = height; this.width = width; this.length = length; cr = new Color(str); } void show(){ System.out.println("area = " + height*width*length); cr.show_color(); } class Color{ // innerclass private String color; Color(String color){ this.color=color; } void show_color(){ System.out.println("color = " + color); } } } public class CboxDemo{ public static void main(String args[]){ Cbox b = new Cbox(3,5,8,"blue"); Cbox c = new Cbox(8,9,13,"yellow"); System.out.println("Box b:"); b.show(); System.out.println("Box c:"); c.show(); } }
// ShowInner.java class businessCard{ private company c; private employee e; businessCard(String company, String name, String address, String mobile, String email){ c = new company(company); e = new employee(name, address, mobile, email); } void show(){ c.show_company(); e.show_employee(); } void separator(){ System.out.println("-------------------------------"); } class employee{ private String name, address, mobile, email; employee(String n, String a, String m, String e){ name=n; address=a; mobile=m; email=e; } void show_employee(){ System.out.println("| 姓名:"+name+" |"); separator(); System.out.println("| 地址:"+address+" |"); separator(); System.out.println("| 行動電話:"+mobile+" |"); separator(); System.out.println("| E-Mail:"+email+"|"); separator(); } } class company{ private String myCompany; company(String myCompany){ this.myCompany = myCompany; } void show_company(){ System.out.println(" "+myCompany + "公司員工資料"); separator(); } } } public class ShowInner{ public static void main(String arga[]){ new businessCard("STUST", "Aaron Huang", "Taipei,Taiwan","0987-005986", "kaneju0921@gmail.com").show(); } }
◎ 方法內部類別:在方法中宣告的類別。
// MethodInnerClass.java public class MethodInnerClass{ public static void main(String args[]){ new MethodInnerClass().see(); } void see(){ class MyInner{ void foo(){ System.out.println("MethodInnerClass foo"); } } MyInner mi = new MyInner(); // 注意必須建立 MyInner 實體 mi.foo(); // 才能印出 foo() } }
沒有留言:
張貼留言