zoukankan      html  css  js  c++  java
  • csharp 面向对象编程

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Shape
    {
        /**
         * 抽象形状类
         */
        public abstract class Shape
        {
            private int edge;
            //构造函数 
            public Shape(int edge)
            {
                this.edge = edge;
            }
            //抽象类实现的方法,子类可以重用
            public int GetEdge()
            {
                return this.edge;
            }
            //抽象方法,子类必须重写,并在声明上加上override
            public abstract int CalcArea();
        }
    
        /**
         * 三角形类,继承自形状类
         */
        public class Triangle : Shape
        {
            private int bottom;
            private int height;
            //构造函数,构造的同时调用父类指定构造器
            public Triangle(int bottom, int height)
                : base(3)
            {
                this.bottom = bottom;
                this.height = height;
            }
            //重写同名方法
            public override int CalcArea()
            {
                return this.bottom * this.height / 2;
            }
        }
    
        /**
         * 矩形类,继承自形状类
         */
        public class Rectangle : Shape
        {
            private int bottom;
            private int height;
            //构造函数,构造的同时调用父类指定构造器
            public Rectangle(int bottom, int height)
                : base(4)
            {
                this.bottom = bottom;
                this.height = height;
            }
            //重写同名方法
            public override int CalcArea()
            {
                return this.bottom * this.height;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                //测试,可用父类映射子类
                Shape triangle = new Triangle(4, 5);
                Console.WriteLine(triangle.GetEdge());
                Console.WriteLine(triangle.CalcArea());
    
                Shape rectangle = new Rectangle(4, 5);
                Console.WriteLine(rectangle.GetEdge());
                Console.WriteLine(rectangle.CalcArea());
            }
        }
    }
  • 相关阅读:
    Java DB 访问(三)mybatis mapper interface接口
    android stdio 安装后gradle 编译失败,设置代理不管用
    超星自动签到并邮件通知
    关于解决Mac使用docker安装SQL server for Linux 中文乱码问题
    Bom简单介绍
    grid-layout
    Position
    前端学习(注册表)
    关于学精通一门语言的个人看法(欢迎留言补充)
    Google 最新推出数据集搜索
  • 原文地址:https://www.cnblogs.com/zfc2201/p/3561106.html
Copyright © 2011-2022 走看看