標題:
介面 (一)
[打印本頁]
作者:
tonyh
時間:
2021-7-16 19:39
標題:
介面 (一)
本帖最後由 tonyh 於 2021-7-16 20:36 編輯
介面(Interface)是一種特殊的類別,在宣告時不是使用class而是改用interface,但在編譯後也會產生.class檔。介面只有宣告而沒有實作,架構上與抽象類別有點類似,用法與限制卻大不相同。
在Java中,類別並不允許多重繼承,但允許實作多個介面,而在介面繼承時,也允許繼承多個父介面,我們利用這兩種機制來模擬多重繼承。
我們可以把介面想像成物件與外界溝通的橋樑,或物件欲扮演的角色。通常我們使用介面來描述不同類別的物件間,共通的行為或特性。譬如:有個介面叫父親,把這個介面套用在小貓、小鳥、還是人類身上,就能使該物件開始扮演父親的角色,執行照顧子女的任務。
關於介面的幾個重點歸納如下:
1. 介面中的成員變數必須利用關鍵字final宣告為常數,並指定初始值。
2. 介面中的成員方法必須宣告為抽象方法,不能有方法主體。
3. 由於編譯器會自動將介面中的成員變數宣告為public、static與final,因此是常數變數,同時關鍵字可以省略。
4. 由於編譯器會自動將介面中的成員方法宣告為public與abstract,因此是抽象方法,同時關鍵字可以省略。
5. 類別可繼承一個類別但可實作多個介面。
6. 介面不能實作另一個介面但可繼承多個介面。
7. 實作介面的類別可以新增成員變數與成員方法,但一定要定義介面中所有抽象方法的內容,同時因為介面中的成員方法存取權限都是public,所以在這邊定義成員方法時,也必須加上public,否則會編譯錯誤。
[設計導引]
1. 定義抽象類別 Animal, 該類別包含了
兩個屬性成員: age 與 w
建構子
兩個抽象方法: eat(double x) 與 showProfile()
2. 定義一般類別 Human 繼承 Animal, 在該類別中新增
一個屬性成員: name
建構子
實作自父類別繼承下來的兩個抽象方法: eat(double x) 與 showProfile()
3. 定義三個介面: Swimmer, Singer, Father
分別包含了常數變數 LEVEL="專業級" 或 "新手級" 等
以及 swim(double x), sing(String song), takeCare() 等抽象方法
4. 讓類別 Human 實作 Swimmer, Singer, Father 這三個介面
並實作所有抽象方法: swim(double x), sing(String song), takeCare()
5. 在 Human 類別下新增一物件, 並利用該物件帶出所有方法, 如執行畫面所示
public class Ch72 {
public static void main(String[] args) {
Human h1=new Human("湯尼",35,70);
h1.showProfile();
h1.eat(0.85);
h1.showProfile();
h1.swim(1500);
h1.sing("新不了情");
h1.takeCare();
}
}
abstract class Animal
{
int age;
double w;
Animal(int a,double w)
{
age=a;
this.w=w;
}
abstract void eat(double x);
abstract void showProfile();
}
interface Swimmer
{
String LEVEL="專業級";
void swim(double x);
}
interface Singer
{
String LEVEL="專業級";
void sing(String x);
}
interface Father
{
String LEVEL="新手級";
void takeCare();
}
class Human extends Animal implements Swimmer, Singer, Father
{
String name;
Human(String n,int a,double w)
{
super(a,w);
name=n;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
void eat(double x)
{
System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
w+=x;
}
public void swim(double x)
{
System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
}
public void sing(String x)
{
System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
}
public void takeCare()
{
System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
}
}
複製代碼
作者:
夏子涵
時間:
2021-7-16 20:44
public class Ch03 {
public static void main(String[] args) {
Human h=new Human(35, 70, "湯尼");
h.showProfile();
h.eat(0.85);
h.showProfile();
h.swim(1500);
h.sing("快樂頌");
h.takeCare();
}
}
abstract class Animal
{
int age;
double w;
Animal(int a, double w)
{
age=a;
this.w=w;
}
abstract void eat(double x);
abstract void showProfile();
}
class Human extends Animal implements Swimmer, Singer, Father
{
String name;
Human(int a, double w, String n){
super(a, w);
name=n;
}
@Override
void eat(double x) {
// TODO Auto-generated method stub
System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
}
@Override
void showProfile() {
// TODO Auto-generated method stub
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
@Override
public void takeCare() {
// TODO Auto-generated method stub
System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
}
@Override
public void sing(String song) {
// TODO Auto-generated method stub
System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+song+".");
}
@Override
public void swim(double x) {
// TODO Auto-generated method stub
System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
}
}
interface Swimmer
{
String LEVEL="專業級";
void swim(double x);
}
interface Singer
{
String LEVEL="專業級";
void sing(String song);
}
interface Father
{
String LEVEL="新手級";
void takeCare();
}
複製代碼
作者:
黃宥華
時間:
2021-7-16 20:46
public class CH04 {
public static void main(String[] args) {
Human h=new Human(35,70,"湯湯");
h.show();
h.eat(0.85);
h.show();
h.swim(1500);
h.sing("哈哈");
h.care();
}
}
abstract class Animal
{
int age;
double w;
Animal(int a,double w){
age=a;
this.w=w;;
}
abstract void eat(double x);
abstract void show();
}
class Human extends Animal implements Swimmer,Singer,Father{
String name;
Human(int a, double w,String n) {
super(a, w);
name=n;
}
@Override
void eat(double x) {
System.out.println(name+"咕咕咕吃了"+x+"公斤的食物");
w+=x;
}
@Override
void show() {
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤");
}
@Override
public void care() {
System.out.println(name+"以"+Father.LEVEL+"的水準照顧小孩");
}
@Override
public void sing(String s) {
System.out.println(name+"以"+Singer.LEVEL+"的水準唱了"+s);
}
@Override
public void swim(double x) {
System.out.println(name+"以"+Swimmer.LEVEL+"的水準游了"+x+"公尺");
}
}
interface Swimmer{
String LEVEL="權威級";
void swim(double x);
}
interface Singer{
String LEVEL="權威級";
void sing(String s);
}
interface Father{
String LEVEL="入門級";
void care();
}
複製代碼
作者:
劉愷鈞
時間:
2021-7-16 20:47
本帖最後由 劉愷鈞 於 2021-7-23 19:02 編輯
public class JPA01 {
public static void main(String args[])
{
Human h=new Human(35, 70, "湯尼");
h.showProfile();
h.eat(0.85);
h.showProfile();
h.swim(1500);
h.sing("新不了情");
h.takeCare();
}
}
abstract class Animal
{
int age;
double weight;
Animal(int a, int w)
{
age=a;
weight=w;
}
abstract void eat(double x);
abstract void showProfile();
}
class Human extends Animal implements Swimmer,Singer,Father
{
String name;
Human(int a, int w,String n) {
super(a, w);
name=n;
}
void eat(double x) {
System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物");
weight+=x;
}
void showProfile() {
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
}
public void swim(double x)
{
System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
}
public void sing(String song) {
System.out.println(name+"以"+Singer.LEVEL+"水準,唱下了一首"+song+".");
}
public void takeCare()
{
System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
}
}
interface Swimmer
{
String LEVEL="專業級";
void swim(double x);
}
interface Singer
{
String LEVEL="專業級";
void sing(String song);
}
interface Father
{
String LEVEL="新手級";
void takeCare();
}
複製代碼
作者:
李宇澤
時間:
2021-7-16 20:53
public class Ch65 {
public static void main(String[] args) {
Human h1=new Human("湯尼",26,64);
h1.showProfile();
h1.eat(0.99);
h1.showProfile();
h1.swim(1000);
h1.sing("忘情水");
h1.takeCare();
}
}
abstract class Animal
{
int age;
double w;
Animal(int a,double w)
{
age=a;
this.w=w;
}
abstract void eat(double x);
abstract void showProfile();
}
interface Swimmer
{
String LEVEL="專業級";
void swim(double x);
}
interface Singer
{
String LEVEL="專業級";
void sing(String x);
}
interface Father
{
String LEVEL="新手級";
void takeCare();
}
class Human extends Animal implements Swimmer, Singer, Father
{
String name;
Human(String n,int a,double w)
{
super(a,w);
name=n;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
void eat(double x)
{
System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
w+=x;
}
public void swim(double x)
{
System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
}
public void sing(String x)
{
System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
}
public void takeCare()
{
System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
}
}
複製代碼
作者:
沈子晏
時間:
2021-7-16 20:53
public class Ch0716 {
public static void main(String[] args) {
Human h=new Human(35, 70, "湯尼");
h.showProfile();
h.eat(0.85);
h.showProfile();
h.swim(1500);
h.sing("快樂頌");
h.takeCare();
}
}
abstract class Animal
{
int age;
double w;
Animal(int a, double w) {
age=a;
this.w=w;
}
abstract void eat(double x);
abstract void showProfile();
}
class Human extends Animal implements Swimmer, Singer, Father
{
String name;
Human(int a, double w, String n) {
super(a, w);
name=n;
}
void eat(double x) {
System.out.println(name+"咕嚕咕嚕吃不下了"+x+"公斤。");
w+=x;
}
void showProfile() {
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤。");
}
public void swim(double x)
{
System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
}
public void sing(String x)
{
System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
}
public void takeCare()
{
System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
}
}
interface Swimmer
{
String LEVEL="專業級";
void swim(double x);
}
interface Singer
{
String LEVEL="專業級";
void sing(double song);
}
interface Father
{
String LEVEL="專業級";
void takeCare();
}
複製代碼
作者:
孫嘉駿
時間:
2021-7-16 20:54
public class Ch05 {
public static void main(String[] args) {
Human h=new Human(35, 70, "湯尼");
h.showProfile();
h.eat(0.85);
h.showProfile();
h.swim(1500);
h.sing("Afterglow");
h.takeCare();
}
}
abstract class Animal{
int age;
double w;
Animal(int a, double w){
age=a;
this.w=w;
}
abstract void eat(double x);
abstract void showProfile();
}
class Human extends Animal implements Swimmer, Father, Singer{
String name;
Human(int a, double w, String n) {
super(a, w);
name=n;
}
@Override
void eat(double x) {
System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
w+=x;
}
@Override
void showProfile() {
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
public void swim(double x){
System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
}
public void sing(String song){
System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+song+".");
}
public void takeCare(){
System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
}
}
interface Swimmer{
String LEVEL="專業級";
void swim(double x);
}
interface Singer{
String LEVEL="專業級";
void sing(String song);
}
interface Father{
String LEVEL="新手級";
void takeCare();
}
複製代碼
作者:
蔡忻霓
時間:
2021-7-16 20:54
public class Ch72 {
public static void main(String[] args) {
Human h1=new Human("湯霓", 35, 70);
h1.showProfile();
h1.eat(0.85);
h1.showProfile();
h1.swim(1500);
h1.sing("初禮來了");
h1.takeCare();
}
}
abstract class Animal
{
int age;
double w;
Animal(int a,double w)
{
age=a;
this.w=w;
}
abstract void eat(double x);
abstract void showProfile();
}
interface Swimmer
{
String LEVEL="專業級";
void swim(double x);
}
interface Singer
{
String LEVEL="專業級";
void sing(String x);
}
interface Father
{
String LEVEL="新手級";
void takeCare();
}
class Human extends Animal implements Swimmer, Singer, Father
{
String name;
Human(String n,int a,double w)
{
super(a,w);
name=n;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
void eat(double x)
{
System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
w+=x;
}
public void swim(double x)
{
System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
}
public void sing(String x)
{
System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
}
public void takeCare()
{
System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
}
}
複製代碼
作者:
林政瑜
時間:
2021-7-16 20:57
public class Ch01 {
public static void main(String[] args) {
Human h=new Human(35,70,"湯尼");
h.showProfile();
h.eat(0.85);
h.showProfile();
h.swim(1500);
h.sing("新不了情");
h.takeCare();
}
}
abstract class Animal
{
int age;
double w;
Animal(int a,double w){
age=a;
this.w=w;
}
abstract void eat(double x);
abstract void showProfile();
}
class Human extends Animal implements Swimmer,Singer,Father
{
String name;
Human(int a,double w,String n){
super(a,w);
name=n;
}
@Override
void eat(double x) {
System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
w+=x;
}
@Override
void showProfile() {
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
@Override
public void takeCare() {
System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
}
@Override
public void sing(String song) {
System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+song+".");
}
@Override
public void swim(double x) {
System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
}
}
interface Swimmer
{
String LEVEL="專業級";
void swim(double x);
}
interface Singer
{
String LEVEL="專業級";
void sing(String song);
}
interface Father
{
String LEVEL="新手級";
void takeCare();
}
複製代碼
作者:
陳宥穎
時間:
2021-7-16 20:58
public class Ch9999 {
public static void main(String[] args) {
Human h1=new Human("湯尼",35,70);
h1.showProfile();
h1.eat(0.85);
h1.showProfile();
h1.swim(1500);
h1.sing("新不了情");
h1.takeCare();
}
}
abstract class Animal
{
int age;
double w;
Animal(int a,double w)
{
age=a;
this.w=w;
}
abstract void eat(double x);
abstract void showProfile();
}
interface Swimmer
{
String LEVEL="專業級";
void swim(double x);
}
interface Singer
{
String LEVEL="專業級";
void sing(String x);
}
interface Father
{
String LEVEL="新手級";
void takeCare();
}
class Human extends Animal implements Swimmer, Singer, Father
{
String name;
Human(String n,int a,double w)
{
super(a,w);
name=n;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
void eat(double x)
{
System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
w+=x;
}
public void swim(double x)
{
System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
}
public void sing(String x)
{
System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
}
public void takeCare()
{
System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
}
}
複製代碼
作者:
董宸佑
時間:
2021-7-16 21:07
public class Ch01 {
public static void main(String[] args) {
Human h1=new Human("湯尼",35,70);
h1.showProfile();
h1.eat(0.85);
h1.showProfile();
h1.swim(1500);
h1.sing("新不了情");
h1.takeCare();
}
}
abstract class Animal{
int age;
double w;
Animal(int a, double w){
a=age;
this.w=w;
}
abstract void eat(double x);
abstract void showProfile();
}
class Human extends Animal implements Swimmer, Father, Singer{
String name;
Human(String n, int a, double w) {
super(a, w);
name=n;
}
void eat(double x){
System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
w+=x;
}
void showProfile(){
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
@Override
public void sing(String x) {
System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
}
@Override
public void takeCare() {
System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
}
@Override
public void swim(double x) {
System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
}
}
interface Father{
String LEVEL="新手級";
public abstract void takeCare();
}
interface Singer{
String LEVEL="專業級";
public abstract void sing(String x);
}
interface Swimmer{
String LEVEL="專業級";
public abstract void swim(double x);
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2