接口和抽象类之间到底有什么区别?
当前回答
代表实际实现的抽象类和接口之间的差异。
接口:它是一个关键字,用于定义对象的模板或蓝图,它强制所有子类遵循相同的原型,作为实现,所有子类都可以根据其要求自由实现功能。
我们应该使用接口的一些其他用例。
两个外部对象(应用程序中的第三方集成)之间的通信通过此处的接口完成。
抽象类:抽象,它是一个关键字,当我们在任何类之前使用这个关键字时,它就成为抽象类。它主要用于我们需要定义模板以及所有子类后面的对象的一些默认功能时,这样它就删除了多余的代码和一个可以使用抽象类的用例,例如,我们不希望其他类可以直接实例化该类的对象,只有派生类可以使用该功能。
抽象类示例:
public abstract class DesireCar
{
//It is an abstract method that defines the prototype.
public abstract void Color();
// It is a default implementation of a Wheel method as all the desire cars have the same no. of wheels.
// and hence no need to define this in all the sub classes in this way it saves the code duplicasy
public void Wheel() {
Console.WriteLine("Car has four wheel");
}
}
**Here is the sub classes:**
public class DesireCar1 : DesireCar
{
public override void Color()
{
Console.WriteLine("This is a red color Desire car");
}
}
public class DesireCar2 : DesireCar
{
public override void Color()
{
Console.WriteLine("This is a red white Desire car");
}
}
接口示例:
public interface IShape
{
// Defines the prototype(template)
void Draw();
}
// All the sub classes follow the same template but implementation can be different.
public class Circle : IShape
{
public void Draw()
{
Console.WriteLine("This is a Circle");
}
}
public class Rectangle : IShape
{
public void Draw()
{
Console.WriteLine("This is a Rectangle");
}
}
其他回答
在实用性方面(JAVA),抽象类和接口之间的主要区别是抽象类可以保存状态。除了保持状态,我们还可以使用Interface实现其余操作。
通常抽象类用于某些东西的核心,但接口用于附加外围设备。
当您想为车辆创建基本类型时,应该使用抽象类,但如果您想添加一些不属于车辆基本概念的功能或属性,则应该使用接口,例如,您想添加“ToJSON()”函数。
接口具有广泛的抽象范围,而不是抽象类。您可以在传递的论证中看到这一点。请查看以下示例:
如果您使用vehicle作为参数,您可以使用它的派生类型之一(公共汽车或汽车同一类别只是车辆类别)。但当您使用IMoveable接口作为参数时,您有更多的选择。
当您想在继承层次结构中提供多态行为时,请使用抽象类。
当您想要完全无关的类的多态行为时,请使用接口。
如果您有一些可供多个类使用的通用方法,请使用抽象类。否则,如果你想让类遵循某种明确的蓝图,就去寻找接口。
以下示例演示了这一点。
Java中的抽象类:
abstract class Animals
{
// They all love to eat. So let's implement them for everybody
void eat()
{
System.out.println("Eating...");
}
// The make different sounds. They will provide their own implementation.
abstract void sound();
}
class Dog extends Animals
{
void sound()
{
System.out.println("Woof Woof");
}
}
class Cat extends Animals
{
void sound()
{
System.out.println("Meoww");
}
}
以下是Java中接口的实现:
interface Shape
{
void display();
double area();
}
class Rectangle implements Shape
{
int length, width;
Rectangle(int length, int width)
{
this.length = length;
this.width = width;
}
@Override
public void display()
{
System.out.println("****\n* *\n* *\n****");
}
@Override
public double area()
{
return (double)(length*width);
}
}
class Circle implements Shape
{
double pi = 3.14;
int radius;
Circle(int radius)
{
this.radius = radius;
}
@Override
public void display()
{
System.out.println("O"); // :P
}
@Override
public double area()
{
return (double)((pi*radius*radius)/2);
}
}
简而言之,一些重要的要点:
默认情况下,Java接口中声明的变量是final。抽象类可以有非final变量。默认情况下,Java接口中声明的变量是静态的。抽象类可以具有非静态变量。默认情况下,Java接口的成员是公共的。Java抽象类可以具有类成员的通常风格,如private、protected等。。
让我们再次讨论这个问题:
首先要告诉你的是,1/1和1*1的结果是相同的,但这并不意味着乘法和除法是相同的。显然,他们有着良好的关系,但请注意,你们两个都不同。
我将指出主要的区别,其余的已经解释过了:
抽象类对于建模类层次结构非常有用。乍一看任何需求,我们都部分清楚要构建什么,但我们知道要构建什么。所以抽象类就是基类。
接口对于让其他层次结构或类知道我能做什么很有用。当你说我有能力做某事时,你必须有这种能力。接口将标记类必须实现相同的功能。