面向对象——继承与构造函数

简介: 面向对象——继承与构造函数

继承的概念:让派生类(子类)拥有基类(父类)所有的功能。


实现:通过在派生类后加 “:”冒号,实现继承。好处:增加了代码的重用性。


如果一个类没有显示定义其父类,那么他的父类是System.Object,只能有一个基类。


创建派生类对象的时候,默认的情况的下,在派生类通过调用基类的默认构造(访问修饰符是public,并且没有任何的形参)函数来创建基类对象的。如果基类没有默认的构造,则需要在派生类中显示调用基类的构造函数。


方式:派生类构造函数():base(实参)


{}


构造函数:类构造函数,对象构造函数,派生类调用基类的构造函数。构造函数的作用:初始化字段,给字段赋值。


静态构造函数:静态构造函数不能有访问修饰符,必须是静态的。


静态构造函数不能有任何参数。静态构造函数有且仅有一个构造函数。静态构造函数只执行一次,调入内存后就不再执行。


例:


  Public class Person


             {

        Static Person(){}


}


Static的作用:在内存中创建类的实体。


注:没有指定基类,则基类默认为object


防止继承—密封类,关键字(sealed),该类就不能作为其他类的基类。  


例:给饭卡充钱:


   public class Peson


   {

       private ushort height;//字段


       public ushort Height


       {

           get


           {

               Console.WriteLine("调用Get方法");


               return height;


           }


           set


           {

               Console.WriteLine("调用Set方法");


               height = value;


           }


       }


       private string name;


       private byte age;


   }


   public class Card


   {

       private string owner;


       public string Owner


       {

           get { return owner; }


           set { owner = value; }


       }


       private decimal balance;


       public decimal Balance


       {

           get { return balance; }


           set { balance = value; }


       }


   }


   public class Teacher : Peson


   {

       public void FullMoney(Card card, decimal money)


       {

           card.Balance += money;


       }


   }


   public class Student : Peson


   {

       private Card myCard;


       public Card MyCard


       {

           get { return myCard; }


           set { myCard = value; }


       }


   }


   class Program


   {

       static void Main(string[] args)


       {

           Student s = new Student();


           s.Height = 15;


           Console.WriteLine(s.Height);


           Card card = new Card();


           s.MyCard = card;


           Teacher t = new Teacher();


           t.FullMoney(card, 100);


           Console.WriteLine(s.MyCard.Balance);


       }


   }


输出结果:


     调用Set方法


     调用Get方法


     15


     100



派生类调用基类的方法:1、如果派生类中没有基类的方法,可以直接调用该方法。


2、派生类中有该方法应该用base.方法名调用


例:public class CustomerAccount


   {

       public virtual decimal CalculatePrice()


       {

           return 0.0m;


       }


   }


   class GoldAccount : CustomerAccount


   {

       public override decimal CalculatePrice()


       {

           return base.CalculatePrice()*0.9m;


       }


}


构造函数举例:


1、public class Student


   {

       public Student()


       {

           Console.WriteLine("调用构造函数");


       }


   }


   class Program


   {

       static void Main(string[] args)


       {

           Student st = new Student();


       }


   }


输出结果:调用构造函数


例2、在继承中,创建对象的执行过程。


 public class Person


   {

       public Person()


       {

           Console.WriteLine("Peson");


       }


       public Person(string name)


       {

           Console.WriteLine("Name:"+name);


       }


   }


   public class Student:Person


   {

       public Student()


           : base()//当为无参时,可有可无,系统会自动添加


       {

           Console.WriteLine("调用构造函数");


       }


       public Student(string name)


           : base()//当基类有多个构造函数时,base用来指定调用的构造函数


       {

           Console.WriteLine("调用构造函数:姓名:"+name);


       }


       public Student(string name, int age)


           : base(name)//调用 Person(string name)构造函数


       {

           Console.WriteLine("调用构造函数:姓名:" + name+" 年龄:"+age);


       }


   }


   class Program


   {

       static void Main(string[] args)


       {

         Student st = new Student();


         Student st1 = new Student("xiaowang");


         Student st2 = new Student("xiaowang",12);


       }


   }


输出结果:


 Peson


 调用构造函数


Peson


调用构造函数:姓名:xiaowang


调用构造函数:姓名:xiaowang 年龄:12


目录
相关文章
|
1月前
面向对象编程:类、对象、继承与多态
面向对象编程:类、对象、继承与多态
17 0
|
1月前
|
算法 编译器 程序员
【C++入门到精通】C++入门 —— 继承(基类、派生类和多态性)
继承(inheritance)机制是面向对象程序设计使代码可以复用的最重要的手段,它允许程序员在保持原有类特性的基础上进行扩展,增加功能,这样产生新的类,称派生类。继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程。以前我们接触的复用都是函数复用,继承是类设计层次的复用。
84 0
|
1月前
|
存储 编译器 C++
C++类与对象【继承】
C++类与对象【继承】
|
1月前
|
Java 编译器
【JAVA面向对象编程】--- 探索子类如何继承父类
【JAVA面向对象编程】--- 探索子类如何继承父类
22 0
|
1月前
|
C++
『 C++类与对象』继承
『 C++类与对象』继承
|
10月前
|
Java
面向对象编程基础:类、对象、封装、继承和多态
面向对象编程基础:类、对象、封装、继承和多态
94 0
|
12月前
|
编译器 C++
C++类和对象-继承&多态
C++类和对象-继承&多态
55 0
|
编译器 C++
类和对象-继承
类和对象-继承
52 1
类和对象-继承
总结继承和多态的一些问题
本人学习了多态和继承后的一些问题,解答出来并总结,有些是容易搞混的概念。
总结继承和多态的一些问题
面向对象的封装,继承,多态(一)
面向对象的封装,继承,多态。
66 0
面向对象的封装,继承,多态(一)