我该如何解决这个C#难题? [英] How do I solve this C# conundrum?

查看:72
本文介绍了我该如何解决这个C#难题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我完全迷失了。这是我的作业,但我无法弄清楚如何处理以下界面中缺少的脚本/部分:



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 Calculate_Area {
class 计划{


/// < 摘要 >
///
/// < / summary >
/// < param name =option < span class =code-summarycomme nt>> < / param >
静态 void CalculateArea( int 选项){

if (option == 1 ){
CircleArea();
} else if (option == 2 ){
RectangleArea();
} else {
CylinderArea();

} // end if else


} // end CalculateArea



/// < 摘要 >
/// 输入用户选择
/// < / summary >
/// < < span class =code-summarycomment>返回 > 1,2,3或0 < / returns >
static int ReadMenuOption(){

int option = 0 ;
bool validMenuOption = false ;

do {
option = int .Parse(Console。的ReadLine());

if ((选项> = 0 )&&(选项< = 3 )){
validMenuOption = true ;
} else {
Console.WriteLine( \ n\t选项必须为1,2,3或0);
DisplayMenu();
} // 如果

} while (!validMenuOption);

return 选项;
} // end ReadMenuOption




/// < 摘要 >
/ // 显示形状菜单
/// < / summary >
static void DisplayMenu(){
string menu = \ n 1)Circle
+ \ n 2)矩形
+ \ n 3)气缸(已关闭)
+ \ n \ n输入您的选择(1,2,3或0退出):;

Console.Write(menu);
} // end DisplayMenu



静态 void Main(){
int menuOption;

DisplayMenu();
menuOption = ReadMenuOption();

if (menuOption!= 0 ){
CalculateArea( menuOption);
} // end if if


控制台.ReadKey();
}
}
}

解决方案

所有选项1,2,3都很大编程滥用,并且对于单独类型的形状具有选项和switch语句是绝对的OOP滥用;但一般的1,2,3滥用更糟糕。你从一开始就做错了。



我担心你还没有准备好听到答案,但我会给你一些要点。 br />


您需要使用OOP 后期绑定多态性。首先,您需要使用抽象(因此,虚拟)方法的一些基类,例如 GalculateArea()。此方法不应该有参数,因为隐式this参数将始终为您提供对这些实例方法的对象的引用。在每个派生类中重写它们,例如 Rectangle Ellipse 等等。您的运行时类型始终是非抽象的,但您可以拥有基类类型的编译时类型的变量/成员。如果在某个实例上调用这些方法,则调用将始终动态调度到(派生)运行时类型的方法。 所以,你只需要一个所有类型的电话。这个机制位于OOP的核心。



基于此,你可以具有由集合表示的不同运行时类型的多态对象集合,具有基类类型的编译类型元素类型。类似的机制可以基于接口



你真的需要从头开始学习它。



参见:

http://en.wikipedia.org/wiki / Dynamic_dispatch [ ^ ],

http://en.wikipedia.org/wiki/Virtual_function [ ^ ],

http://en.wikipedia.org/wiki/Virtual_method_table [ ^ ],

http://en.wikipedia.org / wiki / Late_binding [ ^ ],

http:// en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 [ ^ ]。



你需要的不仅仅是上面提到的一般解释,你需要在.NET和C#代码示例上学习它你需要自己开发和学习;这是掌握概念的唯一方法。



即使在无关的问题中,你真的需要使用开关语句并列出一些选项,永远不要使用1,2,3 ......选项。谁知道他们应该是什么意思?这是不可支持的。至少为此定义一些枚举类型。



-SA

I am new to C#, and I am completely lost. This is my homework but I can’t figure out how to deal with the missing script/ parts of the following interface:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calculate_Area {
    class Program {


        /// <summary>
        ///
        /// </summary>
        /// <param name="option"></param>
        static void CalculateArea(int option) {

            if (option == 1) {
                CircleArea();
            } else if (option == 2) {
                RectangleArea();
            } else {
                CylinderArea();

            }//end if else


        }//end CalculateArea



        /// <summary>
        /// Inputs user choice
        /// </summary>
        /// <returns> 1,2, 3 or 0</returns>
        static int ReadMenuOption() {

            int option = 0;
            bool validMenuOption = false;

            do {
                option = int.Parse(Console.ReadLine());

                if ((option >= 0) && (option <= 3)) {
                    validMenuOption = true;
                } else {
                    Console.WriteLine("\n\t Option must be 1, 2, 3 or 0");
                    DisplayMenu();
                } //end if

            } while (!validMenuOption);

            return option;
        } //end ReadMenuOption




        /// <summary>
        /// Display shape menu
        /// </summary>
        static void DisplayMenu() {
            string menu = "\n 1) Circle"
                        + "\n 2) Rectangle"
                        + "\n 3) Cylinder (closed)"
                        + "\n\nEnter your choice(1, 2, 3 or 0 to exit):";

            Console.Write(menu);
        } //end DisplayMenu



        static void Main() {
            int menuOption;

            DisplayMenu();
            menuOption = ReadMenuOption();

            if (menuOption != 0) {
                CalculateArea(menuOption);
            }// end if


            Console.ReadKey();
        }
    }
}

解决方案

All your options 1, 2, 3 is a big programming abuse, and having "options" and the switch statement for a separate types of shapes is an absolute OOP abuse; but the general "1, 2, 3 abuse" is much worse. All you did is wrong from the very beginning.

I'm afraid you are not yet prepared to hear the answer, but I'll give you main points.

You need to use OOP late binding and polymorphism. First, you need to have some base class with abstract (hence, virtual) methods, such as GalculateArea(). This method should not have arguments, because implicit "this" argument will always give you the reference to the object for these instance methods. Override them in each derived class, such as Rectangle, Ellipse and so on. Your runtime types are always non-abstract, but you can have some variable/member of the compile-time type of the base class type. If you call these method on some instance, the call will always dynamically dispatch to the method of the (derived) runtime type. So, you only need one call for all types. This mechanism lies at the very heart of OOP.

Based on this, you can have polymorphic set of objects of different runtime types represented by the collection with compile type element type of the base class type. The similar mechanism can be based on interfaces.

You really need to learn it all from scratch.

See also:
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Virtual_function[^],
http://en.wikipedia.org/wiki/Virtual_method_table[^],
http://en.wikipedia.org/wiki/Late_binding[^],
http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^].

What you need is not just the general explanations referenced above, you need to learn it on .NET and C# code sample you need to develop and study on your own; this is the only way to grasp the conceptions.

Even, in unrelated problems, you really need to use switch statements and list some options, never use such thing as 1, 2, 3... options. Who knows what they are supposed to mean? This is not supportable. At least define some enum type for that.

—SA


这篇关于我该如何解决这个C#难题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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