0505.Net基础班第九天(面向对象处级)
1、面向过程-----> 面向对象
面向过程:面向的是完成这件事儿的过程,强调的是完成这件事儿的动作。
把大象塞进冰箱里 1、打开冰箱门 2、把大象塞进去,亲下大象的屁股 3、关闭冰箱门
孙全 瘦小 矮 小屌丝 孙全踩着小板凳打开冰箱门 孙全找翟盼盼帮忙把大象塞进冰箱里,孙全踩着板凳去亲。 孙全踩着板凳关闭冰箱门
翟盼盼 190cm 非常大力气 1、翟自己就能打开冰箱门 2、翟自己将大象塞进冰箱里,翟可以自己亲一下。 3、翟自己关闭冰箱门
如果我们用面向过程的思想来解决这件事儿,当执行这件事的人的不同的时候, 我们需要为每个不同的人量身定做解决事情的方法。
面向对象:找个对象帮你做事儿。 把大象塞进冰箱里 我们把冰箱作为对象: 1、冰箱门可以被打开 2、大象可以被塞进冰箱里 3、冰箱门可以被关闭
孙全 孙全 1 孙全 2 孙全 3
翟盼盼 翟 1 翟 2 翟 3
面向对象:意在写出一个通用的代码,屏蔽差异。
关门 面向过程:关门 张三 一脚把门踹紧了 李四 轻轻的把门带上了 王五 门没关严,留了个尾巴
面向对象:关门 门可以被关闭
我在黑板上画了一个零星 将 圆圈作为对象 圆圈可以被画在黑板上
将 黑板作为对象 黑板可以被画圆
我在黑板上画了一个圆 张三上来的 哪尺子比这画了一个特别圆的圆 李四上来的 随便一划拉 王五上来了 圆规画了一个圆 圆可以被画在黑板上 圆圈可以被画在黑板上 将圆圈作为对象:这个圆圈可以被画在黑板上。 黑板可以被画圆 黑板作为一个对象:可以被画圆圈 被画方块 被画正方向
试着描述孙全和颜XX的特征和行为 姓名:孙全 性别:男 身高:180cm 体重:70kg 年龄:22岁 吃喝拉撒睡一切正常 健康 吃喝嫖赌抽
姓名:颜XX 性别:男 身高:180cm 体重:70KG 年龄:23岁 脑残 身体一切健康
我们在代码中描述一个对象,通过描述这个对象的属性和方法 对象必须是看得见摸得着的
灯:属性和方法 属性: 外形:长的 亮度:500W 颜色:白色 牌子:XX 方法:发光
电风扇:属性、方法 外形:三个扇叶 颜色:白色 品牌:XX 方法:转动,扇风
我们把这些具有相同属性和相同方法的对象进行进一步的封装,抽象出来 类这个概念。 类就是个模子,确定了对象应该具有的属性和方法。 对象是根据类创建出来的。 类就是一个盖大楼的图纸 对象 就是盖出来的大楼。
2、类 语法: [public] class 类名 { 字段; 属性; 方法; } 写好了一个类之后,我们需要创建这个类的对象, 那么,我们管创建这个类的对象过程称之为类的实例化。 使用关键字 new.
this:表示当前这个类的对象。 类是不占内存的,而对象是占内存的。
3、属性 属性的作用就是保护字段、对字段的赋值和取值进行限定。 属性的本质就是两个方法,一个叫get()一个叫set()。 既有get()也有set()我们诚之为可读可写属性。 只有get()没有set()我们称之为只读属性 没有get()只有set()我们称之为只写属性
Field字段 Method方法 Property属性
****字段就是女人 属性才是男人。
4、访问修饰符 public:公开的公共的,在哪都能访问。 private:私有的,只能在当前类的内部进行访问,出了这个类就访问不到了。
5、 当我们创建好一个类的对象后,需要给这个对象的每个属性去赋值。 我们管这个过程称之为对象的初始化。
6、静态和非静态的区别 1)、在非静态类中,既可以有实例成员,也可以有静态成员。 2)、在调用实例成员的时候,需要使用对象名.实例成员; 在调用静态成员的时候,需要使用类名.静态成员名; 总结:静态成员必须使用类名去调用,而实例成员使用对象名调用。 静态函数中,只能访问静态成员,不允许访问实例成员。 实例函数中,既可以使用静态成员,也可以使用实例成员。 静态类中只允许有静态成员,不允许出现实例成员。
使用: 1)、如果你想要你的类当做一个"工具类"去使用,这个时候可以考虑将类写成静态的。 2)、静态类在整个项目中资源共享。 只有在程序全部结束之后,静态类才会释放资源。
堆 栈 静态存储区域
释放资源。GC Garbage Collection垃圾回收器
7、构造函数 作用:帮助我们初始化对象(给对象的每个属性依次的赋值) 构造函数是一个特殊的方法: 1)、构造函数没有返回值,连void也不能写。 2)、构造函数的名称必须跟类名一样。
创建对象的时候会执行构造函数 构造函数是可以有重载的。 *** 类当中会有一个默认的无参数的构造函数,当你写一个新的构造函数之后,不管是有参数的还是 无参数的,那个默认的无参数的构造函数都被干掉了。 8、new关键字 Person zsPerson=new Person(); new帮助我们做了3件事儿: 1)、在内存中开辟一块空间 2)、在开辟的空间中创建对象 3)、调用对象的构造函数进行初始化对象
9、this关键字 1)、代表当前类的对象 2)、在类当中显示的调用本类的构造函数 :this
01面向对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _01面向对象
{
class Program
{
static void Main(string[] args)
{
// string s;
// Person sunQuan;//自定义类
// 创建Person类的对象
Person suQuan = new Person();
suQuan.Name = "孙全";
suQuan.Age = -23;
suQuan.Gender = '春';
suQuan.CHLSS();
Console.ReadKey();
}
}
}
02面向对象复习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _02面向对象复习
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Age = -110;
p.Name = "zhangsan";
p.Gender = '中';
p.SayHello();
Person p2 = new Person();
p2.Name = "李四";
p2.Age = 88;
p2.Gender = '女';
p2.SayHello();
Console.ReadKey();
}
}
}
03静态和非静态的区别
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03静态和非静态的区别
{
public class Person
{
private static string _name;
public static string Name
{
get { return Person._name; }
set { Person._name = value; }
}
private char _gender;
public char Gender
{
get { return _gender; }
set { _gender = value; }
}
public void M1()
{
Console.WriteLine("我是非静态的方法");
}
public static void M2()
{
Console.WriteLine("我是一个静态方法");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03静态和非静态的区别
{
class Program
{
static void Main(string[] args)
{
//调用实例成员
Person p = new Person();
p.M1();//实例方法
Person.M2();//静态方法
// Student s = new Student();
Console.WriteLine();
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03静态和非静态的区别
{
public static class Student
{
private static string _name;
public static string Name
{
get { return Student._name; }
set { Student._name = value; }
}
public static void M1()
{
Console.WriteLine("Hello World");
}
// private int _age;
}
}
04面向对象练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04面向对象练习
{
class Program
{
static void Main(string[] args)
{
Student s = new Student("张三",100,100,100);
Console.ReadKey();
//Student zsStudent = new Student("张三", 18, '男', 100, 100, 100);
//zsStudent.SayHello();
//zsStudent.ShowScore();
//Student xlStudent = new Student("小兰", 16, '女', 50, 50, 50);
//xlStudent.SayHello();
//xlStudent.ShowScore();
//Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04面向对象练习
{
public class Student
{
//字段、属性、方法、构造函数
//析构函数 构造函数
//当程序结束的时候 析构函数才执行
//帮助我们释放资源
//GC Garbage Collection
~Student()
{
Console.WriteLine("我是析构函数");
}
public Student(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;
}
public Student(string name, int chinese, int math, int english):this(name,0,'c',chinese,math,english)
{
//this.Name = name;
//this.Chinese = chinese;
//this.Math = math;
//this.English = english;
}
public Student(string name, int age, char gender)
{
this.Name = name;
if (age < 0 || age > 100)
{
age = 0;
}
this.Age = age;
this.Gender = gender;
}
public Student()
{
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
public int Age
{
get { return _age; }
set
{
if (value < 0 || value > 100)
{
value = 0;
}
_age = value;
}
}
private char _gender;
public char Gender
{
get
{
if (_gender != '男' && _gender != '女')
{
return _gender = '男';
}
return _gender;
}
set { _gender = value; }
}
private int _chinese;
public int Chinese
{
get { return _chinese; }
set { _chinese = value; }
}
private int _math;
public int Math
{
get { return _math; }
set { _math = value; }
}
private int _english;
public int English
{
get { return _english; }
set { _english = value; }
}
public void SayHello()
{
Console.WriteLine("我叫{0},我几年{1}岁了,我是{2}生", this.Name, this.Age, this.Gender);
}
public void ShowScore()
{
Console.WriteLine("我叫{0},我的总成绩是{1},平均成绩是{2}", this.Name, this.Chinese + this.Math + this.English, (this.Chinese + this.Math + this.English) / 3);
}
}
}
05练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _05练习
{
class Program
{
static void Main(string[] args)
{
Ticket t = new Ticket(150);
t.ShowTicket();
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _05练习
{
public class Ticket
{
//写一个Ticket类,有一个距离属性(本属性只读,在构造方法中赋值),
//不能为负数,有一个价格属性,价格属性只读,
//并且根据距离distance计算价格Price (1元/公里):
// 0-100公里 票价不打折
//101-200公里 总额打9.5折
//201-300公里 总额打9折
//300公里以上 总额打8折
private double _distance;
public double Distance
{
get { return _distance; }
}
public Ticket(double distance)
{
if (distance < 0)
{
distance = 0;
}
this._distance = distance;
}
private double _price;
// 0-100公里 票价不打折
//101-200公里 总额打9.5折
//201-300公里 总额打9折
//300公里以上 总额打8折
public double Price
{
get
{
if (_distance > 0 && _distance <= 100)
{
return _distance * 1.0;
}
else if (_distance >= 101 && _distance < 200)
{
return _distance * 0.95;
}
else if (_distance >= 201 && _distance < 300)
{
return _distance * 0.9;
}
else
{
return _distance * 0.8;
}
}
}
public void ShowTicket()
{
Console.WriteLine("{0}公里需要{1}元",Distance,Price);
}
}
}