多态
Java中的多态是指同一个方法在不同的对象上有不同的行为:
案例一
以下有四个类 : 动物类与狗、猫类 Test测试类
// 动物类
class Animal {
public void sound() {
System.out.println("动物发出声音");
}
}
// 狗类
class Dog extends Animal {
@Override
public void sound() {
System.out.println("狗发出汪汪的声音");
}
}
// 猫类
class Cat extends Animal {
@Override
public void sound() {
System.out.println("猫发出喵喵的声音");
}
}
// 测试类
public class PolymorphismExample {
public static void main(String[] args) {
Animal animal1 = new Dog(); // 父类引用指向子类对象
Animal animal2 = new Cat(); // 父类引用指向子类对象
animal1.sound(); // 调用的是子类Dog的sound()方法
animal2.sound(); // 调用的是子类Cat的sound()方法
}
}
以上程序会输出:
狗发出汪汪的声音
猫发出喵喵的声音
案例二
以下有四个类 : 车类与宝马、奔驰子类 Test测试类
// 车类
class Car {
public void drive() {
System.out.println("开车");
}
}
// 宝马类
class BMW extends Car {
@Override
public void drive() {
System.out.println("开宝马");
}
}
// 奔驰类
class Benz extends Car {
@Override
public void drive() {
System.out.println("开奔驰");
}
}
// 测试类
public class PolymorphismExample {
public static void main(String[] args) {
Car car1 = new BMW(); // 父类引用指向子类对象
Car car2 = new Benz(); // 父类引用指向子类对象
car1.drive(); // 调用的是子类BMW的drive()方法
car2.drive(); // 调用的是子类Benz的drive()方法
}
}
以上程序会输出:
开宝马
开奔驰
案例三
abstract class Animal {
public abstract void move();
}
class Cat extends Animal {
@Override
public void move() {
System.out.println("Cat is walking");
}
}
class Dog extends Animal {
@Override
public void move() {
System.out.println("Dog is running");
}
}
class Bird extends Animal {
@Override
public void move() {
System.out.println("Bird is flying");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Animal cat = new Cat();
Animal dog = new Dog();
Animal bird = new Bird();
cat.move(); // output: Cat is walking
dog.move(); // output: Dog is running
bird.move(); // output: Bird is flying
}
}
抽象类
抽象类是指不能实例化的类,只能作为其他类的基类来使用。下面是一些Java抽象类的案例以及答案:
案例一
Shape(形状)抽象类:
abstract class Shape {
public abstract double calculateArea();
public abstract double calculatePerimeter();
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double calculateArea() {
return length * width;
}
public double calculatePerimeter() {
return 2 * (length + width);
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double calculateArea() {
return Math.PI * radius * radius;
}
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
public class Main {
public static void main(String[] args) {
Shape rectangle = new Rectangle(5, 4);
System.out.println("Rectangle Area: " + rectangle.calculateArea());
System.out.println("Rectangle Perimeter: " + rectangle.calculatePerimeter());
Shape circle = new Circle(3);
System.out.println("Circle Area: " + circle.calculateArea());
System.out.println("Circle Perimeter: " + circle.calculatePerimeter());
}
}
以上程序输出结果为:
Rectangle Area: 20.0
Rectangle Perimeter: 18.0
Circle Area: 28.274333882308138
Circle Perimeter: 18.84955592153876
案例二
Animal(动物)抽象类:
abstract class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public abstract void sound();
public void eat() {
System.out.println(name + " is eating.");
}
}
class Dog extends Animal {
public Dog(String name) {
super(name);
}
public void sound() {
System.out.println("Dog barks.");
}
}
class Cat extends Animal {
public Cat(String name) {
super(name);
}
public void sound() {
System.out.println("Cat meows.");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog("Tom");
dog.sound();
dog.eat();
Animal cat = new Cat("Jerry");
cat.sound();
cat.eat();
}
}
输出结果:
Dog barks.
Tom is eating.
Cat meows.
Jerry is eating.
案例三
一个抽象类 `Animal` 和两个继承类 `Dog` 和 `Cat` 的用法。
abstract class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract void makeSound();
}
class Dog extends Animal {
public Dog(String name) {
super(name);
}
public void makeSound() {
System.out.println(getName() + " is barking.");
}
}
class Cat extends Animal {
public Cat(String name) {
super(name);
}
public void makeSound() {
System.out.println(getName() + " is meowing.");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog("Tom");
dog.makeSound();
Animal cat = new Cat("Jerry");
cat.makeSound();
}
}
输出结果:
Tom is barking.
Jerry is meowing.
接口
接口定义了一系列方法的签名(方法名、参数列表和返回类型),而具体的实现则由实现该接口的类来完成
案例一
一个接口 `Drawable` 和两个实现类 `Circle` 和 `Rectangle` 的用法。
interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() {
System.out.println("Drawing a circle.");
}
}
class Rectangle implements Drawable {
public void draw() {
System.out.println("Drawing a rectangle.");
}
}
public class Main {
public static void main(String[] args) {
Drawable circle = new Circle();
circle.draw();
Drawable rectangle = new Rectangle();
rectangle.draw();
}
}
输出结果:
Drawing a circle.
Drawing a rectangle.
案例二
定义一个"动物"的接口,具有"吃"和"睡"两个方法。具体的动物类可以根据需要来实现这个接口,并实现自己独特的行为。
// 定义动物接口
interface Animal {
void eat();
void sleep();
}
// 实现动物接口的猫类
class Cat implements Animal {
@Override
public void eat() {
System.out.println("猫正在吃饭");
}
@Override
public void sleep() {
System.out.println("猫正在睡觉");
}
}
// 实现动物接口的狗类
class Dog implements Animal {
@Override
public void eat() {
System.out.println("狗正在吃骨头");
}
@Override
public void sleep() {
System.out.println("狗正在打盹");
}
}
// 测试类
public class Main {
public static void main(String[] args) {
Animal cat = new Cat();
Animal dog = new Dog();
cat.eat(); // 输出:猫正在吃饭
cat.sleep(); // 输出:猫正在睡觉
dog.eat(); // 输出:狗正在吃骨头
dog.sleep(); // 输出:狗正在打盹
}
}
好,以上内容就是今天学习内容的案例 !!!