首页 > 技术文章 > c#基类继承

mercuialC 2017-02-13 21:00 原文

【 塔 · 第 三 条 约 定 】

编写一个多边形作为基类(成员:定点数)抽象方法(子类实现):体积、边长

  1. 正三角形类:成员 边长
  2. 长方形类:成员 长宽
using System;
using System.Collections.Generic;
using System.Text;

namespace 第三条约定
{
    abstract class Base
    {
        public static int point;//定点数
        public static double area;//面积
        
    }
    class regular_triangle : Base//定义正三角形类
    {
        public void input()
        {
            Console.WriteLine("请输入正三角形定点数:");
            
            point = Convert.ToInt32(Console.ReadLine());//获取定点数
            if (point != 3)
            {
                Console.WriteLine("这不是一个三角形");
            }
         
        }
        int length;//私有成员 边长
        public void _regular_triangle()
        {
            Console.WriteLine("请输入正三角形的边长");
            length = Convert.ToInt32(Console.ReadLine()); ;//获取边长
            area = 0.433 * (length * length);
        }
        public void output()
        {
            Console.WriteLine("边长:{0}", length);//输出私有成员边长
            Console.WriteLine("正三角形的面积是:{0}", area);
        }

    }
    class orthogon : Base//定义矩形类
    {


        int length;//私有成员 长
        int width;//私有成员 宽
        public void input()
        {
            Console.WriteLine("请输入矩形的定点数:");

            point = Convert.ToInt32(Console.ReadLine());//获取定点数
            if (point != 4)
            {
                Console.WriteLine("这不是一个矩形");
            }
         
        }
        public void _orthogon()
        {

            Console.WriteLine("请输入矩形的长与宽");
            length = Convert.ToInt32(Console.ReadLine());//获取长
            width = Convert.ToInt32(Console.ReadLine());//获取宽
            area = (length * width);

        }
        public void output()
        {
            Console.WriteLine("长:{0},宽:{1}", length, width);//输出私有成员长与宽
            Console.WriteLine("矩形的面积是:{0}",area);
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        第三条约定.regular_triangle a = new 第三条约定.regular_triangle();
             a.input();
             a._regular_triangle();
            a.output();//输出形状与面积
        第三条约定.orthogon b = new 第三条约定.orthogon();
             b.input();
             b._orthogon();
            b.output();//输出形状与面积
        Console.ReadKey();
    }
    
    public static int point { get; set; }
}

遇到的问题

  • 在调试的时候程序没有Console.ReadKey();导致调试时没有等待输入,窗口闪退的情况
  • 还有题目理解不明,继承还是有点不明白。

推荐阅读