I'm a C# beginner (3-day experience). Today I'm practising the inheritance in C#. Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Geometry
{
public class Geometry
{
public double perimeter;
public double area;
public bool special;
}
public class Rect : Geometry
{
public void Rectangle( double length, double width )
{
perimeter = (length + width) * 2;
area = length * width;
special = (length == width) ? true : false;
Console.WriteLine("Rectangle information: ");
Console.WriteLine("Perimeter: " + perimeter);
Console.WriteLine("Area: " + area);
if (special) Console.WriteLine("This is a special rectangle. In fact, this is a square!");
Console.WriteLine();
}
}
public class Cir : Geometry
{
public void Circle(double radius)
{
perimeter = radius * 2 * Math.PI;
area = radius * radius * Math.PI;
Console.WriteLine("Perimeter: " + perimeter);
Console.WriteLine("Area: " + area);
Console.WriteLine();
}
}
public class Main
{
public static void main(String[] args)
{
Rectangle a = new Rectangle(23.00, 21.4);
Circle b = new Circle(5.8);
}
}
The compiler said that "The type or namespace of 'Rectangle' could not be found", same as Circle. What's wrong? I don't really understand although I tried many ways to fix them. How can I find and understand the errors?
Read the error. You never wrote a class named Rectangle
. Rect
is not the same thing.
You have 2 errors.
Error #1 is that your constructor name and your class name are not the same
Error #2 is that the constructor cannot have a return type (void)
public class Rectangle
{
public Rectangle(double length, double width)
{
...
}
}
I think you should focus some of your learning on knowing the difference between a class
and a method
.
In your code you have created the following two classes:
public class Cir : Geometry
public class Rect : Geometry
Yet when you instantiate the classes in your Main()
method you do not call them by their class name, instead by the void
methods you have defined for each:
Rectangle a = new Rectangle(23.00, 21.4);
Circle b = new Circle(5.8);
What you likely want to do is rename both classes to Rectangle and Circle add a Constructor to each class that takes the parameters you want.
public class Circle : Geometry
{
public Circle(double radius)
{
perimeter = radius * 2 * Math.PI;
area = radius * radius * Math.PI;
}
}
Try to keep your methods light, restricting them to a single responsibility or purpose and name the accordingly. For instance in your Geometry
class, you could add a void PrintDetails()
method or even better a void PrintArea()
AND a void PrintPerimeter()
method:
public class Geometry
{
public double perimeter;
public double area;
public bool special;
public void PrintArea()
{
Console.WriteLine("Area: " + area);
}
public void PrintPerimeter()
{
Console.WriteLine("Perimeter: " + perimeter);
}
}
You can only create instances/objects of classes. Your class names are
Rect
and
Cir
Your Object names of these classes here are
a
and
b
respectively. so Your code should be
Rect a = new Rect;
a.Rectangle(23.00, 21.4); //Calls your method using Rect class object
Cir b = new Cir;
b.Circle(5.8);//Calls your method using Cir class object
The class you define is "Rect" as example. Now while you may think that is an abbreviation to "Rectangle", the compiler has a more altruistic point of view - it is NOT the same, so error.
Same with "Circ".
Now, both classes have CONSTRUCTORS with the proper name (which thus are not constructors) but the class names themselves - are wrong.
You did not closed the namespace geometry. You can create an instance of Rect(class) or Cir, not Rectangle(method) or Circle.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.