c#写一个玩家类,玩家可以拥有各种工具,现在有四种工具,扳手,拖把,钉子,锅子玩家默认拥有锅子,请在玩家类中写一个方法,可以拾取不同的工具替换自己拥有的工具
时间: 2024-04-14 07:30:19 浏览: 18
好的,下面是一个示例的C#代码:
```csharp
public class Player
{
private string[] tools = { "扳手", "拖把", "钉子", "锅子" };
private string currentTool = "锅子";
public void PickUpTool(string newTool)
{
if (tools.Contains(newTool))
{
currentTool = newTool;
Console.WriteLine("成功拾取工具:" + newTool);
}
else
{
Console.WriteLine("未找到该工具:" + newTool);
}
}
}
```
在上述代码中,`Player`类有一个私有字段`tools`,存储了所有可用的工具名称,以及一个私有字段`currentTool`,表示当前玩家所拥有的工具。`PickUpTool`方法可以用来拾取新的工具并替换当前的工具。如果传入的新工具在可用工具列表中,将会更新`currentTool`字段,并打印成功拾取的信息;否则,将会打印未找到该工具的信息。
你可以根据需要在该类中添加其他属性和方法。希望对你有所帮助!
相关问题
C# 写一个http请求工具类
可以使用 C# 自带的 HttpClient 类来实现 HTTP 请求。以下是一个简单的 HTTP 请求工具类示例:
```csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace HttpUtils
{
public class HttpUtil
{
private static readonly HttpClient client = new HttpClient();
public static async Task<string> Get(string url)
{
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
public static async Task<string> Post(string url, string data)
{
HttpContent content = new StringContent(data);
HttpResponseMessage response = await client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
}
```
使用示例:
```csharp
using System;
using System.Threading.Tasks;
using HttpUtils;
namespace Test
{
class Program
{
static async Task Main(string[] args)
{
string result = await HttpUtil.Get("https://www.baidu.com");
Console.WriteLine(result);
result = await HttpUtil.Post("https://www.example.com/api", "{\"name\":\"John\",\"age\":30}");
Console.WriteLine(result);
}
}
}
```
写一个C#mysql工具类
以下是一个简单的 C# MySQL 工具类的实现,包含了连接、查询和关闭数据库等基本操作:
```csharp
using System;
using System.Data;
using MySql.Data.MySqlClient;
namespace MySqlUtils
{
public class MySqlHelper
{
private readonly string connectionString; // 数据库连接字符串
public MySqlHelper(string connectionString)
{
this.connectionString = connectionString;
}
// 执行查询,返回 DataTable
public DataTable ExecuteQuery(string sql)
{
using (var connection = new MySqlConnection(connectionString))
{
connection.Open();
var command = new MySqlCommand(sql, connection);
var adapter = new MySqlDataAdapter(command);
var dataTable = new DataTable();
adapter.Fill(dataTable);
return dataTable;
}
}
// 执行非查询操作,返回受影响的行数
public int ExecuteNonQuery(string sql)
{
using (var connection = new MySqlConnection(connectionString))
{
connection.Open();
var command = new MySqlCommand(sql, connection);
return command.ExecuteNonQuery();
}
}
// 关闭数据库连接
public void Close()
{
MySqlConnection.ClearAllPools(); // 清空连接池
}
}
}
```
使用示例:
```csharp
using System;
using System.Data;
using MySqlUtils;
namespace Test
{
class Program
{
static void Main(string[] args)
{
var connectionString = "server=localhost;uid=root;pwd=root;database=test;";
var helper = new MySqlHelper(connectionString);
// 查询
var dataTable = helper.ExecuteQuery("SELECT * FROM users");
foreach (DataRow row in dataTable.Rows)
{
Console.WriteLine(row["id"] + " " + row["name"]);
}
// 非查询操作
var affectedRows = helper.ExecuteNonQuery("INSERT INTO users(name) VALUES('Test')");
Console.WriteLine("Affected Rows: " + affectedRows);
helper.Close();
}
}
}
```
注意:该示例中的连接字符串需要根据实际情况进行修改。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)