车辆对象数组 - C#

Vehicle Object Array - C#

我在编写一些代码时遇到问题。我不太确定在哪里以及如何编写构造函数和访问器。

我要做的activity是这样的:

编写 3 个派生的 classes 以允许用户输入三种车辆的详细信息及其属性。

• 汽车(品牌、型号、年份、车身类型)

• 飞机(品牌、型号、年份、noEngines、engineType)

• 船(品牌、型号、年份、长度、船体类型)

第 4 个 class 是包含共享属性和方法的基础 class 车辆

将所有属性设为私有(在派生 classes 中)或受保护(在基 class 中)并为每个属性编写访问器方法。

为每个派生的 class 编写 2 个构造函数。一个没有参数,另一个接受派生 class 中的属性值作为参数。

编写一个名为 Fleet.cs 的控制台应用程序,它创建并显示每种车辆类型的 2 个

到目前为止我的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Vehicle
    {
        static void Main(string[] args)
        {
        }

        class Car
        {
            protected string make
            {
                get
                {
                    return make;
                }
                set
                {
                    make = value;
                }
            }

            protected string model
            {
                get
                {
                    return model;
                }
                set
                {
                    model = value;
                }
            }

            protected int year
            {
                get
                {
                    return year;
                }
                set
                {
                    year = value;
                }
            }

            protected string bodyType
            {
                get
                {
                    return bodyType;
                }
                set
                {
                    bodyType = value;
                }
            }

            public bool isInitialized;
            public Car()
            {
                isInitialized = true;
            }
        }
    }

    class Airplane
    {
        protected string make
        {
            get
            {
                return make;
            }
            set
            {
                make = value;
            }
        }

        protected string model
        {
            get
            {
                return model;
            }
            set
            {
                model = value;
            }
        }

        protected int year
        {
            get
            {
                return year;
            }
            set
            {
                year = value;
            }
        }

        protected int numEngines
        {
            get
            {
                return numEngines;
            }
            set
            {
                numEngines = value;
            }
        }

        protected int engineType
        {
            get
            {
                return engineType;
            }
            set
            {
                engineType = value;
            }
        }
    }

    class Boat
    {
        protected string make
        {
            get
            {
                return make;
            }
            set
            {
                make = value;
            }
        }

        protected string model
        {
            get
            {
                return model;
            }
            set
            {
                model = value;
            }
        }

        protected string year
        {
            get
            {
                return year;
            }
            set
            {
                year = value;
            }
        }

        protected string length
        {
            get
            {
                return length;
            }
            set
            {
                length = value;
            }
        }

        protected string hullType
        {
            get
            {
                return hullType;
            }
            set
            {
                hullType = value;
            }
        }
    }
}

第一部分OOP原则

Classes:

