抽象类和普通公共类有什么区别? [英] What is the difference between an abstract class and normal public class?

查看:155
本文介绍了抽象类和普通公共类有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抽象类和普通公共类之间有什么区别?



注意:

我见过很多不同但是我需要知道继承一个抽象类的使用,而继承一个公共类则具有相同的功能。

来自下面的例子,

1.想知道它们之间的区别(C:A和C:B)



我尝试过:



公共抽象类A

{

//一些成员;

}



公共舱B

{

//部分会员;

}



公共舱C:A

{

//访问A的成员;

}





公共舱C:B

{

//访问B的成员

}

What is the difference between an abstract class and normal public class?

Note:
I've seen many difference but i need to know the "use of inheriting an abstract class" while "inheriting a public class" does same functionality.
From Below example,
1.Want to know difference between( C : A and C:B )

What I have tried:

public abstract class A
{
//some members;
}

public class B
{
//some members;
}

public class C : A
{
// accessing the members of "A";
}


public class C : B
{
// accessing the memebers of "B"
}

推荐答案

差异不在C类中 - 它是抽象类与普通类的本质:一个摘要class无法实例化,从Abstract类派生的具体类必须实现基类的所有抽象成员。



所有这意味着你不能做这:

The difference isn't in the class C - it's in the nature of an Abstract Class vs a "normal" class: An Abstract class cannot be instantiated, and a concrete class derived from an Abstract class must implement all abstract members of the base class.

All that means is that you can't do this:
public abstract class A
    {
    }
...
    A a = new A();

因为编译器会抱怨A不是具体类。

当你需要一个类来提供常见的行为时,这非常有用,但是作为它自己的实例是无用的。

例如,你可能有一个抽象的Car类,它声明了一个抽象的Drive方法,派生的制造商模型类必须全部实现它。您可以驾驶任何基于汽车的实例,但要获得实际购买的车辆,它必须是FordFiesta,或MercedesAClass或MiniCooper。这些是可以创建实例的Cars的具体类。

Because the compiler will complain that A is not a concrete class.
This is really useful when you need a class to provide common behavior, but that is useless as an instance on it's own.
For example you might have an abstract Car class which declares an abstract Drive method, and the derived manufacturer model classes must all implement it. You can Drive any Car based instance, but to get a vehicle you can actually buy, it needs to be a FordFiesta, or a MercedesAClass, or a MiniCooper. These are the "concrete classes" of Cars for which instances can be created.


我将从为什么使用抽象类的角度回答你的问题。 br />


抽象类与非抽象类相比具有以下特性(普通类就是你所说的或接口)。



抽象类可以同时使用抽象方法和非抽象方法,例如 -

I am going to answer your question in the point of view of "Why do we use abstract class".

Abstract class has the following features in compare to a non-abstract class("normal class" is what you are saying or Interface).

An abstract class can have both abstract method and non-abstract method, Eg -
abstract class ExampleClass
      {
            //This is a Non-abstract method
            public int Add(int Num1, int Num2)
            {
                return Num1 + Num2;
            }

            //This is an abstract method
            //This method will be overridden in the derived class
            public abstract int Subtract(int Num1, int Num2);
      }





Abstract类的功能可以进一步扩展,而无需更改它的子类。

假设您的项目中有一个名为IExample的接口。现在,如果您对此接口进行任何更改,则其所有子类都将起作用。



当您需要基类提供某些方法的默认实现而其他方法被子类重写时,抽象类非常有用。使用接口有更多的限制,而Abstract类提供了混合的方法来做事。



最后,Abstract类的全部功能只能被理解通过在项目设计和实验中实现它,而不是试图在理论上理解。


抽象类是半定义的类,我们定义将由子类实现的抽象方法。

我们应该使用抽象类,原因如下:

1.在基类中保留一些常用代码

2.强制执行规则您的子类将遵循的类层次结构并实现抽象方法
Abstract class is half defined class where we define abstract methods which will be implemented by child classes.
We should use abstract classes for following reason:
1. To keep some common code in base class
2. To enforce rules in your class hierarchy that your child classes will follow and implement abstract methods


这篇关于抽象类和普通公共类有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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