小编Neg*_*cki的帖子

为什么构造函数不会影响此示例中的属性

我想知道为什么当我们 p = new Person("TOM", 999);通过调用 fred.PrintInfo(); 它没有改变p到TOM和999,但通过使用p.age = 99; 我们可以很好地改变fred的年龄,构造函数和属性都是公共的,那么我在这里缺少什么?我不想对这段代码做任何事我只想要原因.

using System;

class Person
{
    public string fullName;
    public int age;

    public Person(string n, int a)
    {
        fullName = n;
        age = a;
    }

    public void PrintInfo()
    {
        Console.WriteLine("{0} is {1} years old", fullName, age);
    }
}

class MainClass
{
    public static void SendAPersonByValue(Person p)
    {
        p.age = 99;

        p = new Person("TOM", 999);
    }

    public static void Main()
    {
        Person fred = new Person("Fred", 12);
        fred.PrintInfo();
        SendAPersonByValue(fred); …
Run Code Online (Sandbox Code Playgroud)

c#

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

如果语句SyntaxError:语法无效

我想计算中位数我不知道这里有什么问题更新:

def median(a,b,c):
    if a>b:
        if b>c:
        return b
        else:
        if a>c:
            return c
        else:
            return a
    else:
        if b<c:
            return b
        else:
        if a>c:
            return a
        else: 
            return c
Run Code Online (Sandbox Code Playgroud)

我的错误是:文件"prog.py",第4行返回b ^ IndentationError:预期缩进块

python

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

标签 统计

c# ×1

python ×1