主题:[讨论]关于(静态/实例)成员和方法的问题[求助]
(静态/实例)成员的区别,(静态/实例)方法的区别,书上说的一大堆,但我感觉都是格式上的区别,并没有从运用的理论上去阐述之间的区别。
我所想知道的是:
什么样的成员要被定义成静态成员,什么样的成员要被定义成实例成员,
什么样的方法要被定义成静态方法,什么样的方法要被定义成实例方法。
代码:
using System;
using System.Collections.Generic;
using System.Text;
/*该示例读取新雇员的姓名和id,逐个增加雇员,计数器计数并显示新雇员的
* 有关信息以及新的雇员数。(该程序从键盘读取当前雇员数)*/
namespace guyong
{
public class Employee
{
public string id;
public string name;/*为什么id和name就定义成实例成员*/
public Employee()
{ }
public Employee(string name, string id)
{
this.name = name;/*右边的name代表什么?它赋值给左边的this.name又代表什么?*/
this.id = id;
}
public static int employeeCounter;/*employeeCounter变量存储雇员数,为什么就把它定义成静态成员?*/
public static int AddEmployee() /*该方法为什么就被定义成静态方法?*/
{
return ++employeeCounter;
}
}
class MainClass:Employee
{
public static void Main(string[] args)/*这里 Main()方法的修饰符public用与不用有区别吗?*/
{
Console.Write("输入雇员姓名:");
string name = Console.ReadLine();
Console.Write("输入雇员姓名id:");
string id = Console.ReadLine();
Employee e = new Employee(name, id);
Console.Write("输入雇员数:");
string n = Console.ReadLine();
Employee.employeeCounter = Int32.Parse(n);
Employee.AddEmployee();
Console.WriteLine("Name:{0}",e.name);
Console.WriteLine("ID:{0}",e.id);
Console.WriteLine("新的雇员数为:{0}",Employee.employeeCounter);
}
}
}
-------------------------------
希望大大们能指点我代码中的困惑,我想从根本上去理解静态与实例的区别,而不是书上那种:有static的就是静态的。(妈妈的,我都想骂,我定义的时候怎么知道要不要加static啊。)
希望大大们能给我讲讲,再帮我看看代码中的疑问...
谢谢了....
我所想知道的是:
什么样的成员要被定义成静态成员,什么样的成员要被定义成实例成员,
什么样的方法要被定义成静态方法,什么样的方法要被定义成实例方法。
代码:
using System;
using System.Collections.Generic;
using System.Text;
/*该示例读取新雇员的姓名和id,逐个增加雇员,计数器计数并显示新雇员的
* 有关信息以及新的雇员数。(该程序从键盘读取当前雇员数)*/
namespace guyong
{
public class Employee
{
public string id;
public string name;/*为什么id和name就定义成实例成员*/
public Employee()
{ }
public Employee(string name, string id)
{
this.name = name;/*右边的name代表什么?它赋值给左边的this.name又代表什么?*/
this.id = id;
}
public static int employeeCounter;/*employeeCounter变量存储雇员数,为什么就把它定义成静态成员?*/
public static int AddEmployee() /*该方法为什么就被定义成静态方法?*/
{
return ++employeeCounter;
}
}
class MainClass:Employee
{
public static void Main(string[] args)/*这里 Main()方法的修饰符public用与不用有区别吗?*/
{
Console.Write("输入雇员姓名:");
string name = Console.ReadLine();
Console.Write("输入雇员姓名id:");
string id = Console.ReadLine();
Employee e = new Employee(name, id);
Console.Write("输入雇员数:");
string n = Console.ReadLine();
Employee.employeeCounter = Int32.Parse(n);
Employee.AddEmployee();
Console.WriteLine("Name:{0}",e.name);
Console.WriteLine("ID:{0}",e.id);
Console.WriteLine("新的雇员数为:{0}",Employee.employeeCounter);
}
}
}
-------------------------------
希望大大们能指点我代码中的困惑,我想从根本上去理解静态与实例的区别,而不是书上那种:有static的就是静态的。(妈妈的,我都想骂,我定义的时候怎么知道要不要加static啊。)
希望大大们能给我讲讲,再帮我看看代码中的疑问...
谢谢了....