您能协助我们解决C#中的oop问题吗? [英] Can you help us with an oop problem in c#?

查看:98
本文介绍了您能协助我们解决C#中的oop问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

你好吗?
我们来自阿根廷
抱歉,我的英语不太好.

我有一个练习可以解决OOP,我们正在寻找解决问题的最佳方法

要解决的问题是计算超类型集合(车辆)中任何子类型(汽车,卡车)的实例数

http://img808.imageshack.us/img808/9120/problemni.png [ ^ ]

Hi guys!

How are you?
We are from Argentina
Sorry but my english isn''t very good.

I have got an exercise to resolve with OOP and we are looking for the best way to solve it

The problem to solve is count the number of instances of any subtype (Car, Truck) in a supertype collection (Vehicle)

http://img808.imageshack.us/img808/9120/problemni.png[^]

IList<Vehicle> vehicles = new List<Vehicle>();

Car aCar = null;
Truck aTruck = null;

aCar = new Car();
vehicles.Add(aCar);

aCar = new Car();
vehicles.Add(aCar);

aCar = new Car();
vehicles.Add(aCar);

aTruck = new Truck();
vehicles.Add(aTruck);

aTruck = new Truck();
vehicles.Add(aTruck);

// 3 cars
// 2 trucks

int cars = 0;
int trucks = 0;

foreach(Vehicle v in vehicles) {
    if(v is Car){
        cars++;
    } else if(v is Truck){
        trucks++;
    }
}



是否有更好的方法来解决此问题,而无需使用子句"is Type"?
干杯!
Fernando Lopez Guevara



is there a better way to solve this problem, without using clause "is Type"?
Cheers!
Fernando Lopez Guevara

推荐答案

我认为,为了拥有计数器,您需要一次获取每个计数器的类型.因此您的代码:
I think in order to have counters you need to get type of each of them atleat once. Thus your code:
foreach(Vehicle v in vehicles) 
{    
  if(v is Car)
  {        
    cars++;    
  } 
  else if(v is Truck)
       {        
          trucks++;    
       }
}


看起来不错:thumbsup:


Looks fine. :thumbsup:


也许这可以帮到您

May be this alternate helps you

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

namespace ConsoleApplication1
{

    class Vehicle
    {
        public static int Count = 0;
        public Vehicle()
        {
            Count++;
        }
    }

    class Car :Vehicle
    {
        public static int Count = 0;
        public Car()
        {
            Count++;
        }
    }

    class Truck : Vehicle
    {
        public static int Count = 0;
         public Truck()
        {
            Count++;
        }
    }

    class Class3
    {
        public static void Main()
        {
            Vehicle V1 = new Car();
            Vehicle V2 = new Car();

            Vehicle V3 = new Truck();

            Console.WriteLine("No of Cars =" + Car.Count);
            Console.WriteLine("No of Truck =" + Truck.Count);
            Console.WriteLine("No of Vehicle=" + Vehicle.Count);

        }
    }
}


我可以!我想和你分享代码=)

i could it! and i want to share the code with you =)

using System.Collections.Generic;
using System;
namespace Visitor {
    public interface IVisitor {
        void Visit(Car c);
        void Visit(Truck t);
    }
    public class PaintVisitor : IVisitor {
        public void Visit(Car car) {
            car.Paint("red");
        }
        public void Visit(Truck truck) {
            truck.Paint("blue");
        }
    }
    public abstract class Vehicle {
        private string _color = "Black";
        public void Paint(string color) {
            this._color = color;
        }
        public abstract void Accept(IVisitor v);
        public override string ToString() {
            return string.Format("{0} - {1}", this.GetType(), this._color);
        }
    }
    public class Car : Vehicle {
        public override void Accept(IVisitor v) {
            v.Visit(this);
        }
    }
    public class Truck : Vehicle {
        public override void Accept(IVisitor v) {
            v.Visit(this);
        }
    }
    public class Program {
        public static void Main(string[] args) {
            List<Vehicle> vehicles = new List<Vehicle>();
            vehicles.Add(new Car());
            vehicles.Add(new Truck());
            vehicles.Add(new Car());
            vehicles.ForEach(x => Console.WriteLine(x));
            PaintVisitor pVisitor = new PaintVisitor();
            vehicles.ForEach(x => x.Accept(pVisitor));
            vehicles.ForEach(x => Console.WriteLine(x));
            Console.Read();
        }
    }
}



谢谢大家



thanks everyone


这篇关于您能协助我们解决C#中的oop问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