作者:gaoyong0713 | 来源:互联网 | 2024-11-20 17:46
本文探讨了多态的基本概念,包括其在现实世界和编程中的表现形式。多态不仅能够优化代码结构,减少重复代码,还能提高程序的灵活性和扩展性。文章通过具体的Java代码示例,展示了如何利用继承和方法重写来实现多态。
多态是面向对象编程中的一个重要概念,它允许一个接口或基类以多种形态出现,即不同的子类可以有不同的行为。这种机制有助于代码的优化,避免冗余,同时增加程序的灵活性。
在现实生活中,多态表现为同一事物在不同条件下产生不同的结果。例如,动物在遇到危险时,有的会逃跑,有的会攻击。而在编程中,多态则体现在通过相同的接口调用不同对象的方法,从而执行不同的操作。
实现多态的关键在于继承和方法重写。通过继承,子类可以从父类继承属性和方法,而方法重写则允许子类根据需要改变从父类继承来的方法的行为。
主人类示例
public class Owner {
// 为宠物治疗
public void treat(Pet pet) {
if (pet.getHealth() <50) {
pet.goToHospital();
}
}
// 给宠物喂食
public void feed(Pet pet) {
pet.eat();
}
}
宠物基类
public class Pet {
private String name = "Unnamed";
private int health = 100;
private int affection = 0;
public Pet() {
System.out.println("Base class no-arg constructor");
}
public Pet(String name) {
this.name = name;
}
public Pet(String name, int health, int affection) {
this.name = name;
setHealth(health);
setAffection(affection);
System.out.println("Base class parameterized constructor");
}
public void setHealth(int health) {
if (health <0 || health > 100) {
System.out.println("Please enter a value between 0 and 100!");
this.health = 60;
return;
}
this.health = health;
}
public int getHealth() {
return this.health;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAffection() {
return this.affection;
}
public void setAffection(int affection) {
if (affection <0 || affection > 100) {
System.out.println("Please enter a value between 0 and 100!");
this.affection = 60;
return;
}
this.affection = affection;
}
public void displayInfo() {
System.out.println("Pet confession:\nMy name is " + this.name + ", health is " + this.health + ", and affection with the owner is " + this.affection + ".");
}
public void goToHospital() {}
}
宠物狗子类
public class Dog extends Pet {
private String breed = "Clever Labrador Retriever";
public Dog() {
System.out.println("Subclass Dog no-arg constructor");
}
public Dog(String name, int health, int affection) {
super(name, health, affection);
System.out.println("Subclass Dog parameterized constructor");
}
public Dog(String name, int health, int affection, String breed) {
this(name, health, affection);
this.breed = breed;
System.out.println("Subclass Dog four-parameter constructor");
}
public String getBreed() {
return this.breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
@Override
public void displayInfo() {
super.displayInfo();
System.out.println(", I am a: " + this.breed);
}
@Override
public void goToHospital() {
System.out.println("Injection, take medicine");
setHealth(60);
}
}
宠物企鹅子类
public class Penguin extends Pet {
private String gender = "Q Baby";
public Penguin() {
System.out.println("Subclass Penguin no-arg constructor");
}
public Penguin(String name, int health, int affection, String gender) {
super(name, health, affection);
this.gender = gender;
System.out.println("Subclass Penguin parameterized constructor");
}
public String getGender() {
return this.gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public void displayInfo() {
super.displayInfo();
System.out.println(", my gender is: " + this.gender);
}
@Override
public void goToHospital() {
System.out.println("Take medicine, rest");
setHealth(60);
}
}
宠物测试类
import java.util.Scanner;
public class TestPet {
public static void main(String[] args) {
Owner owner = new Owner();
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the pet shop!");
System.out.println("Please enter the name of the pet you want to adopt:");
String name = scanner.next();
System.out.println("Please enter the type of pet you want to adopt: 1. Dog 2. Penguin");
int typeNo = scanner.nextInt();
switch (typeNo) {
case 1:
System.out.println("Please enter the pet's health value:");
int dHealth = scanner.nextInt();
System.out.println("Please enter the pet's affection with the owner:");
int dAffection = scanner.nextInt();
System.out.println("Please enter the pet's breed:");
String breed = scanner.next();
Pet dog = new Dog(name, dHealth, dAffection, breed);
dog.displayInfo();
System.out.println("*************************");
owner.treat(dog);
System.out.println("*************************");
dog.displayInfo();
break;
case 2:
System.out.println("Please choose the pet's gender: 1. Female 2. Male");
int genderId = scanner.nextInt();
String gender = (genderId == 1) ? "Female" : "Male";
System.out.println("Please enter the pet's health value:");
int pHealth = scanner.nextInt();
System.out.println("Please enter the pet's affection with the owner:");
int pAffection = scanner.nextInt();
Pet penguin = new Penguin(name, pHealth, pAffection, gender);
penguin.displayInfo();
System.out.println("*************************");
owner.treat(penguin);
System.out.println("*************************");
penguin.displayInfo();
break;
default:
System.out.println("We don't have that type of pet yet, please choose 1 or 2!");
break;
}
}
}