码迷,mamicode.com
首页 > Windows程序 > 详细

C#类(二):继承和多态(EduCoder实训题目)

时间:2020-06-15 22:58:40      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:span   caller   rgs   tor   d3d   turn   c11   圆形   ipa   

第1关:继承

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace K1
{
    public abstract class Shape
    {
        public abstract int Area(); //获取形状面积
    }
    /********** Begin *********/
    class Square : Shape {    
        int side = 0;
        public Square(int _side) {
            side = _side;
        }
        public override int Area(){
            return side * side;            
        }
    }
    /********** End *********/

    public class myCaller
    {
        public static void Main(string[] args)
        {
            for (int i = 0; i < 4; i++)
            {
                string side = Console.ReadLine();
                Square s = new Square(Convert.ToInt32(side));
                Console.WriteLine("Square Area:" + s.Area());
            }
        }
    }
}
View Code

第2关:多态

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace K2
{
    public abstract class Shape
    {
        public abstract int Area(); //获取形状面积
    }

    /********** Begin *********/
    //圆形类和正方形类
     class Circular : Shape {    
        int radius = 0;
        public Circular(int _radius) {
            radius = _radius;
        }
        public override int Area(){
            const int PI = 3;
            return PI * radius * radius;            
        }
    }

     class Square : Shape {    
        int side = 0;
        public Square(int _side) {
            side = _side;
        }
        public override int Area(){
            return side * side;            
        }
    }
    /********** End *********/

    public class myCaller
    {
        public static void Main(string[] args)
        {
            Circular c = new Circular(4);
            Square s = new Square(4);

            Shape shape1 = s;
            Shape shape2 = c;

            Console.WriteLine("Square Area:" + shape1.Area());
            Console.WriteLine("Circular Area:" + shape2.Area());

        }
    }
}
View Code

第3关:运算符的重载

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace K3
{
    public class Community
    {
        private int number; //人数

        public Community(int number)
        {
            this.number = number;
        }

        /********** Begin *********/
        //重载+运算符
        public static int operator +(Community a, Community b)
        {
            return a.number+b.number;
        }
        /********** End *********/
    }
    public class myCaller
    {
        public static void Main(string[] args)
        {
            Community violin = new Community(50);   //小提琴社
            Community cello = new Community(27);    //大提琴社

            int total = violin + cello;
            Console.WriteLine("The number of participants in this event is:" + total);
        }
    }
}
View Code

第4关:interface接口

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace K4
{
    /********** Begin *********/
    //定义接口IColor和IShape
    interface IColor
    {
        
    }
    interface IShape
    {
 
    }
    /********** End *********/

    class myBox : IColor, IShape
    {
        private string color;
        private string shape;
        public void showBox()
        {
            Console.WriteLine("box color:" + color);
            Console.WriteLine("box shape:" + shape);
        }
        /********** Begin *********/
        //实现接口函数
        internal void setColor(string v)
        {
            this.color=v;
        }
        internal void setShape(string v)
        {
            this.shape = v;
        }
        /********** End *********/
    }

    public class myCaller
    {
        public static void Main(string[] args)
        {
            myBox box = new myBox();
            box.setColor("Grey");   //调用实现的接口函数
            box.setShape("Cube");
            box.showBox();
        }
    }
}
View Code

我要骂人了,我怎么这么蠢哦!

C#类(二):继承和多态(EduCoder实训题目)

标签:span   caller   rgs   tor   d3d   turn   c11   圆形   ipa   

原文地址:https://www.cnblogs.com/QQ1723545340/p/13138100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!