小编Lew*_*rin的帖子

无法访问继承的全局变量

我正在写一个小程序,应该从一个正方形的底座创建不同形状的房间.形状应该从正方形继承,其中包含顶点的List.

Room (父类)代码:

class Room
{
    private List<Point> vertices;
    private int oX;
    private int oY;
    private int width;
    private int height;

    public Room(int oX, int oY, int width, int height)
    {
        vertices = new List<Point>();
        addVertice(new Point(oX, oY));
        addVertice(new Point(oX + width, oY));
        addVertice(new Point(oX, oY + height));
        addVertice(new Point(oX + width, oY + height));

        this.width = width;
        this.height = height;
   }
}
Run Code Online (Sandbox Code Playgroud)

然后,我继承LShape从Room,但在Visual Studio说,vertices变量不能由于其保护级别访问.

LShape(儿童)班的代码

class LShape: Room
{
    public LShape(int oX, …
Run Code Online (Sandbox Code Playgroud)

c# inheritance

1
推荐指数
2
解决办法
70
查看次数

保存到文件时格式错误

我正在尝试将String Builder保存到文件中,但它不会创建新行.输出应该是这样的 在此输入图像描述

没有换行的输出 在此输入图像描述

class Program
{
    static public void justify(List<string> words, int width)
    {
        //some code to build the arrays
        int ii = 0, jj;
        StringBuilder builder = new StringBuilder();
        do
        {
            jj = result[ii];

            for(int k = ii; k < jj; k++)
            {
                builder.Append(words[k] + " ");
            }

            builder.Append("\n");
            ii = jj;
        }
        while(jj < words.Count);
        Console.WriteLine(builder.ToString());
        StreamWriter file = new StreamWriter("output.txt");
        file.Write(builder.ToString());
        file.Close();
    }

    static void Main(string[] args)
    {
        string file_extention, file1;
        file1 = Console.ReadLine();
        file_extention = "C:\\Users\\sayed\\Desktop\\" …
Run Code Online (Sandbox Code Playgroud)

c#

1
推荐指数
1
解决办法
47
查看次数

为什么在函数中的return语句之后打印Write-Host消息?

我编写了以下函数来尝试启动Sql Server代理服务(如果它当前没有运行):

function Start-SqlAgent([string] $AgentServiceName)
{
    $agentService = Get-Service -Name $AgentServiceName

    if ($AgentServiceName.Status -eq "Running")
    {
        Write-Host "$AgentServiceName is running"
        return
    }

    Write-Host "Starting $AgentServiceName..."
    # Code that starts the service below here (unrelated to my question)
}
Run Code Online (Sandbox Code Playgroud)

当Sql Agent服务正在运行时,我调用这样的函数:

Write-Host "Checking SQL Agent service status..."
Start-SqlAgent -AgentServiceName "SQLSERVERAGENT"
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

检查SQL代理服务状态...

启动SQLSERVERAGENT ...

为什么Starting SQLSERVERAGENT...显示消息?我期望的输出是:

检查SQL代理服务状态...

SQLSERVERAGENT正在运行

powershell

0
推荐指数
1
解决办法
36
查看次数

有人可以解释这个C#语法吗?

这里到底发生了什么:

public decimal[] Coefficients;
public decimal this[int i]
{
    get { return Coefficients[i]; }
    set { Coefficients[i] = value; }
}
Run Code Online (Sandbox Code Playgroud)

这是什么this服务?它是某种延伸decimal吗?

c#

-2
推荐指数
1
解决办法
132
查看次数

标签 统计

c# ×3

inheritance ×1

powershell ×1