如何以及何时使用抽象类 [英] How and when to use an abstract class

查看:100
本文介绍了如何以及何时使用抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在Java中的测试程序。我想知道抽象类在这里有多重要,为什么我们为此使用抽象类。

This is my test program in Java. I want to know how much abstract class is more important here and why we use abstract class for this.

它是强制性的还是最好的方法;如果是这样的话?

Is it a mandatory or is it best method; if so how?

class Shape1 {
    int i = 1;
    void draw() {
        System.out.println("this is shape:" + i);
    }
}

class Shape2 {
    int i = 4;
    void draw() {
        System.out.println("this is shape2:" + i);
    }
}


class Shape {
    public static void main(String args[]) {
        Shape1 s1 = new Shape1();
        s1.draw();

        Shape2 s2 = new Shape2();
        s2.draw();
    }
}


推荐答案

摘要类是包含一个或多个抽象方法的类。抽象方法是声明的方法,但不包含任何实现。抽象类可能无法实例化,并且需要子类来提供抽象方法的实现。让我们看一个抽象类和抽象方法的例子。

Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods. Let's look at an example of an abstract class, and an abstract method.

假设我们通过创建一个以基类开头的类层次来建模动物的行为叫动物。动物能够做不同的事情,如飞行,挖掘和行走,但也有一些常见的操作,如吃饭和睡觉。一些常见的操作由所有动物执行,但也以不同的方式执行。当以不同的方式执行操作时,它是抽象方法的一个很好的候选者(强制子类提供自定义实现)。让我们来看一个非常原始的动物基类,它定义了一种用于发出声音的抽象方法(例如狗吠,牛吼或猪叮当)。

Suppose we were modeling the behavior of animals, by creating a class hierachy that started with a base class called Animal. Animals are capable of doing different things like flying, digging and walking, but there are some common operations as well like eating and sleeping. Some common operations are performed by all animals, but in a different way as well. When an operation is performed in a different way, it is a good candidate for an abstract method (forcing subclasses to provide a custom implementation). Let's look at a very primitive Animal base class, which defines an abstract method for making a sound (such as a dog barking, a cow mooing, or a pig oinking).

public abstract class Animal
{
   public void eat(Food food)
   {
        // do something with food.... 
   }

   public void sleep(int hours)
   {
        try
    {
        // 1000 milliseconds * 60 seconds * 60 minutes * hours
        Thread.sleep ( 1000 * 60 * 60 * hours);
    }
    catch (InterruptedException ie) { /* ignore */ } 
   }

   public abstract void makeNoise();
}

请注意,abstract关键字用于表示抽象方法和抽象类。现在,任何想要实例化的动物(如狗或牛)都必须实现makeNoise方法 - 否则无法创建该类的实例。让我们看一下扩展Animal类的Dog and Cow子类。

Note that the abstract keyword is used to denote both an abstract method, and an abstract class. Now, any animal that wants to be instantiated (like a dog or cow) must implement the makeNoise method - otherwise it is impossible to create an instance of that class. Let's look at a Dog and Cow subclass that extends the Animal class.

public Dog extends Animal
{
   public void makeNoise() { System.out.println ("Bark! Bark!"); }
}

public Cow extends Animal
{
   public void makeNoise() { System.out.println ("Moo! Moo!"); }
}

现在您可能想知道为什么不将抽象类声明为接口,让Dog和Cow实现界面。当然可以 - 但你还需要实施吃饭和睡眠方法。通过使用抽象类,您可以继承其他(非抽象)方法的实现。你不能用接口做到这一点 - 接口不能提供任何方法实现。

Now you may be wondering why not declare an abstract class as an interface, and have the Dog and Cow implement the interface. Sure you could - but you'd also need to implement the eat and sleep methods. By using abstract classes, you can inherit the implementation of other (non-abstract) methods. You can't do that with interfaces - an interface cannot provide any method implementations.

这篇关于如何以及何时使用抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