標題:
介面 (二)
[打印本頁]
作者:
tonyh
時間:
2023-4-8 15:37
標題:
介面 (二)
本帖最後由 tonyh 於 2023-4-8 16:06 編輯
[設計導引]
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. 定義一個介面: Genius 多重繼承 Swimmer, Singer, Father 這三個介面
在該介面中新增一抽象方法 fly()
5. 讓類別 Human 實作介面 Genius
並實作所有抽象方法: swim(double x), sing(String song), takeCare(), fly()
6. 在 Human 類別下新增一物件, 並利用該物件帶出所有方法, 如執行畫面所示
public class Ch73 {
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();
h1.fly();
}
}
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();
}
interface Genius extends Swimmer, Singer, Father
{
String LEVEL="大神級";
void fly();
}
class Human extends Animal implements Genius
{
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+"水準,開始扮演父親的角色,照顧小孩.");
}
public void fly()
{
System.out.println(name+"以"+Genius.LEVEL+"水準,快速揮動兩片扇子,飛了起來!");
}
}
複製代碼
作者:
鄞美旭
時間:
2023-4-8 16:25
public class A {
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();
h.fly();
}
}
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 song);
}
interface Father
{
String LEVEL="新手級";
void takeCare();
}
interface Genius extends Swimmer,Singer, Father
{
String LEVEL="大神級";
void fly();
}
class Human extends Animal implements Genius
{
String name;
Human(String n, int a, double w)
{
super(a, w);
name=n;
}
@Override
void eat(double x) {
System.out.println(name+"咕嚕咕嚕吃下"+x+"公斤的食物。");
w=w+x;
}
@Override
void showProfile() {
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤。");
}
@Override
public void swim(double x) {
System.out.println(name+"以"+Swimmer.LEVEl+"水準,刷刷刷快速地遊了"+x+"公尺。");
}
@Override
public void sing(String song) {
System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+song+"。");
}
@Override
public void takeCare() {
System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩。");
}
@Override
public void fly() {
System.out.println(name+"以"+Genius.LEVEL+"水準,快速揮動兩片扇子,飛了起來!");
}
}
複製代碼
作者:
夏子甯
時間:
2023-4-8 16:26
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();
h.fly();
}
}
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();
}
interface Genius extends Swimmer, Singer,Father
{
String LEVEL="大神級";
void fly();
}
class Human extends Animal implements Genius
{
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;
}
@Override
public void swim(double x) {
System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
}
@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 fly() {
System.out.println(name+"以"+Genius.LEVEL+"水準,快速揮動兩片扇子,飛了起來!");
}
}
複製代碼
作者:
秉宏1
時間:
2023-4-8 16:27
public class Ch73 {
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();
h1.fly();
}
}
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();
}
interface Genius extends Swimmer, Singer, Father
{
String LEVEL="大神級";
void fly();
}
class Human extends Animal implements Genius
{
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+"水準,開始扮演父親的角色,照顧小孩.");
}
public void fly()
{
System.out.println(name+"以"+Genius.LEVEL+"水準,快速揮動兩片扇子,飛了起來!");
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2