您的位置:首页 > 编程语言 > C#

.Net学习笔记----2015-07-21(C#基础复习04,面向对象语法和继承复习)

2015-07-21 11:55 861 查看
面向对象:

1、封装、继承、多态

***字段:存储数据,访问修饰符应该设置为private 私有的

***属性:保护字段,对字段的取值和赋值进行限定

***new关键字: 1、在堆中开辟空间   2、在开辟的空间中创建对象  3、调用对象的构造函数

***this关键字:1、代表当前类的对象  2、显示的调用自己类里面的构造函数

***构造函数:初始化对象,当创建对象的时候会调用构造函数

***对字段的保护方法:

1、get()

2、set()

3、构造函数

***return的作用:

1、立即结束本次方法

2、在方法中返回要返回的值

示例代码:

class Person
{
//字段、属性、构造函数、方法、接口
//类内部的成员,如果不加访问修饰符,默认private
string _name;

public string Name
{
get { return _name; }
set
{
if (value != "刘备")
{
value = "刘备";
}
_name = value;
}
}
int _age;
public int Age
{
get
{
if (_age < 0 || _age > 100)
{
return 0;
}
return _age;
}
set { _age = value; }
}
char _gender;
public char Gender
{
get { return _gender; }
set { _gender = value; }
}
int _chinese;

public int Chinese
{
get { return _chinese; }
set { _chinese = value; }
}
int _math;

public int Math
{
get { return _math; }
set { _math = value; }
}
int _english;

public int English
{
get { return _english; }
set { _english = value; }
}

public Person() { }
public Person(string name, int age, char gender, int chinese, int math, int english)
{
this.Name = name;
this.Age = age;
this.Gender = gender;
this.Chinese = chinese;
this.Math = math;
this.English = english;
}
/// <summary>
/// 代码冗余
/// </summary>
/// <param name="name"></param>
/// <param name="age"></param>
/// <param name="gender"></param>
//public Person(string name, int age, char gender)
//{
//    this.Name = name;
//    this.Age = age;
//    this.Gender = gender;
//}

/// <summary>
/// this的另一种用法,显示调用自己的构造函数,这里调用的是上面全参的构造函数
/// </summary>
/// <param name="name"></param>
/// <param name="age"></param>
/// <param name="gender"></param>
public Person(string name, int age, char gender)
: this(name, age, gender, 0, 0, 0)
{

}
public Person(char gender)
{
if (gender != '男' && gender != '女')
{
gender = '男';
}
this.Gender = gender;
}
public void SayHello()
{
Console.WriteLine("{0}-----{1}-----{2}", this.Name, this.Age, this.Gender);
}
}


static void Main(string[] args)
{
Person p = new Person('中');
//给对象的每个属性赋值的过程,称之为对象的初始化
p.Name = "张三";
p.Age = -10;
//p.Gender = '中';
p.SayHello();

Console.ReadKey();
}


继承复习:

解决代码的冗余,实现多态,增加代码扩展性,便于维护

***继承的特性:

1、单根性

2、传递性

子类并没有继承父类的构造函数,而是会默认调用父类的无参的构造函数

class Program
{
static void Main(string[] args)
{
}
}

public class Person
{
public Person(string name, int age, char gender)
{

}
public string Name
{ get; set; }

public char Gender
{ get; set; }
public int Age
{ get; set; }
public void CHLSS()
{

}
}

public class Student : Person
{
//在子类中调用父类的构造函数 使用关键字base
public Student(string name, int age, char gender, int id)
: base(name, age, gender)
{
this.ID = id;
}

/// <summary>
/// 自动属性,本质和自己手写的一样,自动生成一个私有字段
/// </summary>

public int ID
{ get; set; }
}

public class Teacher : Person
{
public Teacher(string name, int age, char gender, double salary)
: base(name, age, gender)
{
this.Salary = salary;
}
public double Salary
{ get; set; }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: