一、概述
在OOP这个概念中,所有的对象都是通过类来描述的;但是并不是所有的类都是用来描述对象的。如果一个类没有包含足够的信息来描述一个具体的对象,这样的类称为抽象类。
抽象类:(1)抽象类不能实例化对象,如果实例化某个对象,编译无法通过。只有抽象类的非抽象子类可以创建对象(2)可以包含:成员变量、成员方法、构造方法等。(3)构造方法、类方法(用static修饰的方法)不能声明为抽象方法
抽象方法:(1)如果一个类包含抽象方法,那么该类一定是抽象类(2)任何子类必须重写父类的抽象方法,或者声明自身为抽象类
定义:abstract class 类名
二、示例:农民喂动物
Animal:抽象类--------getName()、move()、drink()
Reptile、Mammal:继承抽象类
蛇、老虎、山羊、兔子:分别继承Reptile或Mammal
fammer:bringWater()、feedWater(Animal animal)---- 负责将水带到指定的地方,然后动物移动去目的地,喝水
代码如下:
package abstractDemo;
/**
* @author lpx
* @Description TODO
* @date 2021-04-07
*/
public abstract class Animal {
abstract String getName();
abstract void move(String destination);
abstract void drink();
}
abstract class Reptile extends Animal{
}
abstract class Mammal extends Animal{
}
class Tiger extends Mammal{
private static String name="tiger";
@Override
String getName() {
return this.name;
}
@Override
void move(String destination) {
System.out.println("tiger move to "+destination+".");
}
@Override
void drink() {
System.out.println("tiger lower it is head and drink");
}
}
class Goat extends Mammal{
private static String name="goat";
@Override
String getName() {
return this.name;
}
@Override
void move(String destination) {
System.out.println("goat move to "+destination+".");
}
@Override
void drink() {
System.out.println("goat lower it head to drink");
}
}
class Rabbit extends Mammal{
private static String name="rabbit";
@Override
String getName() {
return this.name;
}
@Override
void move(String destination) {
System.out.println("rabbit move to "+destination+".");
}
@Override
void drink() {
System.out.println("rabbit put out it is tongue and drink");
}
}
class Snake extends Reptile{
private static String name="snake";
@Override
String getName() {
return this.name;
}
@Override
void move(String destination) {
System.out.println("snake move to "+destination+".");
}
@Override
void drink() {
System.out.println("snake dived into and drink");
}
}
class Farmer{
public void bringWater(String destination){
System.out.println("Farmer bring water to " + destination + ".");
}
public void feedWater(Animal a){ // polymorphism
this.bringWater("Feeding Room");
a.move("Feeding Room");
a.drink();
}
}
class Test{
public static void main(String[] args) {
Farmer fm=new Farmer();
Snake snake=new Snake();
Goat goat=new Goat();
Tiger tiger=new Tiger();
Rabbit rabbit=new Rabbit();
fm.feedWater(snake);
fm.feedWater(goat);
fm.feedWater(tiger);
fm.feedWater(rabbit);
}
}
执行结果:
总结:
如果每种动物不是抽象出drink和move方法的话,就无法实现多态。农夫类就需要根据参数的不同重载多个feedWater。如果继续添加动物那么重载个数就越来越多。为了方便对比理解,我也按照常规思维去写了一下,代码如下:
package abstractDemo;
/**
* @author lpx
* @Description TODO
* @date 2021-04-07
*/
class Tiger1{
private static String name="tiger";
String getName() {
return this.name;
}
void move(String destination) {
System.out.println("tiger move to "+destination+".");
}
void drink() {
System.out.println("tiger lower it is head and drink");
}
}
class Goat1{
private static String name="goat";
String getName() {
return this.name;
}
void move(String destination) {
System.out.println("goat move to "+destination+".");
}
void drink() {
System.out.println("goat lower it head to drink");
}
}
class Rabbit1{
private static String name="rabbit";
String getName() {
return this.name;
}
void move(String destination) {
System.out.println("rabbit move to "+destination+".");
}
void drink() {
System.out.println("rabbit put out it is tongue and drink");
}
}
class Farmer1{
public void bringWater(String destination){
System.out.println("Farmer bring water to " + destination + ".");
}
public void feedWater(Goat1 goat){ // polymorphism
this.bringWater("Feeding Room");
goat.move("Feeding Room");
goat.drink();
}
public void feedWater(Tiger1 tiger){ // polymorphism
this.bringWater("Feeding Room");
tiger.move("Feeding Room");
tiger.drink();
}
public void feedWater(Rabbit1 rabbit1) {
this.bringWater("Feeding Room");
rabbit1.move("Feeding Room");
rabbit1.drink();
}
}
public class Test1{
public static void main(String[] args) {
Farmer1 farmer1=new Farmer1();
Tiger1 tiger1=new Tiger1();
Goat1 goat1=new Goat1();
Rabbit1 rabbit1=new Rabbit1();
farmer1.feedWater(tiger1);
farmer1.feedWater(goat1);
farmer1.feedWater(rabbit1);
}
}
手敲了这个例子,发现基础很重要呀,以前学习的不细致,导致很多地方迷糊不知道为啥那么写。
(1)一个.java文件中可以定义多个class类,但是只有一个可以定义为public,且这个类名必须和文件名相同
(2)抽象类继承抽象类,不用覆盖其抽象方法(原因也很简单呀,本身他就是抽象的不能实现方法体)。而非抽象类继承抽象类必须覆盖抽象方法,非抽象方法非必要。
(3)重写和重载的区别(高频面试题