有些內容使用中英雙語,有些只有英文或中文。歡迎使用與分享任何內容,但先來信告知並標示此部落格為出處。
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().


Build Animal class, having constructor and toString().


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


The main program~


Running result


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!