简体   繁体   English

永远不会分配c#字段,并且其默认值始终为null?

[英]c# field is never assigned to and will always have its default value null?

C# Inhertance. C#Inhertance。 I's trying to inherit class quadrilateral to class sqaure. 我试图将类四边形继承到类sqaure。 I am unable to figure this out and I got the following error and I dont know what it means. 我无法弄清楚这一点,我得到了以下错误,我不知道这意味着什么。 Field square.p1 is never assigned to and will always have its default value null? 字段square.p1永远不会被赋值并且将始终具有其默认值null?

    using System;

    class testquadrilaterals 
    {
        static void Main() 
{
    quadrilateral obj1 = new quadrilateral(1.1, 1.2, 6.6, 2.8, 6.2, 9.9, 2.2, 7.4);
    obj1.printcoordinates();
    Console.WriteLine("\n");
    square obj2 = new square( 4.0, 0.0, 8.0, 0.0, 8.0, 4.0, 4.0, 4.0 );
    obj2.printsquare();

    Console.ReadLine();
}
    }

    class point 
    {
        private double x;
private double y;

public point(double x_coord, double y_coord)
{
    x = x_coord;
    y = y_coord;
}

public double X { get { return x; } set { x = value; } }
public double Y { get { return y; } set { y = value; } }

public override string ToString()
{
    return ("(" + X + ", " + Y + ")");
}
    }

    class quadrilateral
    {
point p1, p2, p3, p4;

public quadrilateral(double x1, double y1, double x2, double y2,
    double x3, double y3, double x4, double y4)
{
    p1 = new point(x1, y1);
    p2 = new point(x2, y2);
    p3 = new point(x3, y3);
    p4 = new point(x4, y4);
}

public point P1 { get { return p1; } set { p1 = value; } }
public point P2 { get { return p2; } set { p2 = value; } }
public point P3 { get { return p3; } set { p3 = value; } }
public point P4 { get { return p4; } set { p4 = value; } }

public void printcoordinates()
{
    Console.WriteLine("Coordinates of Quadrilateral: \n" + p1.ToString() + " " + p2.ToString() + " " + p3.ToString() + " " + p4.ToString() + " ");
}
    }


    class square : quadrilateral
    {
point p1, p2, p3, p4;

public square(double x1, double y1, double x2, double y2,
    double x3, double y3, double x4, double y4) : base (x1, y1, x2, y2, x3, y3, x4, y4)
{ }

public void printsquare()
{
    Console.WriteLine("Coordinates of Square: \n" + p1.ToString() + " " + p2.ToString() + " " + p3.ToString() + " " + p4.ToString() + " ");
    Console.WriteLine("Area: " + area());
   // Console.WriteLine("Sides: " + sides());
}

public double area() 
{

    return Math.Abs(p1.X - p2.X) * Math.Abs(p1.Y - p3.Y); 
}
// public double sides() { }

} }

In this case the class square defines 4 fields which it never sets: p1, p2, p3 and p4 在这种情况下,类square定义了4个从未设置的字段:p1,p2,p3和p4

class square : quadrilateral {
  point p1, p2, p3, p4;

  ...
}

These fields are private and never set hence they are essentially useless as is. 这些字段是私有的,从不设置,因此它们本质上是无用的。 The C# compiler is alerting you to this because this is usually an indication of a bug. C#编译器会提醒您,因为这通常表示存在错误。

In this case it looks like you may have intended for square to simply use the values in quadrilateral . 在这种情况下,看起来您可能已经打算使用square来简单地使用quadrilateral的值。 The fields have the same name and type and there is an inheritance relationship between the two types. 这些字段具有相同的名称和类型,并且这两种类型之间存在继承关系。 If that's the case you should delete the fields in square and change the accessibility to protected in quadrilateral 如果是这种情况,您应该删除square的字段,并在quadrilateral中将可访问性更改为protected

class quadrilateral {
  protected  point p1, p2, p3, p4;
  ...
}

This will allow square to access the values 这将允许square访问值

如果从四面体继承,则不需要在square中再次定义变量p1 / p2 / p3 / p4,您将从基类继承它们。

In Square::area(), the p1s and p2s are referring to those defined within the square class, not those in the quadrilateral class. 在Square :: area()中,p1s和p2s指的是方形类中定义的那些,而不是四边形类中的那些。 You have both square::p1, square::p2, etc. and quadrilateral ::p1, quadrilateral::p2, etc. The square p1s and p2s are never assigned to. 你有square :: p1,square :: p2等和quadrilateral :: p1,quadrilateral :: p2等。方形p1s和p2s永远不会分配给。 To have your square class use the quadrilateral class' p1s and p2s, don't redefine them in square. 要让你的方形类使用四边形类'p1s和p2s,不要在square中重新定义它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:永远不会将字段分配给,并且始终将其默认值设置为null - Warning: Field is never assigned to, and will always have its default value null 字段'xxx'永远不会分配给,并且将始终具有其默认值null - Field 'xxx' is never assigned to, and will always have its default value null 永远不会将字段xxx分配给,并始终将其默认值设置为null - Field xxx is never assigned to, and will always have its default value null 字段…永远不会分配给它,并且其默认值始终为null - Field … is never assigned to, and will always have its default value null 该字段永远不会分配给它,并且其默认值始终为null - The Field Is Never Assigned To And Will Always Have Its Default Value null 字段从未分配给它,并且将始终具有其默认值null - Field is never assigned to, and will always have its default value null C#不能声明Int的公共double数组:Field永远不会被赋值,并且它的默认值始终为null - C# can't declare public double array of Int: Field is never assigned to, and will always have its default value null c#中给出警告的结构字段永远不会分配给,并且始终具有默认值0 - Struct in c# giving warning Field is never assigned to,and will always have its default value0 字段“ FIELD”从未分配,并且将始终具有其默认值 - field 'FIELD' is never assigned to, and will always have its default value 私有类中的公共字段:“Field [FieldName]永远不会分配给,并且将始终具有其默认值null” - Public field in private class: “Field [FieldName] is never assigned to, and will always have its default value null”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM