Java---抽象定义及举例
⼀、概述
在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";
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");
java重写和重载的区别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.drink();
}
public void feedWater(Tiger1 tiger){ // polymorphism
this.bringWater("Feeding Room");
tiger.drink();
}
public void feedWater(Rabbit1 rabbit1) {
this.bringWater("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)重写和重载的区别(⾼频⾯试题 )
重载:定义相同的⽅法名,参数不同。属于编译时多态
重写:⼦类重写⽗类的⽅法@Override。属于运⾏时多态
今天先到这⾥啦,每天和⼤家共同学习共同进步,看到就是缘分,记得点赞 哦。bye~~~
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论