C#中的OOP概念问题 [英] OOP concept problem in C#

查看:93
本文介绍了C#中的OOP概念问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我是C#的新生,在我的业余项目中遇到了逻辑/概念问题.我想要一个好的面向对象的方法,但无法弄清楚:confused:

我已经简化了我的问题,以便于理解.
考虑一下,一个家庭"类,它具有许多不同类型的房间.每个房间都有一些共同的属性,并具有许多不同的属性.
请阅读代码,其自我说明.

问题A:如何在"writeAllRoomData"功能中获取所有房间的适当数据?

问题B:是否有更好的方法,以使Room(具有特定房间类型)的每个实例都没有其他Roomtype变量的负担?
(例如,在下面的第一个代码中,每个room实例将具有每种roomtype的struct vars)

问题C:与实际代码一样,房间类型太多.每当房间类中的某个函数想要编辑该特定房间类型的属性时(例如通过使用接口或多态函数等),是否有一种更好的方法可以在不检查房间类型的情况下编写此类代码?
如果是,请提供一个小的示例代码.

Hello everyone,
I am a freshman in C#, and stuck with a logical/conceptual problem in my hobby project. I want a good object oriented approach, but can not figure out how :confused:

I''ve simplified my problem for easy understanding.
Consider, a class "Home", it has many different kind of Rooms. Each room share some common property and has many different properties.
Please read the code, its self explainatory.

Ques A : How to get all the rooms''s appropriate data in "writeAllRoomData" function ?

Ques B : Is there a better way, so that each instance of Room(of a particular roomtype) does not have the burden of other roomtype''s vars?
(e.g. in the first code below each instance of room will have struct vars of every roomtype)

Ques C : As in the actual code, type of rooms are too many. Is there a better way to make such code without checking for roomtype, everytime a function in room class want to edit properties of that particular roomtype, (e.g. by using interfaces or Polymorphic functions etc ?)
If yes, can you please provide a small sample code.

class home
{
	OnButtonClick()
	{
		switch(something)
		{
			case A : roomType = enumRoomType.bedroom;
			case B : roomType = enumRoomType.bathroom;
			....
		}
		Room room = new Room(roomType);
		listofRoom.Add(room);
	}
	
	writeAllRoomData()
	{
		foreach(room in listofRoom)
		{
			//How to access the appropriate data for each room ?
			//e.g. if listofRoom[0] is of type bedroom, access "bed"
		}
	}
	
	
}





class Room
{
	private enumRoomType roomType;
	
	public void Room (enumRoomType roomType)
	{
		this.roomType = roomType;
	}
	
	struct BedroomVars{
		public bool bed;
		public int lamps;
	}
	BedroomVars _bedroomVars;

	struct BathroomVars{
		public bool tub;
		public int soaps;
	}	
	BathroomVars _bathroomVars;
	
	struct kitchen{
	...
	}
	
	...

	public void SomeFunc();
	{
		switch(RoomType)
		{
			case enumRoomType.bedroom : 
				// Do something (maybe sex ... LOL:)
				_bedroomVars.bed = 1; break; 
			case enumRoomType.bathroom : 
				_bathroomVars.soaps = 2; break;
			...
		} 
	}
}



另一种选择是继承Room类,以制作不同的roomclass.



Another option is to inherit Room class for making different room classes

class Room
{
	private enumRoomType roomType;
	
	private BedroomVars _bedroomVars;
	private BathroomVars _bathroomVars;
		
	public void Room (enumRoomType roomType)
	{
		this.roomType = roomType;
		switch (roomType)
		{
			case enumRoomType.Bedroom : 
				_bedroomVars = new BedroomVars; break;
			case enumRoomType.Bathroom : 
				_bathroomVars = new BathroomVars; break;
		}
	}

	public void SomeFunc();
	{
        	switch(RoomType)
        	{
            		case enumRoomType.bedroom :
				// Do something 
                		_bedroomVars.bed = 1; break;
            		case enumRoomType.bathroom :
				 // Do something
                		_bathroomVars.soaps = 2; break;
	            ...
        	}
	}
}

public class BedroomVars : Room
{
	bool bed;
	int lamps;
}

public class BathroomVars : Room
{
	bool tub;
	int soaps;
}	



我将非常感谢您的帮助.



I will be very thankful for any help.

推荐答案

1.正确的OO方法是继承的方法.

2. writeAllRoomData()
-在您的基类中将此方法声明为虚拟方法(考虑将基类标记为abstract,因此您必须从中继承)Room,并在继承的类中重写实现.之后,您可以调用第一个基本实现(base.writeAllRoomData()),并使用继承的类的特殊属性对其进行扩展.
-您的Home将保留对Room对象的数组(列表)的引用,这些对象将使用专门的实例(例如Room newRom = new Kitchen())进行初始化.

1. Right OO approach approach is the inheritance.

2. writeAllRoomData()
- declare this method as virtual in your base class (consider marking the base class as abstract so you must inherit from it) Room and override implementation in inherited class. Thereafter you can call at first base implementation (base.writeAllRoomData()) and extend it with specialized properties of inherited class.
- your Home will hold reference to an array (list) of Room objects which will be initialized with a specialized instance (such as Room newRom = new Kitchen()).

public abstract class Room
{
  public int Length { get;set;}
  public int Width { get;set;}

  public virtual void writeAllRoomData()
  {
    Console.WriteLine("Size: {0}x{1}", Length, Width);
  }
}

public class Kitchen: Room
{
  public bool HasCooker { get;set; }
  public override void writeAllRoomData()
  {
    base.writeAllRoomData();
    Console.writeAllRoomData("Has cooker: {0}", HasCooker);
  }
}

public class Home
{
  private List<Room> _rooms = new List<Room>();

  public List<Room> Rooms { get { return _rooms; }}

  public void writeHome()
  {
    Console.WriteLine("Home {");

    foreach(Room room in Rooms)
      room.writeAllRoomData();

    Console.WriteLine("}");
  }
}


这篇关于C#中的OOP概念问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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