Definition: a string instance fields: name
Define a double static fields: fundMoney, the initial value of 1000 yuan
The field in properties,
AddFund definition instances (ordinary) method to charity donations, modify the value of a static field), parameters for donations, methods the results of the output format is as follows:
XXX donations yyy yuan, now the charity fund for z yuan (XXX, yyy, ZZZ according to own actual data)
Create at least two objects in order to test the change of the static field
CodePudding user response:
namespace Test
{
Class Program
{
The class Foundation
{
Public string Name {get; The set; }
Public static double FundMoney {get; The set; }
Public Foundation (string name) {name=name; }
Public void AddFund (double donate)
{
FundMoney +=donate;
Console. WriteLine (${Name} "donated ${donate}, charity fund is now ${FundMoney}");
}
}
The static void Main ()
{
Foundation Aaron=new Foundation (" Aaron ");
Aaron. AddFund (1000);
Foundation Ben=new Foundation (" Ben ");
Ben. AddFund (2500.5);
}
}
}
CodePudding user response:
Two properties change on the setPublic string Name {get; Private set; }
Public static double FundMoney {get; Private set; }
CodePudding user response:
Using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
The namespace ConsoleApplication1
{
Class Program
{
The static void Main (string [] args)
{
Foundation a=new Foundation (" * * ");
A.A ddFund (1000);
Foundation b=new Foundation (" li si ");
B.A ddFund (2000);
C=new Foundation Foundation (" detective ");
C.a. ddFund (5000);
}
}
Public class Foundation
{
///& lt; Summary>
///name
///& lt;/summary>
Private string name;
Public string Name
{
The get {return name; }
The set {name=value; }
}
///& lt; Summary>
///fund lines
///& lt;/summary>
Private static double fundMoney;
Public static double FundMoney
{
The get {return fundMoney; }
The set {fundMoney=value; }
}
///& lt; Summary>
///static constructor
///& lt;/summary>
The static Foundation ()
{
FundMoney=1000;
}
///& lt; Summary>
///constructor
///& lt;/summary>
///& lt; Param name="name" & gt;
Public Foundation (string name)
{
this.name=name;
}
///& lt; Summary>
///money
///& lt;/summary>
///& lt; Param name="money" & gt;
Public void AddFund (double money)
{
FundMoney +=money;
Console. WriteLine (" donated ${1}, {0} now charity ${2} ", name, money, fundMoney);
}
}
}