在Java中,abstract關鍵字可以用于以下三個地方:
示例代碼:
abstract class Animal {
abstract void sound();
void sleep() {
System.out.println("Animal sleeps");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound();
animal.sleep();
}
}
示例代碼:
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound();
}
}
示例代碼:
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound();
}
}