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面向对象

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _01面向对象
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13            // string s;
14           //  Person sunQuan;//自定义类
15            // 创建Person类的对象
16             Person suQuan = new Person();
17             suQuan.Name = "孙全";
18             suQuan.Age = -23;
19             suQuan.Gender = ‘春‘;
20             suQuan.CHLSS();
21             Console.ReadKey();
22         }
23     }
24 }

02面向对象复习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _02面向对象复习
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Person p = new Person();
14             p.Age = -110;
15             p.Name = "zhangsan";
16             p.Gender = ‘中‘;
17             p.SayHello();
18
19             Person p2 = new Person();
20             p2.Name = "李四";
21             p2.Age = 88;
22             p2.Gender = ‘女‘;
23             p2.SayHello();
24             Console.ReadKey();
25         }
26     }
27 }

03静态和非静态的区别

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _03静态和非静态的区别
 8 {
 9     public class Person
10     {
11         private static string _name;
12
13         public static string Name
14         {
15             get { return Person._name; }
16             set { Person._name = value; }
17         }
18         private char _gender;
19
20         public char Gender
21         {
22             get { return _gender; }
23             set { _gender = value; }
24         }
25         public void M1()
26         {
27
28             Console.WriteLine("我是非静态的方法");
29         }
30         public static void M2()
31         {
32
33             Console.WriteLine("我是一个静态方法");
34         }
35     }
36 }

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _03静态和非静态的区别
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //调用实例成员
14             Person p = new Person();
15             p.M1();//实例方法
16             Person.M2();//静态方法
17            // Student s = new Student();
18
19
20             Console.WriteLine();
21             Console.ReadKey();
22         }
23     }
24 }

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _03静态和非静态的区别
 8 {
 9     public static class Student
10     {
11         private static string _name;
12
13         public static string Name
14         {
15             get { return Student._name; }
16             set { Student._name = value; }
17         }
18
19         public static void M1()
20         {
21             Console.WriteLine("Hello World");
22         }
23
24
25        // private int _age;
26
27
28     }
29 }

04面向对象练习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _04面向对象练习
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13
14             Student s = new Student("张三",100,100,100);
15             Console.ReadKey();
16             //Student zsStudent = new Student("张三", 18, ‘男‘, 100, 100, 100);
17
18             //zsStudent.SayHello();
19             //zsStudent.ShowScore();
20
21
22             //Student xlStudent = new Student("小兰", 16, ‘女‘, 50, 50, 50);
23
24             //xlStudent.SayHello();
25             //xlStudent.ShowScore();
26             //Console.ReadKey();
27
28
29
30         }
31     }
32 }

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6
  7 namespace _04面向对象练习
  8 {
  9     public class Student
 10     {
 11         //字段、属性、方法、构造函数
 12
 13        //析构函数  构造函数
 14
 15
 16
 17         //当程序结束的时候  析构函数才执行
 18         //帮助我们释放资源
 19         //GC Garbage Collection
 20         ~Student()
 21         {
 22             Console.WriteLine("我是析构函数");
 23         }
 24
 25
 26         public Student(string name, int age, char gender, int chinese, int math, int english)
 27         {
 28             this.Name = name;
 29             this.Age = age;
 30             this.Gender = gender;
 31             this.Chinese = chinese;
 32             this.Math = math;
 33             this.English = english;
 34         }
 35         public Student(string name, int chinese, int math, int english):this(name,0,‘c‘,chinese,math,english)
 36         {
 37             //this.Name = name;
 38             //this.Chinese = chinese;
 39             //this.Math = math;
 40             //this.English = english;
 41         }
 42         public Student(string name, int age, char gender)
 43         {
 44             this.Name = name;
 45             if (age < 0 || age > 100)
 46             {
 47                 age = 0;
 48             }
 49             this.Age = age;
 50             this.Gender = gender;
 51         }
 52
 53         public Student()
 54         {
 55
 56         }
 57
 58
 59
 60         private string _name;
 61         public string Name
 62         {
 63             get { return _name; }
 64             set { _name = value; }
 65         }
 66         private int _age;
 67         public int Age
 68         {
 69             get { return _age; }
 70             set
 71             {
 72                 if (value < 0 || value > 100)
 73                 {
 74                     value = 0;
 75                 }
 76                 _age = value;
 77             }
 78         }
 79         private char _gender;
 80         public char Gender
 81         {
 82             get
 83             {
 84                 if (_gender != ‘男‘ && _gender != ‘女‘)
 85                 {
 86                     return _gender = ‘男‘;
 87                 }
 88                 return _gender;
 89             }
 90             set { _gender = value; }
 91         }
 92         private int _chinese;
 93         public int Chinese
 94         {
 95             get { return _chinese; }
 96             set { _chinese = value; }
 97         }
 98         private int _math;
 99         public int Math
100         {
101             get { return _math; }
102             set { _math = value; }
103         }
104         private int _english;
105         public int English
106         {
107             get { return _english; }
108             set { _english = value; }
109         }
110
111
112         public void SayHello()
113         {
114             Console.WriteLine("我叫{0},我几年{1}岁了,我是{2}生", this.Name, this.Age, this.Gender);
115         }
116
117         public void ShowScore()
118         {
119             Console.WriteLine("我叫{0},我的总成绩是{1},平均成绩是{2}", this.Name, this.Chinese + this.Math + this.English, (this.Chinese + this.Math + this.English) / 3);
120         }
121
122     }
123 }

