為什么我用接口實(shí)現(xiàn)狗看門的功能出錯(cuò)?
public static void main(String[] args) {
// TODO Auto-generated method stub
? ??
? ? Iwatchdoor wt1 = new Dog();//為什么出現(xiàn)這個(gè)錯(cuò)誤Cannot instantiate the type Dog
? ? wt1.watchDoor();
? ? Iwatchdoor wt2 = new Door();
? ? wt2.watchDoor();
? ??
}
public abstract class Dog extends Animal implements Iwatchdoor {
public void eat(){
System .out .println("狗具有吃肉的能力");
}
public void watchDoor(){
System.out .println("狗具有看門的能力");
}
}
2016-04-18
你的狗吃肉 和狗看門都放在同一個(gè)類里面的,所以你直接用wt1調(diào)用狗看門就可以了 ??
你的? Iwatchdoor wt2 = new Door();不對(duì) ? ? Door是一個(gè)方法
看不到你全部代碼,也不知道我說的對(duì)不
2016-09-26
public abstract class Dog extends Animal implements Iwatchdoor {
這個(gè)Dog類好像沒必要是抽象類吧,你加了abstract,抽象類不能創(chuàng)建對(duì)象的。
2016-04-19
package com.imooc;
public abstract class Dog extends Animal implements Iwatchdoor {
public void eat(){
System .out .println("狗具有吃肉的能力");
}
public void watchDoor(){
System.out .println("狗具有看門的能力");
}
}
package com.imooc;
public abstract interface Iwatchdoor {
public abstract void watchDoor();
}
package com.imooc;
public class Initial {
public static void main(String[] args) {
// TODO Auto-generated method stub
? ??
? ? Iwatchdoor wt1 = new Dog();//為什么出現(xiàn)這個(gè)錯(cuò)誤Cannot instantiate the type Dog
? ? wt1.watchDoor();
? ? Iwatchdoor wt2 = new Door();
? ? wt2.watchDoor();
? ??
}
}