您的位置:首页 > 编程语言 > Java开发

java-interface demo

2013-09-18 14:22 309 查看
1、主板demo

interface PCI

{

abstract void open();

abstract void close();

}

class MainBoard

{

void usePCI(PCI p)

{

p.open();

p.close();

}

}

class netCard implements PCI

{

public void open()

{

System.out.println("netCard
open!");

}

public void close()

{

System.out.println("netCard
close!");

}

}

class soundCard implements PCI

{

public void open()

{

System.out.println("soundCard
open!");

}

public void close()

{

System.out.println("soundCard
close!");

}

}

class MainBoardDemo

{

public static void main(String[] args)

{

final int x = 0;

class Test

{

void
get()

{

System.out.println("soundCard
close!" + x);

}

}

Test t = new Test();

t.get();

MainBoard mb = new
MainBoard();

mb.usePCI(new
netCard());

mb.usePCI(new
soundCard());

}

}

2、人demo

class InterfaceDemo

{

public static void main(String[] args)

{

Person p = new Student();

Student s = (Student) p;

s.say();

s.chifan();

s.shuijiao();

//System.out.println("Hello
World!");

}

}

class Person

{

void shuijiao()

{

System.out.println("睡觉");

};

}

class Student extends Person implements Smoke,Chifan

{

public void say()

{

System.out.println("Hello!");

}

public
void chifan()

{

System.out.println("chifan!");

}

}

interface Smoke

{

public abstract void say();

}

interface Chifan

{

void chifan();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: