如何通过抽象方法和抽象类实现抽象

如何通过抽象方法和抽象类实现抽象

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

问题描述

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

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?

推荐答案

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.


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

08-03 18:34