1.繼承匿名類別
/*匿名類別(Anonymous classes)是區域內部類
別的一種,顧名思義匿名類別沒有名稱,所
以也不能定義建構子。
匿名類別的目的只是要建立一個物件,物
件建立之後就不會再使用該類別。
匿名類別常當作覆蓋或實作抽象方法之用。*/
/*匿名類別的功能就是只建立一個物件,所以類別
主體的定義和建立物件是一氣呵成。
繼承類別建立匿名類別:
實作介面建立匿名類別:
new 父類別(參數列) {
// 類別主體
}
new 介面() {
// 類別主體
}*/
class EX8_25 {
public static void main(String [] args) {
Sup s = new Sup() { // anonymous class declaration
{ System.out.println(this); }
void m() {//覆蓋方法
System.out.println("覆蓋過的m方法");
}
void n() { //匿名類別自訂的方法
System.out.println("自訂的n方法");
}
};
s.m();
// s.n(); //錯誤
}
}
class Sup {
void m() { System.out.println("Sup實體的m方法"); }
//void n() { System.out.println("Sup實體的n方法"); }
}
2.第九章介面、套件與修飾字
/*介面多重繼承的語法:
interface 子介面extends 父介面一, 父介面二, ... {
//新增的靜態常數及抽象方法
}
類別實作多個介面的語法:
class 類別名稱implements 介面一, 介面二, ... {
//類別主體敘述
}
介面的多重繼承*/
class EX9_1 {
public static void main(String [] args) {
MyDog boy = new MyDog("小仔");
System.out.println("我的愛犬叫:" + boy.name );
System.out.println(boy.name+"的叫聲為:" + boy.getSound() );
System.out.println(boy.name+"跑的速度為:" + boy.getSpeed() );
System.out.println(boy.name+"跑的速度為:" + boy.slow );
System.out.println(boy.name+"跑的速度為:" + boy.midspeed );
}
}
class Animal {
public String name;
Animal(String name) { this.name=name; }
}
interface Pet {
int slow = 4; //抽象屬性宣告 static, final, public
int midspeed = 12;
int fast = 20;
String getSound(); //抽象方法宣告 abstract, public
int getSpeed();
}
class MyDog extends Animal implements Pet {
MyDog(String name) { super(name); }
public String getSound() { return "旺旺"; }
public int getSpeed() { return Pet.fast; }
}
3.
class EX9_3 {
public static void main(String [] args) {
Singable[] a = {new MyCat(), new MyBird(), null};
a[2] = new Singable(){
public void sing() { System.out.println("叮咚!叮咚!"); }
};
System.out.println("3個會叫的東西,猜猜看各是什麼?");
for(int i=0; i
}
interface Singable { void sing(); }
class MyCat implements Singable {
public void sing() { System.out.println("喵~喵~喵~"); }
}
class MyBird implements Singable {
public void sing() { System.out.println("啾!啾!啾!"); }
}
4.
class EX9_2 extends ClassC {
public static void main(String [] args) {
EX9_2 a=new EX9_2();
ClassB b = new ClassB();
ClassC c = new ClassC();
System.out.println("a為InterfaceA型別: " + (a instanceof InterfaceA) );
System.out.println("b為InterfaceA型別: " + (b instanceof InterfaceA) );
System.out.println("c為InterfaceA型別: " + (c instanceof InterfaceA) );
}
}
interface InterfaceA {
void m(); //宣告抽象方法m()
}
class ClassB {
public void m() {} //定義m()方法
}
class ClassC implements InterfaceA {
public void m() {} //實作m()方法
}
5.
/*
IA IB IC ID
↑↑ ↑
IE
IA IB IC ID
↑ ↑ ↑ ↑
IF
*/
class EX9_4 {
public static void main(String[] args) {
IE e = new IE(){};
IF f = new IF(){};
System.out.println("e為IA型別: " + (e instanceof IA) );//T
System.out.println("e為IB型別: " + (e instanceof IB) );//T
System.out.println("e為IC型別: " + (e instanceof IC) );//F
System.out.println("e為ID型別: " + (e instanceof ID) );//T
System.out.println("f為IA型別: " + (f instanceof IA) );//T
System.out.println("f為IB型別: " + (f instanceof IB) );//T
System.out.println("f為IC型別: " + (f instanceof IC) );//T
System.out.println("f為ID型別: " + (f instanceof ID) );//T
System.out.println("e.i=" + e.i);
//System.out.println("f.i=" + f.i); //錯誤
System.out.println("f.k=" + f.k);
}
}
interface IA {}
interface IB { int i=1; }
interface IC { int i=2; }
interface ID { int k=3; }
interface IE extends IA, IB, ID {}
interface IF extends IA, IB, IC, ID {}
6.QA5.
interface I {
void setX(int X);
int getX();
}
//A
interface A extends I {
void m();//因繼承不一定要實作
}
//B
//interface B implements I {
class B implements I {
public void setX(int x){}
public int getX(){return 1;}
}
//C
abstract class C implements I{
public void setX(int x){}
}
//D
class D implements I {
public void setX(int x){}
public int getX(){return 1;}
}
//E
// class E implements I{
abstract class E implements I{
public int getX(){return 1;}
}
7.
class EX9_5 {
public static void main(String[] args) {
IIA f = (IIA)new CCF();
System.out.println("f屬於IIA型別:"+ (f instanceof IIA));
System.out.println("f屬於IIB型別:"+ (f instanceof IIB));
System.out.println("f屬於IIC型別:"+ (f instanceof IIC));
System.out.println("f屬於IID型別:"+ (f instanceof IID));
System.out.println("f屬於CCE型別:"+ (f instanceof CCE));
System.out.println("f屬於CCF型別:"+ (f instanceof CCF));
}
}
interface IIA {}
interface IIB {}
interface IIC extends IIA, IIB {}
interface IID {}
class CCE {}
class CCF extends CCE implements IIC, IID {}
8.
interface IA{}
class CB implements IA {}
class CC implements IA {}
class QA9_4 extends CC {
public static void main (String[] args){
IA a =new QA9_4();
CB b =new CB();
CC c =new CC();
c=(QA9_4)a;
a=b;
}
}
9.
interface A {}
interface B extends A {}
class C {}
class D implements B {}
class E extends C {
D d;
E(){d= new D();
}
}
class QA9_6{
public static void main(String arg[]){
E e=new E();
D d=new D();
System.out.println("e屬於C型別:"+ (e instanceof C));
System.out.println("e屬於A型別:"+ (e instanceof A));
System.out.println("d屬於D型別:"+ (d instanceof D));
System.out.println("d屬於B型別:"+ (d instanceof B));
System.out.println("d屬於A型別:"+ (d instanceof A));
}
}
10.
class EX9_8a {
public static void main(String[] args) {
java.util.Date d = new java.util.Date();
System.out.println( d );
}
}
11.
import java.util.*;
class EX9_8 {
public static void main(String[] args) {
Date d = new Date();
System.out.println( d );
System.out.println( d.getDate() );
System.out.println( d.getDay() );
System.out.println( d.getHours() );
System.out.println( d.getMinutes() );
System.out.println( d.getMonth() );
System.out.println( d.getDay() );
System.out.println( d.getSeconds() );
System.out.println( d.getTime() );
System.out.println( d.getYear() );
}
}
留言列表