public class Animal { public void makeSound() { System.out.println("Animal is making a sound."); }}public class Dog extends Animal { @Override public void makeSound() { System.out.println("Dog is barking."); }}public class Cat extends Animal { @Override public void makeSound() { System.out.println("Cat is meowing."); }}public class AnimalSound { public static void main(String[] args) { Animal animal1 = new Dog(); Animal animal2 = new Cat(); animal1.makeSound(); animal2.makeSound(); }}
时间: 2024-04-04 17:31:50 浏览: 22
这段代码展示了Java中的继承和多态特性。Animal类是一个基类,其中定义了一个方法makeSound(),输出“Animal is making a sound.”。Dog和Cat类都是继承自Animal类的子类,并且分别重写了makeSound()方法,输出Dog和Cat特有的声音。
在AnimalSound类的main()方法中,通过创建一个Dog对象和一个Cat对象,分别调用它们的makeSound()方法。由于这两个对象都是Animal类型的引用,因此它们执行makeSound()方法时会根据实际类型调用对应子类的方法,从而输出不同的声音。
这种基于继承和多态的设计方式可以使代码更加灵活,可以方便地增加新的子类,而不需要修改基类的代码。
相关问题
All method binding in Java uses late binding unless the method is static or final (private methods are implicitly final).
Yes, that's correct. In Java, all non-static and non-final methods use late binding (also known as dynamic binding or run-time binding) by default. This means that the method implementation to be called is decided at runtime based on the actual type of the object that the method is being called on.
For example, consider the following code:
```
class Animal {
public void makeSound() {
System.out.println("Animal is making a sound");
}
}
class Cat extends Animal {
public void makeSound() {
System.out.println("Meow!");
}
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Woof!");
}
}
public class Main {
public static void main(String[] args) {
Animal animal1 = new Cat();
Animal animal2 = new Dog();
animal1.makeSound(); // output: Meow!
animal2.makeSound(); // output: Woof!
}
}
```
In this example, we have a parent class `Animal` and two child classes `Cat` and `Dog`. Each class has its own implementation of the `makeSound()` method. In the `main()` method, we create two objects of type `Animal`, but their actual types are `Cat` and `Dog`, respectively. When we call the `makeSound()` method on these objects, the implementation that gets called is based on the actual type of the object, not the declared type of the variable.
However, if a method is declared as static or final, it uses early binding (also known as static binding or compile-time binding) instead of late binding. In the case of a static method, the method implementation to be called is determined at compile time based on the declared type of the variable. In the case of a final method, the method implementation is also determined at compile time, but cannot be overridden by any subclasses.
17、(1分) 在Java中,哪个关键字用于实现多态? A. abstract B. final C noneD. static
在Java中,关键字`abstract`和`final`都不是用于实现多态的,而是用于限制类或方法的特性。正确答案是C、没有关键字用于实现多态。
多态是面向对象编程的一个重要概念,指同一操作作用于不同的对象,可以有不同的解释和不同的实现。在Java中,多态是通过继承、接口、方法重载和方法重写等机制实现的,没有专门的关键字用于实现多态。
例如,可以通过定义一个父类和多个子类,让它们重写父类中的方法,从而实现多态:
```java
class Animal {
public void makeSound() {
System.out.println("Animal is making sound...");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Dog is barking...");
}
}
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Cat is meowing...");
}
}
public class Main {
public static void main(String[] args) {
Animal animal1 = new Dog();
Animal animal2 = new Cat();
animal1.makeSound(); // 输出 "Dog is barking..."
animal2.makeSound(); // 输出 "Cat is meowing..."
}
}
```
在上面的例子中,Animal类定义了一个makeSound()方法,Dog和Cat类分别继承了Animal类并重写了makeSound()方法,然后通过创建Animal类的引用变量,分别指向Dog和Cat类的实例,实现了多态。
因此,本题答案为C、没有关键字用于实现多态。