A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events. A class is like a blueprint. It defines the data and behavior of a type. If the class is not declared as static, client code can use it by creating objects or instances which are assigned to a variable. The variable remains in memory until all references to it go out of scope. At that time, the CLR marks it as eligible for garbage collection. If the class is declared as static, then only one copy exists in memory and client code can only access it through the class itself, not an instance variable. For more information, see Static Classes and Static Class Members (C# Programming Guide). Unlike structs, classes support inheritance, a fundamental characteristic of object-oriented programming. For more information, see Inheritance (C# Programming Guide).

对象也是 classes 的实例。

Inheritance:

Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics (or pillars) of object-oriented programming. Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.

派生 class:
基于先前存在的 class(即基础 class)创建的 class。派生的 class 继承它派生的基 class 的所有成员变量和方法。 也称为派生类型。

Method:

A method (or message) in object-oriented programming (OOP) is a procedure associated with an object class. An object is made up of behavior and data. Data is represented as properties of the object and behavior as methods. Methods are also the interface an object presents to the outside world. For example a window object would have methods such as open and close. One of the most important capabilities that a method provides is method overriding. The same name (e.g., area) can be used for multiple different kinds of classes. This allows the sending objects to invoke behaviors and to delegate the implementation of those behaviors to the receiving object. For example an object can send an area message to another object and the appropriate formula will be invoked whether the receiving object is a rectangle,circle, triangle, etc.

Attributes and properties:

"Fields", "class variables", and "attributes" are more-or-less the same - a low-level storage slot attached to an object. Each language's documentation might use a different term consistently, but most actual programmers use them interchangeably. (However, this also means some of the terms can be ambiguous, like "class variable" - which can be interpreted as "a variable of an instance of a given class", or "a variable of the class object itself" in a language where class objects are something you can manipulate directly.)

"Properties" are, in most languages I use, something else entirely - they're a way to attach custom behaviour to reading / writing a field. (Or to replace it.)

因此,如果要对它们进行分类,它们就是 OOP(面向对象编程)原则。

第二部分:

Write a Console Application called Fleet.cs which creates and displays 2 of each Vehicle type.

因此,一种方法是创建硬编码的车辆。另一种方法是使用 Console.Readline() 向用户询问车辆详细信息。 Main 方法可能看起来像这样。

static void Main(string[] args)
{
    Vehicle v1 = new Vehicle { Make = "test1", Model = "model1", Year = 1996 };
    Vehicle v2 = new Vehicle { Make = "test2", Model = "model2", Year = 1997 };
    Console.WriteLine(v1);
    Console.WriteLine(v2);
    ...
}

然后您将为每个 class 覆盖 ToString() 方法。像这样:

public override string ToString()
{
    return string.Format("Vehicle is {0} and of model {1} and is made in {2}.", make, model, year);
}

这里也可以用base.ToString()得到派生class.

中上(底)class的数据

编辑 1:用户输入:

所以如果你想要用户输入,你可以像这样编写程序:

static void Main(string[] args)
{
    //input
    Vehicle v1 = new Vehicle();
    Console.Write("Enter the make of 1st vehicle: ");
    v1.Make = Console.ReadLine();
    Console.Write("Enter the model of 1st vehicle: ");
    v1.Model = Console.ReadLine();
    Console.WriteLine("Enter the year of manufacturing for 1st vehicle:");
    v1.Year = int.Parse(Console.ReadLine());
    //output
    Console.WriteLine("The data for 1st vehicle: ");
    Console.WriteLine(v1);
    ...
}

更好的方法是在 class 中创建 Input 方法并从 Main 程序中调用它。所以代码不会重复。

完成的程序

Vehicle.cs

using System;

class Vehicle
{
    string make, model;
    int year;

    public string Make { get { return make; } set { make = value; } }
    public string Model { get { return model; } set { model = value; } }
    public int Year { get { return year; } set { year = value; } }

    public Vehicle()
    {
        make = model = "Unknown";
        year = 0;
    }

    public Vehicle(string make, string model, int year)
    {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    public virtual void GetFromInput()
    {
        Console.Write("Enter the make of vehicle: ");
        Make = Console.ReadLine();
        Console.Write("Enter the model of vehicle: ");
        Model = Console.ReadLine();
        Console.WriteLine("Enter the year of manufacturing for vehicle: ");
        Year = int.Parse(Console.ReadLine());
    }

    public override string ToString()
    {
        return string.Format("Vehicle is {0} and of model {1} and is made in {2}.", make, model, year);
    }
}

Car.cs

using System;

class Car : Vehicle
{
    string bodyType;
    public string BodyType { get { return bodyType; } set { bodyType = value; } }

    public Car() : base()
    {
        bodyType = "Unknown";
    }

    public Car(string make, string model, int year, string bodyType) : base(make, model, year)
    {
        this.bodyType = bodyType;
    }

    public override void GetFromInput()
    {
        base.GetFromInput();
        Console.Write("Enter body type for the car: ");
        BodyType = Console.ReadLine();
    }

    public override string ToString()
    {
        return base.ToString() + string.Format("This vehicle is a car with body type of {0}.", BodyType);
    }
}

Airplane.cs

using System;

class Airplane : Vehicle
{
    int noEngines;
    string engineType;
    public int NumberOfEngines{ get { return noEngines; } set { noEngines = value; } }
    public string EngineType { get { return engineType; } set { engineType = value; } }

    public Airplane() : base()
    {
        noEngines = 0;
        engineType = "Unknown";
    }

    public Airplane(string make, string model, int year, int noEngines, string engineType) : base(make, model, year)
    {
        this.noEngines = noEngines;
        this.engineType = engineType;
    }

    public override void GetFromInput()
    {
        base.GetFromInput();
        Console.Write("Enter the number of engines on an airplane: ");
        NumberOfEngines = int.Parse(Console.ReadLine());
        Console.Write("Enter the engine type for the airplane: ");
        EngineType = Console.ReadLine();
    }

    public override string ToString()
    {
        return base.ToString() + string.Format("This vehicle is an airplane with {0} engines and engine type of {1}.", NumberOfEngines, EngineType);
    }
}

Boat.cs

using System;

class Boat : Vehicle
{
    int length;
    string hullType;
    public int Length { get { return length; } set { length = value; } }
    public string HullType { get { return hullType; } set { hullType = value; } }

    public Boat() : base()
    {
        length = 0;
        hullType = "Unknown";
    }

    public Boat(string make, string model, int year, int length, string hullType) : base(make, model, year)
    {
        this.length = length;
        this.hullType = hullType;
    }

    public override void GetFromInput()
    {
        base.GetFromInput();
        Console.Write("Enter the length of the boat: ");
        Length = int.Parse(Console.ReadLine());
        Console.Write("Enter the hull type for the boat: ");
        HullType = Console.ReadLine();
    }

    public override string ToString()
    {
        return base.ToString() + string.Format("This vehicle is a boat with length of {0} and hull type of {1}.", Length, HullType);
    }
}

Fleet.cs

using System;

class Fleet
{
    static void Main(string[] args)
    {
        Vehicle v1 = new Vehicle();
        v1.GetFromInput();
        Console.WriteLine(v1);
        //... for the other vehicles
    }
}

这可以使用 class 继承来实现。

您的每个 车辆 class 都需要继承一个通用的 class 来实现 'all' [=21= 的功能需求]vehicles,这个 class(Vehicle 接受),然后可以在 C# 中用于识别任何类型的车辆 class/type。

你可以抽象出每辆车所需的通用功能,并实现一个 class 公开了这些常见关系:

using System;

public namespace CodeSpace {
 public class Vehicle { 
  public Vehicle(Type type, string make, string model) {
   Model = model;
   Make = make;
   Type = type;
  }
  public Type VehicleType { get; private set; }
  public string Make { get; set; } 
  public string Model { get; set; } 
 }

 public class Airplane : Vehicle {
  public class Airplane(string make, string model) : base(typeof(Airplane), make, model) {
  }
 }

 public class Boat : Vehicle {
  public class Boat(string make, string model) : base(typeof(Boat), make, model) {
  }
 }

 public class Car : Vehicle {
  public class Car(string make, string model) : base(typeof(Car), make, model) {
  }
 }

 class Program {
 public static void Main(params string[] args ) {       
 var vehicles = new List<Vehicle>() {
    new Boat("Canoe", "X2") as Vehicle,
    new Boat("Raft", "A") as Vehicle,
    new Car("Ford", "T") as Vehicle,
    new Airplane("BMW", "Idk") as Vehicle,
 };

  foreach(var v in vehicles) { 
   Console.WriteLine(v.VehicleType.FullName);
  }
 }
}

}

现在您的所有车辆都可以使用一个 class 来识别,它通过一个通用界面公开所有车辆。