继承中的构造函数的问题-base,this的用法
2012-08-18 23:27
549 查看
先写一个父类:
在写一个继承这个父类的子类
实例化子类并调用:
编译运行成功!证明我们的写法是没有问题的!
这个时候我们修改下父类的代码:
这个时候编译就会出错:提示缺少一个无参数的构造函数:
少什么我们就先加什么:那么我们就给父类增加一个无参数的构造函数
我们在编译,提示编译成功!
证明在调用子类的构造函数会默认去调用父类中的无参数的构造函数!继承的时候,构造函数不能被继承
但是当父类中不存在无参数的构造函数呢!
这个时候我们就可以使用base方法,在子类中制定要调用父类的构造函数,这样子类就不会默认的去调用父类中无参数的构造函数,编译也不会报错
上面讲了Base的用法,下面我们将下this的用法!
先看一段代码
通过上面的代码你是不是发现一些问题:有很多重复的地方,一个类里面有很多种构造方法,如果每一个我们都给里面的参数赋值的话,显得非常的麻烦,通过this我们就能更好的优化这些代码,并且清晰明了
当然this还有很多其它的用法,不仅仅是这一种 ,不同的场合有不同的含义!本文仅介绍在构造函数中的用法!
class Person { public string Name { get; set; } public int Age { get; set; } }
在写一个继承这个父类的子类
class Student : Person { public Student(string name, int age, double score) { this.Name = name; this.Age = age; this.Score = score; } public double Score { get; set; } }
实例化子类并调用:
Student stu = new Student("张三", 18, 100);
编译运行成功!证明我们的写法是没有问题的!
这个时候我们修改下父类的代码:
class Person { public Person(string name, int age) { this.Name = name; this.Age = age; } public string Name { get; set; } public int Age { get; set; } }
这个时候编译就会出错:提示缺少一个无参数的构造函数:
少什么我们就先加什么:那么我们就给父类增加一个无参数的构造函数
class Person { public Person() { Console.WriteLine("Person类中的无参数的构造函数。。。。"); } public Person(string name, int age) { this.Name = name; this.Age = age; } public string Name { get; set; } public int Age { get; set; } }
我们在编译,提示编译成功!
证明在调用子类的构造函数会默认去调用父类中的无参数的构造函数!继承的时候,构造函数不能被继承
但是当父类中不存在无参数的构造函数呢!
class Person { public Person(string name, int age) { this.Name = name; this.Age = age; } public string Name { get; set; } public int Age { get; set; } }
这个时候我们就可以使用base方法,在子类中制定要调用父类的构造函数,这样子类就不会默认的去调用父类中无参数的构造函数,编译也不会报错
class Student : Person { //不修改父类,而是在子类的构造函数后面通过:base(),显示的去调用父类的某个构造函数。 public Student(string name, int age, double score) : base(name, age) //base的作用1:在子类中调用父类的构造函数。 { // this.Name = name; //this.Age = age; this.Score = score; } public double Score { get; set; } }
上面讲了Base的用法,下面我们将下this的用法!
先看一段代码
public class Person : MyClass { public Person(string name, int age, string email, double salary) { this.Name = name; this.Age = age; this.Email = email; this.Salary = salary; } public Person(string name) { this.Name = name; } public Person(string name, int age) { this.Name = name; this.Age = age; } public Person(string name, string email) { this.Name = name; this.Email = email; } public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } public double Salary { get; set; } }
通过上面的代码你是不是发现一些问题:有很多重复的地方,一个类里面有很多种构造方法,如果每一个我们都给里面的参数赋值的话,显得非常的麻烦,通过this我们就能更好的优化这些代码,并且清晰明了
public class Person : MyClass { public Person(string name, int age, string email, double salary) { this.Name = name; this.Age = age; this.Email = email; this.Salary = salary; } //this作用1:在当前类的构造函数后面通过:this来调用当前类自己的其他构造函数。 public Person(string name) : this(name, 0, null, 0) { // this.Name = name; } public Person(string name, int age) : this(name, age, null, 0) { //this.Name = name; //this.Age = age; } public Person(string name, string email) : this(name, 0, email, 0) { //this.Name = name; //this.Email = email; } public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } public double Salary { get; set; } }
当然this还有很多其它的用法,不仅仅是这一种 ,不同的场合有不同的含义!本文仅介绍在构造函数中的用法!
相关文章推荐
- c#语法糖代码——继承中构造函数的问题,this&base比较
- 构造函数中base和this的用法与区别
- 构造函数中base和this的用法与区别
- 构造函数中base和this的用法与区别
- c# 的继承,继承类实例化的时候,会先调用基类的构造函数,然后再调用自己的构造函数,base 的用法,有override和没有override的区别
- 关于PHP中构造函数以及子类继承父类构造函数的相关问题
- 构造函数 析构函数 的继承与虚拟问题 & 虚函数 纯虚函数
- 构造函数的继承问题
- Java中关于构造函数继承的相关问题
- c# abstract抽象类与继承类子类的构造函数_base
- 关于构造函数的重载 用this语句的调用问题
- python类继承时构造函数的执行问题
- base和this的用法
- iOS block-base 动画简单用法+关键帧动画设置线性变化速度的问题
- 关于有参构造函数的继承问题
- Java类继承中的构造函数问题
- c#学习-base和this在构造函数中的应用
- java this调用函数及构造函数问题
- Java语言深入-构造函数的继承问题
- 所有的页面都继承自定义的BasePage,而有一个特殊页面要继承BasePage,但不想执行父类构造函数调用的某些方法,怎么办?