05练习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _05练习
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Ticket t = new Ticket(150);
14             t.ShowTicket();
15             Console.ReadKey();
16         }
17     }
18 }

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _05练习
 8 {
 9     public class Ticket
10     {
11         //写一个Ticket类,有一个距离属性(本属性只读,在构造方法中赋值),
12         //不能为负数,有一个价格属性,价格属性只读,
13         //并且根据距离distance计算价格Price (1元/公里):
14         //        0-100公里        票价不打折
15         //101-200公里    总额打9.5折
16         //201-300公里    总额打9折
17         //300公里以上    总额打8折
18
19         private double _distance;
20         public double Distance
21         {
22             get { return _distance; }
23         }
24
25         public Ticket(double distance)
26         {
27             if (distance < 0)
28             {
29                 distance = 0;
30             }
31             this._distance = distance;
32         }
33
34         private double _price;
35         //        0-100公里        票价不打折
36         //101-200公里    总额打9.5折
37         //201-300公里    总额打9折
38         //300公里以上    总额打8折
39         public double Price
40         {
41             get
42             {
43                 if (_distance > 0 && _distance <= 100)
44                 {
45                     return _distance * 1.0;
46                 }
47                 else if (_distance >= 101 && _distance < 200)
48                 {
49                     return _distance * 0.95;
50                 }
51                 else if (_distance >= 201 && _distance < 300)
52                 {
53                     return _distance * 0.9;
54                 }
55                 else
56                 {
57                     return _distance * 0.8;
58                 }
59             }
60         }
61
62
63         public void ShowTicket()
64         {
65             Console.WriteLine("{0}公里需要{1}元",Distance,Price);
66         }
67
68     }
69 }

时间: 2024-07-29 09:28:54

0505.Net基础班第九天(面向对象处级)的相关文章

0505.Net基础班第二十一天(基础加强总复习)

1.取消播放器的自动播放功能 2.播放或者暂停按钮 3.下一曲.上一曲 4.多选删除 5.静音和放音 6.选择列表中的音乐文件,单击播放按钮直接播放 7.自动进行下一曲 15秒  44秒 当我和世界不一样 44.--47 那就让我不一样 lblInfomation.Text = musicPlayer.currentMedia.duration.ToString() + "\r\n" + musicPlayer.currentMedia.durationString + "\

0505.Net基础班第十三天(面向对象多态)

1.c#中的访问修饰符 public :公开的公共的 private:私有的,只能在当前类的内部访问 protected:受保护的,只能在当前类的内部以及该类的子类中访问. internal:只能在当前项目中访问.在同一个项目中,internal和public的权限是一样. protected internal:protected+internal 1).能够修饰类的访问修饰符只有两个:public.internal. 2).可访问性不一致. 子类的访问权限不能高于父类的访问权限,会暴漏父类的成

0505.Net基础班第十天(面向对象继承)

1.命名空间 可以认为类是属于命名空间的. 如果在当前项目中没有这个类的命名空间,需要我们手动的导入这个类所在的 命名空间. 1).用鼠标去点 2).alt+shift+F10 3).记住命名空间,手动的去引用 2.在一个项目中引用另一个项目的类 1).添加引用 2).引用命名空间 3.值类型和引用类型 区别: 1.值类型和引用类型在内存上存储的地方不一样. 2.在传递值类型和传递引用类型的时候,传递的方式不一样. 值类型我们称之为值传递,引用类型我们称之为引用传递. 我们学的值类型和引用类型:

0505.Net基础班第十一天(面向对象继承)

1.里氏转换 1).子类可以赋值给父类 2).如果父类中装的是子类对象,那么可以讲这个父类强转为子类对象. 2. 子类对象可以调用父类中的成员,但是父类对象永远都只能调用自己的成员. 3. is:表示类型转换,如果能够转换成功,则返回一个true,否则返回一个false as:表示类型转换,如果能够转换则返回对应的对象,否则返回一个null 4.protected 受保护的:可以在当前类的内部以及该类的子类中访问. 5.ArrayList集合的长度问题 每次集合中实际包含的元素个数(count)

0505.Net基础班第十二天(面向对象多态)

1.绝对路径和相对路径 绝对路径:通过给定的这个路径直接能在我的电脑中找到这个文件. 相对路径:文件相对于应用程序的路径. 结论: 我们在开发中应该去尽量的使用相对路径. 2.装箱.拆箱 装箱:就是将值类型转换为引用类型. 拆箱:将引用类型转换为值类型. 看两种类型是否发生了装箱或者拆箱,要看,这两种类型是否存在继承关系. 3.将创建文件流对象的过程写在using当中,会自动的帮助我们释放流所占用的资源. 4.实现多态的手段 1).虚方法 步骤: 1.将父类的方法标记为虚方法 ,使用关键字 vi

0505.Net基础班第二十天(基础加强总复习)

1.new关键字 1).创建对象 2).隐藏从父类那里继承过来的成员 2.访问修饰符 public:公开的,公共的 private:私有的,只能在当前类的内部访问,类中成员们,如果不加访问修饰符,默认就是private procteced:受保护的,可以在当前类的内部访问,也可以在该类的子类中访问 internal:在当前项目中都可以访问. protected internal: 能够修饰类的访问修饰符只有两个,internal和public 3.常用的关键字 this 1.当前类的对象 2.调

0505.Net 基础班第十七天(GDI绘图)

基础知识复习+练习(带*为选做) 以下练习题做的时候如果遇到不会做的,请跳过本题继续向后做. 编写一段程序,运行时向用户提问“你考了多少分?(0~100)”,接受输入后判断其等级并显示出来.判断依据如下:等级={优 (90~100分):良 (80~89分):中 (60~69分):差 (0~59分):} 编程输出九九乘法表. 定义长度50的数组,随机给数组赋值,并可以让用户输入一个数字n,按一行n个数输出数组  int[]  array = new int[50];     Random r =

0505.Net基础班第六天(复杂数据类型)

1.变量类型 int double string char bool decimal 变量的使用规则:先声明再赋值最后使用 int number; number=10;  number=20; Console.WriteLine(number); 2.Camel  Pascal 3.运算符 赋值运算符:= 复合赋值运算符:+= -= *= /= %=  sum+=age;  sum=sum+age; 算数运算符: + - * / % ++ -- 关系运算符: > < >= <= =

0505.Net基础班第二十二天(委托、XML和播放器项目)

01索引器 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _01索引器 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Person p = new Person(); 14 p