.net作业8

题目一:创建C#控制台应用程序,建立一个点类CzPoint,为其定义两个double类型的私有字段成员x和y,分别表示点的横坐标和纵坐标;对CzPoint类进行相等和不等操作符重载。两个坐标点相等,则指它们的横坐标和纵坐标都相等。

using System;

namespace zuoye81
{
    class Program
    {
        static void Main(string[] args)
        {
            CzPoint point1 = new CzPoint(1, 2);
            CzPoint point2 = new CzPoint(3, 3);
            if (point1 != point2)
                Console.WriteLine("坐标不相等");
            if (point1 == point2)
                Console.WriteLine("坐标相等");
            Console.ReadKey();
        }
    }
    public class CzPoint
    {
        private double x;
        private double y;
        public double X
        {
            get { return x; }
            set { x = value; }
        }
        public double Y
        {
            get { return y; }
            set { y = value; }
        }
        public CzPoint(double x,double y)
        {
            this.x = x;
            this.y = y;
        }
        public static Boolean operator ==(CzPoint point1, CzPoint point2)
        {
            return (point1.x.Equals(point2.x) && point1.y.Equals(point2.y)) ? true : false;
        }
        public static Boolean operator !=(CzPoint point1, CzPoint point2)
        {
            return (!point1.x.Equals(point2.x) ||! point1.y.Equals(point2.y)) ? true : false;
        }
    }
}

 题目二:定义一个水果的基类Fruit,并在基类中定义多个方法,来实现水果的不同特性,如颜色clour( ),形状shape( ),口味taste( )等;声明派生类Apple并重写clour( )、shape( )方法。最后在Main( )中调用上述方法。

using System;

namespace zuoye82
{
    
    public abstract class Fruit
    {
        //获得水果颜色
        public virtual void GetColor()
        {

        }
        //获得水果形状
        public virtual void GetShape()
        {

        }
        //获得水果口味
        public virtual void GetTaste()
        {

        }
        class Program
    {
        static void Main(string[] args)
        {
                Apple apple = new Apple();
                apple.GetColor();
                apple.GetShape();
                apple.GetTaste();
            }
    }
        //苹果
        public class Apple : Fruit
        {
            public Apple()
            {

            }
            public override void GetColor()
            {
                Console.WriteLine("苹果是红色的");
                base.GetColor();
            }
            public override void GetShape()
            {
                Console.WriteLine("苹果是球形的");
                base.GetShape();
            }
            public override void GetTaste()
            {
                Console.WriteLine("苹果是甜的");
                base.GetTaste();
            }
        }
    }
}

题目三:编写程序,定义一个类,该类包含一个方法,方法的功能是使用“out型参数”传递数据,找出这组数据的最大数和最小数,方法的原型参考下面,最后在Main( )中调用该方法。

using System;

namespace zuoye83
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入");
            int[] number = new int[5];
            int m, n;
            for (int i = 0; i < number.Length; i++)
            {
                number[i] = int.Parse(Console.ReadLine());
            }
            Finder fd = new Finder(number);

            int max, min;
            fd.GetMs(out min, out max);
            Console.WriteLine("min={0} max={1}", min, max);

            Console.ReadLine();
        }
    }

    class Finder
    {
        private int[] nums;
        public Finder(int[] nums)
        {
            this.nums = nums;
        }

        public void GetMs(out int min, out int max)
        {
            int A = nums[0];
            int B = nums[0];
            for (int i = 1; i < nums.Length; i++)
            {
                if (nums[i] > A) A = nums[i];
                if (nums[i] < B) B = nums[i];

            }
            max = A;
            min = B;
        }
    }
}

题目四: 试编程,实现两个三维矩阵(3×3)的加法及减法运算。要求:采用运算符重载

using System;

namespace zuoye84
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] a1 = new int[3, 3] { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } };
            int[,] a2 = new int[3, 3] { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
            arrayList b1 = new arrayList(a1);
            arrayList b2 = new arrayList(a2);
            Console.Write("b1:");
            b1.display();
            Console.WriteLine();
            Console.Write("b2:");
            b2.display();
            arrayList b3 = b1 + b2;
            Console.WriteLine();
            Console.WriteLine("b1+b2:");
            b3.display();
            arrayList b4 = b1 - b2;
            Console.WriteLine();
            Console.WriteLine("b1-b2:");
            b4.display();

            Console.ReadKey();
        }
    }
    class arrayList
    {
        private int[,] arrnum;
        public arrayList(int[,] arrnum)
        {
            this.arrnum = arrnum;
        }
        public static arrayList operator +(arrayList a1, arrayList a2)
        {
            int[,] a3 = new int[3, 3];
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    a3[i, j] = a1.arrnum[i, j] + a2.arrnum[i, j];
                }
            }
            arrayList a4 = new arrayList(a3);
            return a4;
        }
        public static arrayList operator -(arrayList a1, arrayList a2)
        {
            int[,] a3 = new int[3, 3];
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    a3[i, j] = a1.arrnum[i, j] - a2.arrnum[i, j];
                }
            }
            arrayList a4 = new arrayList(a3);
            return a4;
        }
        public void display()
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write("{0} ", arrnum[i, j]);
                }
                Console.WriteLine();
            }

        }

    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值