如题所述
å®ä¹ï¼
éç¨è¯è¨è¿è¡æ¶CLRè¦æ±æ¯ä¸ªç±»é½æä¸ä¸ªæé å½æ°ãæé å½æ°æ¯ä¸ä¸ªæç¹æ®ç¨éçæ¹æ³ï¼ç¬¬ä¸æ¬¡å¼ç¨æ¶ä¼åå§åç±»æç±»å®ä¾ã
åç±»ï¼
å®ä¾æé å½æ°ï¼instanceï¼ãç§ææé å½æ°ï¼privateï¼å®ä¾æé å½æ°çä¸ç§ç¹æ®æ åµï¼åéææé å½æ°ï¼staticï¼ã
æé å½æ°æ²¡æè¿åæ°æ®ç±»åï¼ä¸å¢å äºä¸ä¸ªinitializer(åå§åè°ç¨)çé项ï¼å ¶ä½åä¸è¬æ¹æ³æ²¡æåºå«ï¼ä¸è¿è¿æä¸äºçº¦æ
1ï¼æé å½æ°å¿ é¡»ä¸ç±»åç¸å
2ï¼éè¿initializerå¨æé å½æ°ä½åè°ç¨åºç±»æè æ¬ç±»çå¦ä¸ä¸ªæé å½æ°çæ¹æ³
aï¼base(argument list) -è°ç¨åºç±»æé å½æ°
bï¼this(argument list) è°ç¨è¿ä¸ªç±»çå¦ä¸ä¸ªæé å½æ°
3ï¼å¦æ类没æåæé å½æ°ï¼ç¼è¯å¨å°ä¼ç»ç±»åé ä¸ä¸ªæ åçæé å½æ°
å®ä¾åç±»çæ¶åå ¶å®å°±æ¯è°ç¨äºæé å½æ°ï¼
å¦ ï¼
Test t=new Test();//è°ç¨é»è®¤ç空æé å½æ°
Test tt=new Test(test);//è°ç¨äºæä¸ä¸ªåæ°çæé å½æ°
ç§ææé å½æ°ï¼
æ们ç¥é对类æåç¨private修饰æ¶åï¼æ¯ä¸ºäºä¸å 许å¨ç±»å¤çèå´è¿ä¸ªæåï¼
é£ä¹ï¼ç¨private修饰æé å½æ°çæ¶åï¼æ¯ä¸ºç¦æ¢å®ä¾åè¿ä¸ªç±»çåè½ï¼å¦æä¸ä¸ªç±»åªæä¾éæçæ¹æ³åå段ï¼å°±å¯ä»¥ä½¿ç¨ç§ææé å½æ°ï¼æ³¨ï¼ä½¿ç¨äºç§ææé å½æ°ä¸è½è¢«ä¹ä¸ä¼è¢«ç»§æ¿ï¼ä¸è¿å¨ç±»ä¸å¯ä»¥å¯¹èªå·±è¿è¡å®ä¾å~å¯ä»¥æä¾ä¸ä¸ªå·¥åæ¹æ³å建对象
éææé å½æ°ï¼
主è¦ä½ç¨æ¯åå§åéæç±»æåï¼
éå¶ï¼ä¸è½æåæ°ï¼ä¸è½éè½½ï¼ä¸ä¸ªç±»åªè½æä¸ä¸ªéææé å½æ°ï¼ æ§å¿ é¡»æ¯privateï¼ä¸è½è°ç¨å ¶ä»æé å½æ°ï¼åª è½ç±»ä¸çéææåï¼æ³¨æåé»è®¤çæ åæ°çæé å½æ°è¿è¡åºå
ä¸é¢ç代ç æ¯æç»ä¹ çæ¶ååçç®åç使ç¨ä¾åï¼
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class A
{
public A(){} //è¿ä¸ªæé å½æ°ç¼è¯å¨ä¼èªå·±æ·»å ï¼ååä¸åä¸æ ·
private string _firstName;
private string _lastName;
public A(string firstNameï¼ string lastName) {
this._firstName = firstName;
this._lastName = lastName;
}
public string FirstName
{
set
{
_firstName = value;
}
get
{
return _firstName;
}
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public override string ToString() //éåäºobjectçToString()æ¹æ³
{
return My Name is +_firstName+ + _lastName;
}
};
class B : A
{
private string title;
public string Titel
{
get { return title; }
set { title = value; }
}
public B(string firstNameï¼ string lastNameï¼ string title):base(firstNameï¼lastName)// è¿éè°ç¨äºåºç±»çæé å½æ°
{
this.title = title;
}
public override string ToString()
{
return base.ToString()+ï¼And my title is +this.title;
}
}
class C:A
{
private string _title;
public string Titel
{
get { return _title; }
set { _title = value; }
}
private string _level;
public string Level
{
get { return _level; }
set { _level = value; }
}
public C(string firstNameï¼ string lastNamtï¼ string title)
: base(firstNameï¼ lastNamt)
{
this._title = title;
}
public C(string firstNameï¼ string lastNameï¼ string titleï¼ string level)
: this(firstNameï¼ lastNameï¼ title)//è°ç¨äºèªå·±çå¦ä¸ä¸ªæé å½æ°
{
this._level = level;
}
public override string ToString()
{
if(this._level==null){
return base.ToString()+ï¼ And My Title is +this._title;
}else{
return base.ToString()+ï¼ My Title is +this._title +ï¼ And My Level is +this._level;
}
}
}
//ç§ææé å½æ°
class D
{
private D()
{
}
//åè¡¥å
public staic D CreateD=new D();
private static string _name;
public static string Name
{
get { return _name; }
set { _name = value; }
}
private static int _age;
public static int Age
{
get { return _age; }
set { _age = value; }
}
public static new string ToString()
{
return My Name is + _name + ï¼ and I;m + _age.ToString();
}
}
//éææé å½æ°ç±»
class E
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
private static int i;
static E()
{
Console.WriteLine(Static æé å½æ°+i);
}
public E()
{
i += 1;
Console.WriteLine( æ®é æé å½æ° + i);
}
public E(string nameï¼ int age)
{
this.name = name;
this.age = age;
éç¨è¯è¨è¿è¡æ¶CLRè¦æ±æ¯ä¸ªç±»é½æä¸ä¸ªæé å½æ°ãæé å½æ°æ¯ä¸ä¸ªæç¹æ®ç¨éçæ¹æ³ï¼ç¬¬ä¸æ¬¡å¼ç¨æ¶ä¼åå§åç±»æç±»å®ä¾ã
åç±»ï¼
å®ä¾æé å½æ°ï¼instanceï¼ãç§ææé å½æ°ï¼privateï¼å®ä¾æé å½æ°çä¸ç§ç¹æ®æ åµï¼åéææé å½æ°ï¼staticï¼ã
æé å½æ°æ²¡æè¿åæ°æ®ç±»åï¼ä¸å¢å äºä¸ä¸ªinitializer(åå§åè°ç¨)çé项ï¼å ¶ä½åä¸è¬æ¹æ³æ²¡æåºå«ï¼ä¸è¿è¿æä¸äºçº¦æ
1ï¼æé å½æ°å¿ é¡»ä¸ç±»åç¸å
2ï¼éè¿initializerå¨æé å½æ°ä½åè°ç¨åºç±»æè æ¬ç±»çå¦ä¸ä¸ªæé å½æ°çæ¹æ³
aï¼base(argument list) -è°ç¨åºç±»æé å½æ°
bï¼this(argument list) è°ç¨è¿ä¸ªç±»çå¦ä¸ä¸ªæé å½æ°
3ï¼å¦æ类没æåæé å½æ°ï¼ç¼è¯å¨å°ä¼ç»ç±»åé ä¸ä¸ªæ åçæé å½æ°
å®ä¾åç±»çæ¶åå ¶å®å°±æ¯è°ç¨äºæé å½æ°ï¼
å¦ ï¼
Test t=new Test();//è°ç¨é»è®¤ç空æé å½æ°
Test tt=new Test(test);//è°ç¨äºæä¸ä¸ªåæ°çæé å½æ°
ç§ææé å½æ°ï¼
æ们ç¥é对类æåç¨private修饰æ¶åï¼æ¯ä¸ºäºä¸å 许å¨ç±»å¤çèå´è¿ä¸ªæåï¼
é£ä¹ï¼ç¨private修饰æé å½æ°çæ¶åï¼æ¯ä¸ºç¦æ¢å®ä¾åè¿ä¸ªç±»çåè½ï¼å¦æä¸ä¸ªç±»åªæä¾éæçæ¹æ³åå段ï¼å°±å¯ä»¥ä½¿ç¨ç§ææé å½æ°ï¼æ³¨ï¼ä½¿ç¨äºç§ææé å½æ°ä¸è½è¢«ä¹ä¸ä¼è¢«ç»§æ¿ï¼ä¸è¿å¨ç±»ä¸å¯ä»¥å¯¹èªå·±è¿è¡å®ä¾å~å¯ä»¥æä¾ä¸ä¸ªå·¥åæ¹æ³å建对象
éææé å½æ°ï¼
主è¦ä½ç¨æ¯åå§åéæç±»æåï¼
éå¶ï¼ä¸è½æåæ°ï¼ä¸è½éè½½ï¼ä¸ä¸ªç±»åªè½æä¸ä¸ªéææé å½æ°ï¼ æ§å¿ é¡»æ¯privateï¼ä¸è½è°ç¨å ¶ä»æé å½æ°ï¼åª è½ç±»ä¸çéææåï¼æ³¨æåé»è®¤çæ åæ°çæé å½æ°è¿è¡åºå
ä¸é¢ç代ç æ¯æç»ä¹ çæ¶ååçç®åç使ç¨ä¾åï¼
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class A
{
public A(){} //è¿ä¸ªæé å½æ°ç¼è¯å¨ä¼èªå·±æ·»å ï¼ååä¸åä¸æ ·
private string _firstName;
private string _lastName;
public A(string firstNameï¼ string lastName) {
this._firstName = firstName;
this._lastName = lastName;
}
public string FirstName
{
set
{
_firstName = value;
}
get
{
return _firstName;
}
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public override string ToString() //éåäºobjectçToString()æ¹æ³
{
return My Name is +_firstName+ + _lastName;
}
};
class B : A
{
private string title;
public string Titel
{
get { return title; }
set { title = value; }
}
public B(string firstNameï¼ string lastNameï¼ string title):base(firstNameï¼lastName)// è¿éè°ç¨äºåºç±»çæé å½æ°
{
this.title = title;
}
public override string ToString()
{
return base.ToString()+ï¼And my title is +this.title;
}
}
class C:A
{
private string _title;
public string Titel
{
get { return _title; }
set { _title = value; }
}
private string _level;
public string Level
{
get { return _level; }
set { _level = value; }
}
public C(string firstNameï¼ string lastNamtï¼ string title)
: base(firstNameï¼ lastNamt)
{
this._title = title;
}
public C(string firstNameï¼ string lastNameï¼ string titleï¼ string level)
: this(firstNameï¼ lastNameï¼ title)//è°ç¨äºèªå·±çå¦ä¸ä¸ªæé å½æ°
{
this._level = level;
}
public override string ToString()
{
if(this._level==null){
return base.ToString()+ï¼ And My Title is +this._title;
}else{
return base.ToString()+ï¼ My Title is +this._title +ï¼ And My Level is +this._level;
}
}
}
//ç§ææé å½æ°
class D
{
private D()
{
}
//åè¡¥å
public staic D CreateD=new D();
private static string _name;
public static string Name
{
get { return _name; }
set { _name = value; }
}
private static int _age;
public static int Age
{
get { return _age; }
set { _age = value; }
}
public static new string ToString()
{
return My Name is + _name + ï¼ and I;m + _age.ToString();
}
}
//éææé å½æ°ç±»
class E
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
private static int i;
static E()
{
Console.WriteLine(Static æé å½æ°+i);
}
public E()
{
i += 1;
Console.WriteLine( æ®é æé å½æ° + i);
}
public E(string nameï¼ int age)
{
this.name = name;
this.age = age;
温馨提示:答案为网友推荐,仅供参考
第1个回答 2013-10-17
构造函数名与类名相同,不返回任何值 ,主要作用是给字段赋初始值;
如
public class Teacher{public <strong>Teacher</strong>(){}//无参数的构造函数 函数名Teacher 要和类名 相同 publlc Teacher(string names)//有参数的构造函数,给Teacher 类中的字段name 赋初始值{name=names;}string name;//Student 类中的字段public string Name//封装字段{get{return name;}set{name=value;}}}
如
public class Teacher{public <strong>Teacher</strong>(){}//无参数的构造函数 函数名Teacher 要和类名 相同 publlc Teacher(string names)//有参数的构造函数,给Teacher 类中的字段name 赋初始值{name=names;}string name;//Student 类中的字段public string Name//封装字段{get{return name;}set{name=value;}}}
第2个回答 2013-10-17
public class Student{
//无参数构造
public Student(){
}
public Student(string name){
}
}
//无参数构造
public Student(){
}
public Student(string name){
}
}
第3个回答 2013-10-17
VS中构造函数没有快捷键。只能手动敲出来。(注意的是,最好要重载这个构造函数,一个无参,一个有参的,因为在类中,没有系统默认的无参构造早函数,如果在结构中可以省去无参的构造函数)。
VS中虽然没有构造函数的快捷键,但是对字段的封装有快捷键。(ctrl R E)或者在写字段时写打上PROP然后按2次TAB键,直接可以把字段和属性的格式打出来了,然后改下字段和属性的类型名称即可。
VS中虽然没有构造函数的快捷键,但是对字段的封装有快捷键。(ctrl R E)或者在写字段时写打上PROP然后按2次TAB键,直接可以把字段和属性的格式打出来了,然后改下字段和属性的类型名称即可。