新聞中心
求JAVA接口代碼
interface l1 { abstract void test(int i); } interface l2 { abstract void test(String s); } public class jiekou implements l1,l2 { public void test(int i) { System.out.println("接口1"); } public void test(String s) { System.out.println("接口2"); } public static void main(String args[]) { jiekou t=new jiekou(); t.test(42); t.test("Hello"); } } 下一個是內(nèi)部接口 public class groupsix { public interface student_info { public void out(); } public class student implements student_info { int count; String name; public student(String n1) { name=n1; count++; } public void out() { System.out.println(this.name+" count="+this.count); } } public groupsix(String name1[]) { student s1; int i=0; while(iname1.length) { s1=new student(name1[i]); s1.out(); i++; } } public static void main(String args[]) { String arr[]={"A","B","C"}; groupsix g6; new groupsix(arr); } }
專注于為中小企業(yè)提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)樂東黎族免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
Java 接口程序
你這個命題是典型的策略模式(設(shè)計模式中的一種)
不同的交通工具就是每一個裝在“錦囊”(接口)中的“錦囊妙計”(策略)
你可以搜索一下關(guān)鍵字“策略模式”
也可你看一下我這個故事:
劉備要到江東娶老婆了,走之前諸葛亮給趙云(伴郎)三個錦囊妙計,說是按天機拆開解決棘手問題,
嘿,還別說,真是解決了大問題,搞到最后是周瑜陪了夫人又折兵呀,那咱們先看看這個場景是什么樣子
的。
先說這個場景中的要素:三個妙計,一個錦囊,一個趙云,妙計是小亮同志給的,妙計是放置在錦囊
里,俗稱就是錦囊妙計嘛,那趙云就是一個干活的人,從錦囊中取出妙計,執(zhí)行,然后獲勝,用JAVA 程序
怎么表現(xiàn)這個呢?
計是同一類型的東東,那咱就寫個接口:
package com.cbf4life.strategy;
/**
* @author cbf4Life cbf4life@126.com
* I'm glad to share my knowledge with you all.
* 首先定一個策略接口,這是諸葛亮老人家給趙云的三個錦囊妙計的接口
*
*/
public interface IStrategy {
//每個錦囊妙計都是一個可執(zhí)行的算法
public void operate();
}
您的設(shè)計模式
第 5 頁
然后再寫三個實現(xiàn)類,有三個妙計嘛:
package com.cbf4life.strategy;
/**
* @author cbf4Life cbf4life@126.com
* I'm glad to share my knowledge with you all.
* 找喬國老幫忙,使孫權(quán)不能殺劉備
*/
public class BackDoor implements IStrategy {
public void operate() {
System.out.println("找喬國老幫忙,讓吳國太給孫權(quán)施加壓力");
}
}
package com.cbf4life.strategy;
/**
* @author cbf4Life cbf4life@126.com
* I'm glad to share my knowledge with you all.
* 求吳國太開個綠燈
*/
public class GivenGreenLight implements IStrategy {
public void operate() {
System.out.println("求吳國太開個綠燈,放行!");
}
}
package com.cbf4life.strategy;
/**
* @author cbf4Life cbf4life@126.com
* I'm glad to share my knowledge with you all.
* 孫夫人斷后,擋住追兵
*/
public class BlockEnemy implements IStrategy {
public void operate() {
您的設(shè)計模式
第 6 頁
System.out.println("孫夫人斷后,擋住追兵");
}
}
好了,大家看看,三個妙計是有了,那需要有個地方放這些妙計呀,放錦囊呀:
package com.cbf4life.strategy;
/**
* @author cbf4Life cbf4life@126.com
* I'm glad to share my knowledge with you all.
* 計謀有了,那還要有錦囊
*/
public class Context {
//構(gòu)造函數(shù),你要使用那個妙計
private IStrategy straegy;
public Context(IStrategy strategy){
this.straegy = strategy;
}
//使用計謀了,看我出招了
public void operate(){
this.straegy.operate();
}
}
然后就是趙云雄赳赳的揣著三個錦囊,拉著已步入老年行列的、還想著娶純情少女的、色迷迷的劉老
爺子去入贅了,嗨,還別說,小亮的三個妙計還真是不錯,瞅瞅:
package com.cbf4life.strategy;
/**
* @author cbf4Life cbf4life@126.com
* I'm glad to share my knowledge with you all.
*/
public class ZhaoYun {
/**
* 趙云出場了,他根據(jù)諸葛亮給他的交代,依次拆開妙計
*/
public static void main(String[] args) {
Context context;
您的設(shè)計模式
第 7 頁
//剛剛到吳國的時候拆第一個
System.out.println("-----------剛剛到吳國的時候拆第一個-------------");
context = new Context(new BackDoor()); //拿到妙計
context.operate(); //拆開執(zhí)行
System.out.println("\n\n\n\n\n\n\n\n");
//劉備樂不思蜀了,拆第二個了
System.out.println("-----------劉備樂不思蜀了,拆第二個了-------------");
context = new Context(new GivenGreenLight());
context.operate(); //執(zhí)行了第二個錦囊了
System.out.println("\n\n\n\n\n\n\n\n");
//孫權(quán)的小兵追了,咋辦?拆第三個
System.out.println("-----------孫權(quán)的小兵追了,咋辦?拆第三個
-------------");
context = new Context(new BlockEnemy());
context.operate(); //孫夫人退兵
System.out.println("\n\n\n\n\n\n\n\n");
/*
*問題來了:趙云實際不知道是那個策略呀,他只知道拆第一個錦囊,
*而不知道是BackDoor這個妙計,咋辦? 似乎這個策略模式已經(jīng)把計謀名稱寫出來了
*
* 錯!BackDoor、GivenGreenLight、BlockEnemy只是一個代碼,你寫成first、second、
third,沒人會說你錯!
*
* 策略模式的好處就是:體現(xiàn)了高內(nèi)聚低耦合的特性呀,缺點嘛,這個那個,我回去再查查
*/
}
}
就這三招,搞的周郎是“陪了夫人又折兵”呀!這就是策略模式,高內(nèi)聚低耦合的特點也表現(xiàn)出來了,
還有一個就是擴展性,也就是OCP 原則,策略類可以繼續(xù)增加下去,只要修改Context.java 就可以了,這
個不多說了,自己領(lǐng)會吧。
以上摘自網(wǎng)絡(luò),詳情參考 設(shè)計模式.pdf
里面還有類圖
JAVA 接口與多態(tài) 求符合下列要求的代碼
多態(tài)是面向?qū)ο缶幊痰奶卣髦唬涌谑且幌盗蟹椒ǖ穆暶鳎且恍┓椒ㄌ卣鞯募希刑囟ǖ恼Z法和結(jié)構(gòu),這兩者根本不是同一類型和層次上的概念。接口毫無疑問可以體現(xiàn)出多態(tài)性來,但是多態(tài)性未必一定要用接口,只要存在方法的重寫、重載與動態(tài)連接即可體現(xiàn)多態(tài)性(如存在繼承關(guān)系的類之間),所以,不存在“什么時候用接口什么時候用多態(tài)”的問題,程序里寫的具體代碼只可能是接口,只是這代碼可能會體現(xiàn)出多態(tài)性而已,兩者可以在不同的概念層次上并存,不存在沖突。
簡而言之,你可以在程序里用代碼定義一個接口,但是你不能定義一個多態(tài),多態(tài)只是對你代碼特征的一種描述,一種概念上的抽象和總結(jié)。
自定義接口的JAVA代碼
三個錯:
1.Chicken 類里的howtoeat方法改howtoEat;
2.Apple類和Orange 放到Fruit 類外面。
3。Chicken 類的構(gòu)造方法需要給重量參數(shù),因為你只定義了一個構(gòu)造方法。
我調(diào)試的沒問題,改后代碼:
interface Edible{
public String howtoEat();
}
class Animal{
}
class Chicken extends Animal implements Edible,Comparable{
int weight;
public Chicken(int weight){
this.weight=weight;
}
public String howtoEat(){
return "fry it";
}
public int compareTo(Object o){
return weight-((Chicken)o).weight;
}
}
class Tiger extends Animal{
}
abstract class Fruit implements Edible
{}
class Apple extends Fruit {
public String howtoEat(){
return "Make apple cider";
}
class Orange extends Fruit{
public String howtoEat(){
return "Make orange juice";
}
}
}
public class Test{
public static void main(String[] args){
Object[] objects={new Tiger(),new Chicken(10),new Apple()};
for(int i=0;iobjects.length;i++){
showObject(objects[i]);
}
}
public static void showObject(Object object){
if(object instanceof Edible)
System.out.println(((Edible)object).howtoEat());
}
}
求高手幫我看段Java關(guān)于接口的一段代碼
1.implements PCI是實現(xiàn)PCI 接口的意思;
2.不是;這兩個方法是必須寫的,不能少;這兩個方法來自接口中的,既然現(xiàn)實了PCI接口,就一定要實現(xiàn)接口中的所有方法
3.PCI nc= new NetworkCard(); 因為NetworkCard實現(xiàn)了PCI接口,PCI就類似是NetworkCard的父類,這個體現(xiàn)了面相對象編程中的多態(tài);就好比你是一個男生,我可以說你是一個人吧,意思是一樣的;
4.PCI nc = new PCI()這個是不能直接這樣寫的,因為PCI是接口,接口中沒有構(gòu)造方法,這個是new不出來對象的
本文名稱:接口java程序代碼 Java接口代碼
URL分享:http://www.ef60e0e.cn/article/dooipcd.html