有些內容使用中英雙語,有些只有英文或中文。歡迎使用與分享任何內容,但先來信告知並標示此部落格為出處。
Some parts use both Chinese and English, but some parts use only one language. Feel free to share, but please contact me first and list this blog as your reference.

2014年5月15日 星期四

JAVA - What's interface? (English version)

There's the Chinese version for this article. 這邊有中文版的文章

Originally, I was going to learn the Design Patterns,
but I suddenly realize that interface is so important.
Because I didn't use Java for a ling time, I just reviewed the interface for Design Patterns!

Long to short. The interface is for designing.
We can first design the method with name and return type, and without the detailed content.
And these methods must be implemented latter!

Sounds like abstract method?
Though they don't have the abstract key word, they are abstract!
(We can simultaneously pack many abstract stuffs.)

Many class can implement a same interface;
A class also can implement many interfaces at one time.
Because the Java doesn't have Multiple Inheritance, we can achieve similar effect by interfaces.

Let's look following examples. It's very simple, and may not show the brilliant part of interfaces.
But...at least it can run! Learning is from easy to difficult.


This is an simple interface.
I design a simple Sound Behavior, declaring a sound().

/**
* @interface SoundBehavior
*/
public interface SoundBehavior {
String sound();
}

Build Animal class, having constructor and toString().

public class Animal {
private String type;//the type of animal
public Animal(String inputType){
this.type = inputType;
}
public String toString(){
return type;
}
}
view raw Animal.java hosted with ❤ by GitHub

Cat' class, which inherited from Animal class.
Also implements interface(SoundBehavior)'s sound().

public class Cat extends Animal implements SoundBehavior{
private String name;
public Cat(String inputName){
super("Cat");//call the Animal class constructor, type is "Cat"
this.name = inputName;
}
public String sound(){
return "喵喵 Miaow";
}
public String toString(){
return name;
}
}
view raw Cat.java hosted with ❤ by GitHub

The main program~

public class interfaceTest {
/**
* @param interfaceTest
* @purpose test the interface
* Date: 05/14/2014 22:00 in Taiwan
* author: wing (http://autekroy.blogspot.com/)
*/
public static void main(String[] args) {
Cat myCat = new Cat("提摩");
Cat yourCat = new Cat("波比");
System.out.println("我的貓名字是" + myCat);
System.out.println(myCat + "的叫聲是" + myCat.sound());
System.out.println();
System.out.println("你的貓名字是" + yourCat);
System.out.println(yourCat + "的叫聲是" + yourCat.sound());
}
}

Running result

我的貓名字是提摩
提摩的叫聲是喵喵 Miaow
你的貓名字是波比
波比的叫聲是喵喵 Miaow
view raw result hosted with ❤ by GitHub

For more read: (Sorry, this slide is made in traditional Chinese)


If you want to use (copy, paste or quote) my original article, please contact me through email (autek.roy@gmail.com). If there is any mistake or comment, please let me know. :D

如要使用(複製貼上或轉載)作者原創文章,請來信跟我聯絡 (autek.roy@gmail.com)。如果有發現任何的錯誤與建議請留言或跟我連絡。 : )

沒有留言:

張貼留言

請留下您的任何想法或建議!
Please leave any thought or comment!