A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 朱勋 黑马帝   /  2011-12-11 21:16  /  2206 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

在继承中,子类一般要对所继承的父类的成员变量进行初始化是通过调用基类的构造函数进行初始化,但是构造函数可以重载,构造函数之间可以进行互相调用怎么理解。

4 个回复

倒序浏览
王旭 黑马帝 2011-12-11 22:45:22
沙发
构造函数的操作,说白了就是在实例化对象时,按照不同的需要去构造特殊的对象。构造函数之间的调用应该说和不会出现在同一个类内部,往往是在另一个类中构造另一个对象时使用。

评分

参与人数 1技术分 +1 收起 理由
朱勋 + 1

查看全部评分

回复 使用道具 举报
朱勋 黑马帝 2011-12-11 23:06:18
藤椅
王旭 发表于 2011-12-11 22:45
构造函数的操作,说白了就是在实例化对象时,按照不同的需要去构造特殊的对象。构造函数之间的调用应该说和 ...

构造函数调用够造函数就是出现在同一类当中,
  1. using System;

  2. namespace WindowsApplication2
  3. {
  4.         /// <summary>
  5.         /// Summary description for BankCustomer.
  6.         /// </summary>
  7.         public class BankCustomer
  8.         {
  9.                 public string   firstName;
  10.                 public string   lastName;
  11.                 private double   balance;
  12.                 private Person[] relatives;

  13.                 public BankCustomer()
  14.                 {
  15.                         //
  16.                         // TODO: Add constructor logic here
  17.                         //

  18.                         this.firstName = "";
  19.                         this.lastName = "";
  20.                         this.balance = 0.0;
  21.                         relatives = new Person[100];
  22.                 }

  23.                 public BankCustomer(string fn, string ln, double bal)
  24.                 {
  25.                         this.firstName = fn;
  26.                         this.lastName  = ln;
  27.                         this.balance   = bal;
  28.                 }

  29. //                public BankCustomer(string fn, string ln)  // start with 0.0 balance
  30. //                {
  31. //                        this.firstName = fn;
  32. //                        this.lastName = ln;
  33. //                        this.balance = 0.0;
  34. //                }

  35.                 public BankCustomer(string fn, string ln)  // start with 0.0 balance
  36.                         : this(fn, ln, 0.0)
  37.                 { }


  38.                 public double Balance
  39.                 {
  40.                         get
  41.                         {
  42.                                 return this.balance;
  43.                         }
  44.                         set
  45.                         {
  46.                                 if(value<0.0)
  47.                                         System.Console.WriteLine("The amount under zero");
  48.                                 else
  49.                                         this.balance = value;
  50.                         }
  51.                 }//property


  52.                 public Person this [int index]
  53.                 {
  54.                         get
  55.                         {
  56.                                 return relatives[index];
  57.                         }
  58.                         set
  59.                         {
  60.                                 if (value != null)
  61.                                 {
  62.                                         relatives [index] = value;
  63.                                 }
  64.                         }
  65.                 }//indexer
  66.         }
  67. }
复制代码
回复 使用道具 举报
朱勋 黑马帝 2011-12-11 23:06:43
板凳
本帖最后由 朱勋 于 2011-12-11 23:16 编辑
王旭 发表于 2011-12-11 22:45
构造函数的操作,说白了就是在实例化对象时,按照不同的需要去构造特殊的对象。构造函数之间的调用应该说和 ...


构造函数调用够造函数就是出现在同一类当中,
  1. using System;

  2. namespace WindowsApplication2
  3. {
  4.         /// <summary>
  5.         /// Summary description for BankCustomer.
  6.         /// </summary>
  7.         public class BankCustomer
  8.         {
  9.                 public string   firstName;
  10.                 public string   lastName;
  11.                 private double   balance;
  12.                 private Person[] relatives;

  13.                 public BankCustomer()
  14.                 {
  15.                         //
  16.                         // TODO: Add constructor logic here
  17.                         //

  18.                         this.firstName = "";
  19.                         this.lastName = "";
  20.                         this.balance = 0.0;
  21.                         relatives = new Person[100];
  22.                 }

  23.                 public BankCustomer(string fn, string ln, double bal)
  24.                 {
  25.                         this.firstName = fn;
  26.                         this.lastName  = ln;
  27.                         this.balance   = bal;
  28.                 }

  29. //                public BankCustomer(string fn, string ln)  
  30. //                {
  31. //                        this.firstName = fn;
  32. //                        this.lastName = ln;
  33. //                        this.balance = 0.0;
  34. //                }

  35.                 public BankCustomer(string fn, string ln)  //调用了三个参数的构造函数
  36.                         : this(fn, ln, 0.0)
  37.                 { }


  38.                 public double Balance
  39.                 {
  40.                         get
  41.                         {
  42.                                 return this.balance;
  43.                         }
  44.                         set
  45.                         {
  46.                                 if(value<0.0)
  47.                                         System.Console.WriteLine("The amount under zero");
  48.                                 else
  49.                                         this.balance = value;
  50.                         }
  51.                 }//property


  52.                 public Person this [int index]
  53.                 {
  54.                         get
  55.                         {
  56.                                 return relatives[index];
  57.                         }
  58.                         set
  59.                         {
  60.                                 if (value != null)
  61.                                 {
  62.                                         relatives [index] = value;
  63.                                 }
  64.                         }
  65.                 }//indexer
  66.         }
  67. }
复制代码
回复 使用道具 举报
王旭 黑马帝 2011-12-11 23:22:42
报纸
http://www.cnblogs.com/goalbell/articles/922862.html
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马