C#系列-面向对象

00解释

1、面向过程-----> 面向对象

面向过程:面向的是完成这件事儿的过程,强调的是完成这件事儿的动作。

把大象塞进冰箱里
1、打开冰箱门
2、把大象塞进去,亲下大象的屁股
3、关闭冰箱门

孙全 瘦小 矮  小屌丝
孙全踩着小板凳打开冰箱门
孙全找翟盼盼帮忙把大象塞进冰箱里,孙全踩着板凳去亲。
孙全踩着板凳关闭冰箱门

翟盼盼  190cm  非常大力气
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)、静态类在整个项目中资源共享。
只有在程序全部结束之后,静态类才会释放资源。


7、构造函数

作用:帮助我们初始化对象(给对象的每个属性依次的赋值)
构造函数是一个特殊的方法:
1)、构造函数没有返回值,连void也不能写。
2)、构造函数的名称必须跟类名一样。

创建对象的时候会执行构造函数
构造函数是可以有重载的。
***
类当中会有一个默认的无参数的构造函数,当你写一个新的构造函数之后,不管是有参数的还是
无参数的,那个默认的无参数的构造函数都被干掉了。


8、new关键字

Person zsPerson=new Person();
new帮助我们做了3件事儿:
1)、在内存中开辟一块空间
2)、在开辟的空间中创建对象
3)、调用对象的构造函数进行初始化对象


9、this关键字

1)、代表当前类的对象
2)、在类当中显示的调用本类的构造函数  :this

一、面向对象

Program.cs 

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();
        }
    }
}

Person.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01面向对象
{
    public class Person
    {
        private string _name;
        public string Name
        {
            //当你输出属性的值得时候 会执行get方法
            get { return _name; }
            //当你给属性赋值的时候 首先会执行set方法
            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; }
        }
        public void CHLSS()
        {
            Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生,我可以吃喝拉撒睡哟~~~", this.Name, this.Age, this.Gender);
        }
    }
}

二、面向对象part2

Program.cs 

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();
        }
    }
}

Person.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02面向对象复习
{
    public class Person
    {
        //字段、属性、方法
        string _name;
        public string Name
        {
            get
            {
                if (_name != "孙全")
                {
                    return _name = "孙全";
                }
                return _name;
            }
            set { _name = value; }
        }
        int _age;
        public int Age
        {
            get { return _age; }
            set {

                if (value < 0 || value > 100)
                {
                    value = 0;
                }
                _age = value; }
        }
        char _gender;
        public char Gender
        {
            get {
                if (_gender != '男' && _gender != '女')
                {
                    return _gender = '男';
                }
                
                return _gender; }
            set { _gender = value; }
        }


        public void SayHello()
        {
            Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生", this.Name, this.Age,this.Gender);
        }



    }
}

三、静态与非静态区别

 Program.cs 

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();
        }
    }
}

Person.cs

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("我是一个静态方法");
        }
    }
}

Student.cs

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;


    }
}

 四、面向对象练习一

Program.c

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();



        }
    }
}

Student.c 

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);
        }

    }
}

 四、面向对象练习二

Program.c

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();
        }
    }
}

Ticket.c 

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);
        }

    }
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南叔先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值