A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 赵贺景 中级黑马   /  2014-5-10 12:37  /  982 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 赵贺景 于 2014-5-10 16:37 编辑

使用抽象类来实现,计算圆形和矩形的面积和周长。要求:写一个”Shape”类作为圆形和矩形的父类,提供两个抽象方法,分别用来计算周长和面积,子类圆形和矩形分别在自己的类中重写父类的抽象方法

这个问题是不是有两种思路,一种就是抽象类 另外一个可以用接口实现,抽象类的我能理解 但是接口的实现方法 表示卡壳了。求指导

4 个回复

倒序浏览
题目不是说用抽象类来实现吗?抽象类跟接口是不一样的,接口比抽象类更加抽象化。如果使用接口实现的话,最好让接口的功能单一,比如计算面积的接口如下:
public interface ICalcArea(){
    void CalcArea();
}
然后让一个类实现这个接口计算面积的方法,最后通过接口变量来调用这个方法就可以计算面积了。
回复 使用道具 举报
本帖最后由 一片白 于 2014-5-10 14:24 编辑

一、抽象类实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _27
{   
    class Program
    {
        static void Main(string[] args)
        {
            //圆形
            Shape shape1 = new Circle(3.0);
            Console.WriteLine("面积是:" + shape1.CalculateArea());
            Console.WriteLine("周长是:" + shape1.CalculatePerimeter());
            //矩形
            Shape shape2 = new Retangle(4, 7.3);
            Console.WriteLine("面积是:" + shape2.CalculateArea());
            Console.WriteLine("周长是:" + shape2.CalculatePerimeter());
            Console.ReadKey();
        }
    }

    //抽象类 Shape
    abstract class Shape
    {
        public double Area { get; set; }   //面积属性
        public double Perimeter { get; set; }  //周长

        public abstract double CalculateArea();//计算面积的方法
        public abstract double CalculatePerimeter();//计算周长
    }

    //圆形
    class Circle : Shape
    {
        //属性:半径
        public double r { get; set; }
        //构造函数
        public Circle(double r)
        {
            this.r = r;
        }
        //重写 计算面积的方法
        public override double CalculateArea()
        {
            this.Area = Math.PI * r * r;
            return this.Area;
        }

        public override double CalculatePerimeter()
        {
            this.Perimeter = 2 * Math.PI * r;
            return this.Perimeter;
        }
    }
    //矩形
    class Retangle : Shape
    {
        public double height { get; set; }
        public double width { get; set; }

        //构造函数
        public Retangle(double height, double width)
        {
            this.height = height;
            this.width = width;
        }

        public override double CalculateArea()
        {
            this.Area = height * width;
            return this.Area;
        }

        public override double CalculatePerimeter()
        {
            this.Perimeter = (height + width) * 2;
            return this.Perimeter;
        }
    }

}

二、接口实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _27_2
{
    class Program
    {
        static void Main(string[] args)
        {
            ICalculateArea area1 = new Circle(3);
            ICalculateArea area2 = new Retangle(2, 3.33);
            Console.WriteLine("area1的面积是" + area1.CalculateArea());
            Console.WriteLine("area2的面积是" + area2.CalculateArea());

            ICalculatePerimeter p1 = new Circle(3);
            ICalculatePerimeter p2 = new Retangle(2, 3.33);
            Console.WriteLine("area1的周长是" + p1.CalculatePerimeter());
            Console.WriteLine("area2的周长是" + p2.CalculatePerimeter());

            Console.ReadKey();
        }
    }

    //接口 面积
    interface ICalculateArea
    {
        double CalculateArea();
    }
    //接口 周长
    interface ICalculatePerimeter
    {
        double CalculatePerimeter();
    }

    //圆形
    class Circle : ICalculateArea, ICalculatePerimeter
    {
        //属性:半径
        public double r { get; set; }
        //构造函数
        public Circle(double r)
        {
            this.r = r;
        }

        public double CalculateArea()
        {
            return Math.PI * r * r;
        }

        public double CalculatePerimeter()
        {
            return 2 * Math.PI * r;
        }
    }
    //矩形
    class Retangle : ICalculateArea, ICalculatePerimeter
    {
        public double height { get; set; }
        public double width { get; set; }
        //构造函数
        public Retangle(double height, double width)
        {
            this.height = height;
            this.width = width;
        }

        public double CalculateArea()
        {
            return height * width;
        }

        public double CalculatePerimeter()
        {
            return (height + width) * 2;
        }
    }
}


{:3_51:}自己的见解,也不知道是否完全正确。

黑马朋友,共同学习!

回复 使用道具 举报
  1. class Shape
  2.     {
  3.     }
  4.     class Hexagon : Shape, IPointy
  5.     {
  6.         public int Points
  7.         {
  8.             get { return 6; }
  9.         }
  10.     }
  11.     class Circle : Shape
  12.     {

  13.     }
  14.     class Triangle : Shape, IPointy
  15.     {
  16.         public int Points
  17.         {
  18.             get { return 3; }
  19.         }
  20.     }
复制代码
  1. public interface IPointy
  2.     {
  3.         int Points { get; }
  4.     }
复制代码

感觉有点像《精通C#》的题目,你是不是想要这种效果。
回复 使用道具 举报
这个题目考查的是父类中的抽象方法不能被直接调用 子类要重写才能调用
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马