Oop 接口和抽象类之间的区别是什么?
接口和抽象类之间到底有什么区别?可以在这里找到解释:Oop 接口和抽象类之间的区别是什么?,oop,interface,abstract-class,Oop,Interface,Abstract Class,接口和抽象类之间到底有什么区别?可以在这里找到解释: 抽象类是指 仅部分由 程序员它可能包含一个或多个 抽象方法。抽象方法 只是一个函数定义 用于告诉程序员 方法必须在子对象中实现 班级 接口类似于抽象 阶级;事实上,接口占据了市场份额 与类和抽象名称空间相同 上课。因此,你不能 定义具有相同名称的接口 作为一个班级。接口是一个完整的接口 抽象类;它的任何方法 是实现的,而不是类 从它分类,据说是 实现该接口 无论如何,我觉得这个接口的解释有些混乱。一个更常见的定义是:接口定义了实现类必须履行的
抽象类是指 仅部分由 程序员它可能包含一个或多个 抽象方法。抽象方法 只是一个函数定义 用于告诉程序员 方法必须在子对象中实现 班级 接口类似于抽象 阶级;事实上,接口占据了市场份额 与类和抽象名称空间相同 上课。因此,你不能 定义具有相同名称的接口 作为一个班级。接口是一个完整的接口 抽象类;它的任何方法 是实现的,而不是类 从它分类,据说是 实现该接口 无论如何,我觉得这个接口的解释有些混乱。一个更常见的定义是:接口定义了实现类必须履行的契约。接口定义由公共成员的签名组成,没有任何实现代码。接口 接口是一种契约:编写接口的人说,“嘿,我接受这样的东西”,而使用接口的人说“好的,我写的类看起来是那样的” 接口是一个空壳。只有方法的签名,这意味着这些方法没有主体。接口不能做任何事情。这只是一种模式 例如(伪代码):
//我说所有的机动车都应该是这样的:
接口机动车
{
无效运行();
int getFuel();
}
//我的队友同意了,并写下了那个样子的车辆
汽车类
{
int燃料;
无效运行()
{
打印(“wroooooom”);
}
int getFuel()
{
还这个。燃料;
}
}
实现一个接口只消耗很少的CPU,因为它不是一个类,只是一堆名称,因此不需要进行任何昂贵的查找。它在重要的时候非常有用,比如在嵌入式设备中
抽象类 与接口不同,抽象类是类。它们的使用成本更高,因为当您继承它们时,需要进行查找 抽象类看起来很像接口,但它们有更多的东西:您可以为它们定义行为。这更像是一个人说:“这些类应该是这样的,它们有共同点,所以请填空!” 例如:
//我说所有的机动车都应该是这样的:
抽象类机动车
{
int燃料;
//它们都有燃料,所以让我们为每个人实现这一点。
int getFuel()
{
还这个。燃料;
}
//这可能是非常不同的,迫使他们提供
//自己的实现。
抽象空运行();
}
//我的队友遵从了我的要求,写下了那个样子的车辆
汽车类
{
无效运行()
{
打印(“wroooooom”);
}
}
实施 虽然抽象类和接口被认为是不同的概念,但实现有时会使这种说法不真实。有时候,他们甚至不是你想象的那样 在Java中,这个规则是强制执行的,而在PHP中,接口是没有声明方法的抽象类 在Python中,抽象类更像是一种编程技巧,您可以从ABC模块获得它,它实际上使用元类,因此使用类。在这种语言中,接口与duck类型更相关,它是约定和调用描述符的特殊方法(方法)的混合体
与编程一样,在另一种语言中有理论、实践和实践:-)并不是原始问题的答案,但一旦你找到了它们之间差异的答案,你将进入何时使用每一个难题:
我对OOP的了解有限,但到目前为止,将接口视为语法中形容词的等价物一直对我有效(如果这种方法是假的,请纠正我!)。例如,接口名称类似于可以赋予类的属性或功能,一个类可以有许多属性或功能:ISerializable、ICountable、IList、iCachable、IHappy等等
- 抽象类可以有常量、成员、方法存根(没有主体的方法)和定义的方法,而接口只能有常量和方法存根
- 抽象类的方法和成员可以用任何可见性定义,而接口的所有方法都必须定义为
(默认情况下,它们定义为public)public
- 继承抽象类时,具体的子类必须定义抽象方法,而抽象类可以扩展另一个抽象类,并且不必定义父类的抽象方法
- 类似地,扩展另一个接口的接口不负责从父接口实现方法。这是因为接口不能定义任何实现
- 子类只能扩展单个类(抽象或具体),而接口可以扩展,或者类可以实现多个其他接口
- 子类可以定义具有相同或较少限制可见性的抽象方法,而实现接口的类必须定义具有完全相同可见性(public)的方法
interface Package {
String address();
}
class Box implements Package, Property {
@Override
String address() {
return "5th Street, New York, NY";
}
@Override
Human owner() {
// this method is part of another contract
}
}
abstract class GpsBox implements Package {
@Override
public abstract String address();
protected Coordinates whereAmI() {
// connect to GPS and return my current position
}
}
final class DirectBox implements Package {
private final String to;
public DirectBox(String addr) {
this.to = addr;
}
@Override
public String address() {
return this.to;
}
}
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");
}
}
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");
}
}
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);
}
}
abstract class Car
{
string color;
int speed;
}
class HondaAccord : Car
{
MusicPlayer musicPlayer;
}
abstract class Car
{
string color;
int speed;
}
class HondaAccord : Car, IInflateAir, IRefueling
{
MusicPlayer musicPlayer;
}