• 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 }
    View Code

    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 }
    View Code

    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 }
    View Code
     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 }
    View Code
     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 }
    View Code

    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 }
    View Code
      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 }
    View Code

    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 }
    View Code
     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 }
    View Code
  • 相关阅读:
    xargs命令
    grep命令详细解析 --非原创 原作者ggjucheng
    centos如何安装Python3
    Custom Draw 基础(转载)
    Make 命令教程(转载)
    选择Blobs (Evision)
    图像预处理(Evision)
    一个野生程序员开博日
    Ubuntu 14.04 apt源更新
    python核心编程3-13
  • 原文地址:https://www.cnblogs.com/liuslayer/p/4713392.html
Copyright © 2020-2023  润新知