如何通过抽象方法和抽象类实现抽象? [英] How abstraction is achieved through abstract methodsa and abstract classes?

查看:121
本文介绍了如何通过抽象方法和抽象类实现抽象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抽象是代表基本特征而不代表背景细节。但是我如何使用抽象类来实现这一目标呢?或抽象类和抽象中没有关系?

Abstraction is "To represent the essential feature without representing the background details." But how I can achieve this using abstract class? Or is there no relation in abstract class and Abstraction?

推荐答案

抽象,可以是英语中的名词,形容词和动词;在一般的OOP上下文中,我们可以说'抽象类或方法表示虚拟(或元)信息,可能被其他类使用(需要使用)的信息,或者从它们继承的方法。



抽象既可以表示OOP设计原则(名词),也可以指在设计和代码中创建抽象实体的过程(动词)。



为了在C#.NET中实现脚踏实地,'Abstract关键字表示Class是一个实体:



0.旨在用作模板定义(抽象成员)和真实方法(非抽象)的实现的提供者,从而继承自它的类。



如果Abstract类中的所有内容都被声明为'abstract:那么你可以说该Class等同于一个虚拟类。 Abstract Class实现的Real方法可以被视为向继承者提供默认行为。



并且其他类成员声明抽象没有实现。



1.它无法用'new'实例化。所以,不要定义公共构造函数。



2.它不能被密封。



3它不能将任何成员(属性,字段,方法)声明为'private



4.它不支持多重继承(不能从类或接口继承)



5.'抽象方法不能声明主体



那么,使用'抽象类做什么呢?你呢?



1.让你结合一些功能



a。接口...来自非抽象类的继承者必须实现声明为'abstract



...的每个Class成员......以及......



b。一个常规类,提供继承类可以使用或覆盖的实现中的构建。



2.可以不会破坏的方式扩展继承它的类。



虽然这是一篇简短(密集,高级)的小文章,但我认为阅读这篇文章可能会让你了解权力(可扩展性) )可以使用'abstract /'覆盖协同来调节类的行为:[ ^ ]。



这是使用抽象类的一个有点幻想的例子:



"Abstract," can be both noun, adjective, and verb, in English; in general OOP context we can say that 'Abstract Classes or Methods express virtual (or meta-) information, information which may be used (is required to be used) by other Classes, or Methods that inherit from them.

"Abstraction" can mean both a principle of OOP design (noun), and refer to the process (verb) of creating abstract entities in design, and code.

To bring this "down to earth:" in C# .NET, the 'Abstract keyword signifies that a Class is an entity which:

0. is intended to be used as a "provider" of both/either template definitions (abstract members) and implementations of "real" Methods (non-abstract) to Classes that inherit from it.

if everything in an Abstract class is declared 'abstract: then you can say that that Class is equivalent to a Virtual Class. "Real" methods that the Abstract Class implements can be considered as providing "default" behaviors to inheritors.

And that other Class Members declared 'abstract have no "implementation."

1. it can not be instantiated with 'new. so, don't define a public constructor.

2. it cannot be sealed.

3. it cannot have any members (Properties, Fields, Methods) declared 'private

4. it doesn't support multiple inheritance (cannot inherit from Class or Interface)

5. 'abstract Methods cannot declare bodies

So, what does using an 'Abstract Class do for you ?

1. let's you combine some features of

a. Interface ... inheritors from non-abstract Classes must implement each Class member declared as 'abstract

... and ...

b. a "regular" Class that provide build in implementations that the inheriting Class can use, or over-ride.

2. extendable in a way that does not "break" Classes that inherit it.

While this is a short (and dense, and advanced) small article, I think reading this may give you an idea of the power (extendability) possible using the 'abstract/'override synergy to regulate the behavior of Classes: [^].

Here's a somewhat fanciful example of usage of an Abstract Class:

using System;

namespace AbstractClassNameSpace
{
    // show that an abstract class can inherit from an Interface
    interface someInterface
    {
        int someInt { set; get; }
        string someString { set; get; }
    }

    // demonstrate how an abstract class can allow poor design choices !
    // that don't cause compile-time errors: i.e., fields with duplicate names
    interface someOtherInterface
    {
        int someInt { set; get; }
        string someString { set; get; }

        double someBigNumber { set; get; }
    }

    public abstract class AbstractSandBox : someInterface, someOtherInterface
    {
        // required "implementation" of Interface properties can be 'abstract
        public abstract int someInt { get; set; }
        public abstract string someString { get; set; }

        // required "implementation" of Interface property can be an implementation
        public double someBigNumber { get; set; }

        // an implementation not required by inherited Interfaces
        public double hypotenuse(double sidea, double sideb )
        {
            return Math.Sqrt((sidea*sidea) + (sideb*sideb));
        }

        // an abstract Method not required by inherited Interfaces
        public abstract string someWeirdStringStuff(string a, string b);
    }

    public abstract class SomeInheritingClass: AbstractSandBox
    {
        // required by inheritance from Abstract Class
        public override int someInt { get; set; }
        public override string someString { get; set; }

        // not required, and we can't override it
        // but we can hide it, by declaring it with 'new
        // and access the Abstract Class implementation
        // using the keyword .base
        public new double hypotenuse(double sidea, double sideb)
        {
            // call the base method in the Abstract Class
            double hyp = base.hypotenuse(sidea, sideb);

            // we're in a different galaxy now with different geometry
            // where we have to translate :)
            return hyp / Math.E * Math.PI / Math.Log10(hyp);
        }

        // required implementation of Abstract Method defined in from Abstract Class
        // but, by declaring it abstract override we express our intent
        // not to "use it" in this class, but to only use it from inheriting classes
        public abstract override string someWeirdStringStuff(string a, string b);
    }

    // non-abstract Class
    public class GrandchildofAbstractClass : SomeInheritingClass
    {
        public void AccessParentClass()
        {
// set break-point here
            Console.WriteLine(this.hypotenuse(3.0, 4.0));
            Console.WriteLine(this.someWeirdStringStuff("whoops", "adaisy"));
        }

        // accessing the implementation in the Abstract Class 'AbstractSandBox
        public void UseAbstractClassSandBox(string a, string b)
        {
// set break-point here
            Console.WriteLine(this.someWeirdStringStuff(a, b));
        }
    }
}

如果您支持它,我建议您将此代码粘贴到Visual Studio中的项目中,然后添加断点

AccessParentClass和UseAbstractClassSandBox方法中的GrandchildofAbstractClass的代码。



然后,添加自己的代码来创建'的实例' GrandchildofAbstractClass,并尝试调用这些方法,根据需要传递值。



使用F11单步执行代码并观察控制流。

If you are "up for it," I suggest you paste this code into a Project in Visual Studio, then add break-points in the code for the GrandchildofAbstractClass in both the
AccessParentClass and UseAbstractClassSandBox Methods.

Then, add your own code to create an instance of the 'GrandchildofAbstractClass, and try calling those methods, passing values as required.

Single-step through the code with F11 and watch the flow-of-control.


这篇关于如何通过抽象方法和抽象类实现抽象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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