c#基類繼承


【 塔 · 第 三 條 約 定 】

編寫一個多邊形作為基類(成員:定點數)抽象方法(子類實現):體積、邊長

  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();導致調試時沒有等待輸入,窗口閃退的情況
  • 還有題目理解不明,繼承還是有點不明白。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM