A
A
Adjist2017-05-11 09:48:13
OOP
Adjist, 2017-05-11 09:48:13

Inheritance and/or code reduction - C#?

namespace ConsoleApplication10
{
 
 
    class Pryamoyg
    {
        private double x1;
        private double y1;
        private double x2;
        private double y2;
        private double x3;
        private double y3;
        private double x4;
        private double y4;
 
  
 
        private bool Proverka()
        {
            double k1, k2, g, k3, k4;
            bool p = false;
 
 
            k1 = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
            k2 = Math.Sqrt(((x3 - x2) * (x3 - x2)) + ((y3 - y2) * (y3 - y2)));
            k3 = Math.Sqrt(((x4 - x3) * (x4 - x3)) + ((y4 - y3) * (y4 - y3)));
            k4 = Math.Sqrt(((x1 - x4) * (x1 - x4)) + ((y1 - y4) * (y1 - y4)));
            g = Math.Sqrt(((x3 - x1) * (x3 - x1)) + ((y3 - y1) * (y3 - y1)));
            if ((g == Math.Sqrt(k1 * k1 + k2 * k2)) & (g == Math.Sqrt(k3 * k3 + k4 * k4)))
            {
 
                p = true;
            }
            return p;
 
        }
 
        public void Ploshad()
        {
            if (Proverka() == true)
            {
 
 
                double r1, r2, S;
                r1 = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
                r2 = Math.Sqrt(((x3 - x2) * (x3 - x2)) + ((y3 - y2) * (y3 - y2)));
                S = r1 * r2;
                Console.WriteLine("Площадь: {0}", S);
            }
            else
            {
                Console.WriteLine("eto ne pryamoyg");
            }
        }
 
        public void Perim()
        {
 
            if (Proverka() == true)
            {
                double r1, r2, P;
 
                r1 = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
                r2 = Math.Sqrt(((x3 - x2) * (x3 - x2)) + ((y3 - y2) * (y3 - y2)));
                P = (r1 + r2) * 2;
                Console.WriteLine("Периметр: {0}", P);
            }
            else
            {
                Console.WriteLine("eto ne pryamoyg");
            }
        }
 
        class Kvadrat
        {
            private double x1;
            private double y1;
            private double x2;
            private double y2;
            private double x3;
            private double y3;
            private double x4;
            private double y4;
 
            public Kvadrat(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
            {
                this.x1 = x1;
                this.x4 = x4;
                this.x2 = x2;
                this.x3 = x3;
                this.y1 = y1;
                this.y2 = y2;
                this.y3 = y3;
                this.y4 = y4;
            }
 
            private bool Proverka()
            {
                double k1, k2, g, k3, k4;
                bool p = false;
                k1 = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
                k2 = Math.Sqrt(((x3 - x2) * (x3 - x2)) + ((y3 - y2) * (y3 - y2)));
                k3 = Math.Sqrt(((x4 - x3) * (x4 - x3)) + ((y4 - y3) * (y4 - y3)));
                g = Math.Sqrt(((x3 - x1) * (x3 - x1)) + ((y3 - y1) * (y3 - y1)));
                k4 = Math.Sqrt(((x1 - x4) * (x1 - x4)) + ((y1 - y4) * (y1 - y4)));
                if ((g == Math.Sqrt(k1 * k1 + k2 * k2)) & (k1 == k2) & (k1 == k3) & (k1 == k4))
                {
 
                    p = true;
                }
                return p;
            }
 
 
 
 
 
 
            public void Ploshad()
            {
 
                if (Proverka() == true)
                {
 
 
                    double r1, r2, S;
                    r1 = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
                    r2 = Math.Sqrt(((x3 - x2) * (x3 - x2)) + ((y3 - y2) * (y3 - y2)));
                    S = r1 * r2;
                    Console.WriteLine("Площадь: {0}", S);
                }
                else
                {
                    Console.WriteLine("eto ne kvadr");
                }
            }
 
            public void Perim()
            {
 
                if (Proverka() == true)
                {
 
                    double r1, r2, P;
 
                    r1 = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
                    r2 = Math.Sqrt(((x3 - x2) * (x3 - x2)) + ((y3 - y2) * (y3 - y2)));
                    P = (r1 + r2) * 2;
                    Console.WriteLine("Периметр: {0}", P);
                }
                else
                {
                    Console.WriteLine("eto ne kvadr");
                }
            }
 
            class Romb
            {
                private double x1;
                private double y1;
                private double x2;
                private double y2;
                private double x3;
                private double y3;
                private double x4;
                private double y4;
                public Romb(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
                {
                    this.x1 = x1;
                    this.x4 = x4;
                    this.x2 = x2;
                    this.x3 = x3;
                    this.y1 = y1;
                    this.y2 = y2;
                    this.y3 = y3;
                    this.y4 = y4;
                }
 
                private bool Proverka()
                {
                    double k1, k2, g, k3, k4;
                    bool p = false;
                    k1 = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
                    k2 = Math.Sqrt(((x3 - x2) * (x3 - x2)) + ((y3 - y2) * (y3 - y2)));
                    k3 = Math.Sqrt(((x4 - x3) * (x4 - x3)) + ((y4 - y3) * (y4 - y3)));
                    g = Math.Sqrt(((x3 - x1) * (x3 - x1)) + ((y3 - y1) * (y3 - y1)));
                    k4 = Math.Sqrt(((x1 - x4) * (x1 - x4)) + ((y1 - y4) * (y1 - y4)));
                    if ((k1 == k2) & (k1 == k3) & (k1 == k4))
                    {
 
                        p = true;
                    }
                    return p;
                }
 
                public void Ploshad()
                {
 
                    if (Proverka() == true)
                    {
                        double r1, r2, S;
                        r1 = Math.Sqrt(((x3 - x1) * (x3 - x1)) + ((y3 - y1) * (y3 - y1)));
                        r2 = Math.Sqrt(((x4 - x2) * (x4 - x2)) + ((y4 - y2) * (y4 - y2)));
                        S = (r1 * r2) / 2;
                        Console.WriteLine("Площадь: {0}", S);
                    }
                    else
                    {
                        Console.WriteLine("eto ne romb");
                    }
                }
 
                public void Perim()
                {
 
                    if (Proverka() == true)
                    {
                        double r1, P;
 
                        r1 = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
                        P = r1 * 4;
                        Console.WriteLine("Периметр: {0}", P);
                    }
                    else
                    {
                        Console.WriteLine("eto ne romb");
                    }
                }
            }
 
            class Trapecia
            {
                public double x1;
                public double y1;
                public double x2;
                public double y2;
                public double x3;
                public double y3;
                public double x4;
                public double y4;
 
                public void Ploshad()
                {
                    double a1, h1, S1, a2, h2, S2, a3, h3, S3, a4, h4, S4, S;
                    a1 = x2 - x1;
                    h1 = Math.Abs(y1 + (y2 - y1) / 2);
                    S1 = a1 * h1;
                    a2 = x3 - x2;
                    h2 = Math.Abs(y2 + (y3 - y2) / 2);
                    S2 = a2 * h2;
                    a3 = x4 - x3;
                    h3 = Math.Abs(y3 + (y4 - y3) / 2);
                    S3 = a3 * h3;
                    a4 = x1 - x4;
                    h4 = Math.Abs(y4 + (y1 - y4) / 2);
                    S4 = a4 * h4;
                    S = S1 + S2 + S3 + S4;
                    Console.WriteLine("Площадь: {0}", S);
                }
 
 
 
            }
 
            class Paral
            {
                public double x1;
                public double y1;
                public double x2;
                public double y2;
                public double x3;
                public double y3;
                public double x4;
                public double y4;
 
             
 
            }
 
            class Krug
            {
                public double r;
                public const double Pi = Math.PI;
 
                public void Ploshad()
                {
                    double S;
                    S = Pi * (r * r);
                    Console.WriteLine("Площадь : {0}", S);
                }
 
                public void DlinaO()
                {
                    double L;
                    L = 2 * Pi * r;
                    Console.WriteLine("Длина окружностти : {0}", L);
                }
 
            }

This is not a request to do the work for me, but simply to show how to reduce the code. The program, we enter the coordinates and we get the answer as a result, but it is huge, while I read the topic of inheritance, but I don’t fully understand how to reduce the amount of code? At least the part with x/y variables?
*part of the program has been removed due to a 10,000-character limit

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Eremin, 2017-05-11
@Adjist

In Troelson's tutorial, using the example of figures, the principle of inheritance is considered.
Make a figura class (since there is no way in English) and create members there

protected double x1;
protected double y1;
protected double x2;
protected double y2;
protected double x3;
protected double y3;
protected double x4;
protected double y4;

Inherit all your circles and squares from it. You can also define abstract methods Ploshad () and Perim ()
in figura
.
So it's more correct. And you can, for example, work with a collection of shapes. for example
List<figura> figuri = new List<figura>();
figuri.Add(new krug());
figuri.Add(new romb());
figuri.Add(new kvadrat());

foreach(var fig in figuri)
{
Console.WriteLine(fig.ploshad());
}

Not to say that the code will become much less. Although you do not have to declare this footcloth from x1, x2 ... y4 in each class.
It will be more convenient to work with a family of classes - this is a very useful change

A
Anvar Shakhmaev, 2017-05-11
@RxR

It is more important to learn how to name variables and objects correctly: Krug - Disk, Ploshad - Area. It is important.
On the example of the Krug class (other figures by analogy).
- A polymorphic type is created. In this example, the advantage is not noticeable, but in real projects there may be many common methods that can be exactly repeated in descendant classes.
-public const double Pi = Math.PI; - is thrown out, because creates redundancy
- instead of S = Pi * (r * r); you need to use the ready-made Math.Pow method - it's
better to use assignment initialization double area = Math.PI * Math.Pow(Radius, 2); where possible,
methods should return a value without Console.WriteLine("Shape");, so this example can be used in a WindowsForms application, for example
- you can add a constructor with a parameter to initialize the Radius property

class Shape
{
    //общие для фигур методы
    public virtual string GetShapeName()
    {
        return "Shape";
    }
}

class Disk : Shape
{
    public double Radius { get; set; }
    
    public double Area()
    {
        double area = Math.PI * Math.Pow(Radius, 2);
        return area;
    }
 
    public double Length()
    {
        double length = 2 * Math.PI * Radius;
        return length;
    }

    public override string GetShapeName()
    {
        return "Disk";
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question