面向对象编程

一 概念

面向对象编程 :Object Oriented Programming

Object : 物体

Oriented :面向

Programming:编程

万物皆对象 每个物体都是一个单独的对象。

编程方式有两种

  1. 面向过程编程

  2. 面向对象编程

可以理解为模块化编程

面向对象和面向过程编程的区别

面向对象编程:适合做中大型项目。

代码分析

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace oop
{
    //创建一个类
    internal class Computer
    {
​
        //属性
        public string brand;   // 品牌
        public string type;    //型号
        public string color;   //颜色
        public int size;       //大小   多少英寸
        public string date;    // 购买日期
        public int price;      //价格
​
​
        //方法  可以做什么  行为
        public void RunGame()
        {
            Console.WriteLine("当前电脑的品牌是 "+brand);
            Console.WriteLine("当前电脑的型号是 "+type);
            Console.WriteLine("当前电脑的颜色是 "+color);
            //输出一些内容
            Console.WriteLine("电脑正在运行 CS Go 游戏");
        }
​
        public void playMusic()
        {
            Console.WriteLine("电脑正在播放'大意了,没有闪'音乐");
        }
        //方法可以一直写下去
        //根据题意编写对应的属性和方法
​
    }
}
​
​
//=====================================================================
​
​
​
using oop;
using System.Collections;
​
internal class Program
{
    private static void Main(string[] args)
    {
       //刚才创建了一个  模板(类)
       //根据模板 (类) 创建 对象
        Computer computer = new Computer();
​
        //给对象的各个属性赋值
        computer.brand = "华为";
        computer.type = "mate pad 10";
        computer.color = "粉色";
        computer.size = 16;
        computer.date = "2020-12-12";
        computer.price = 9999;
​
        //展示此电脑的信息
       // computer.ShowInfo();
​
        //调用对象的方法
        computer.RunGame();    //华为 
​
        //=====创建第二个对象
        Computer computer2 = new Computer();
​
        //给对象的各个属性赋值
        computer2.brand = "小米";
        computer2.type = "小米Book16";
        computer2.color = "黑色";
        computer2.size = 16;
        computer2.date = "2021-12-12";
        computer2.price = 19999;
​
        //展示此电脑的信息
        // computer.ShowInfo();
​
        //调用对象的方法
        computer2.RunGame(); // 小米?
​
        add(2,3);
        add(3,4);
​
    }
​
    public static void add(int a, int b)
    {
        int c = a + b;
        Console.WriteLine(c);
    }
}
​
​
computer  和  computer2 是完全独立的;互不干扰的;没有联系的。
这两个对象的模板是一样的。
​
​

二 类的定义

类是由属性和方法组成的

属性:描述物体的特征

描述一下自己的笔记本电脑

属性:品牌,具体的型号,外观,颜色,大小,购买日期,价格,配置--------

方法:行为

描述你的好朋友:

属性:性别 , 身高,体重,年龄,五官,发型----------

方法:打游戏 看美女 打篮球

方法:物体的行为

练习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace oop
{
    internal class Computer
    {
​
        //属性
        string brand;   // 品牌
        string type;    //型号
        string color;   //颜色
        int size;       //大小   多少英寸
        string date;    // 购买日期
        int price;      //价格
​
        //方法  可以做什么  行为
        public void RunGame()
        {
            //输出一些内容
            Console.WriteLine("电脑正在运行 CS Go 游戏");
        }
​
        public void playMusic()
        {
            Console.WriteLine("电脑正在播放'大意了,没有闪'音乐");
        }
​
    }
}
​

扩充: 访问修饰符

常见的 访问修饰符

public: ( 公共的) 这是最宽松的访问级别。 使用 public 修饰的成员可以在任何地方被调用。

protected: (受保护的) 使用 protected 修饰的成员只能被同一类、派生类(子类)中的代码访问。不能从类的外部直接访问。

internal : (内部的) 使用 internal 修饰的成员 只能在同一个程序集内部被访问。

private: (私有的) 这是最严格的访问级别。 使用 private修饰的成员只能在声明它的类的内部使用

在内部类中,如果成员 没有修饰符,那么它有一个默认修饰符: private。

三 类和对象的区别

类是一个模板;对象是类的具体实现

 public class Book
    {
​
        public string Name;
        public string Description;
        public string Author;
        public string dates;
        public int count;
        public Book() { }
​
​
        public void BorrowBook()
        {
            Console.WriteLine("实现借书的功能");
        }
​
        public void BackBook()
        {
            Console.WriteLine("归还图书");
        }
​
    }

四 创建对象和对象的使用

 Book book = new Book();
        book.Name = "Test";
​
        Console.WriteLine(book.Name);
        

五 无参构造函数和有参构造函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace oop
{
    //创建一个类
    internal class Computer
    {
​
        //属性
        public string brand;   // 品牌
        public string type;    //型号
        public string color;   //颜色
        public int size;       //大小   多少英寸
        public string date;    // 购买日期
        public int price;      //价格
​
​
        //方法  可以做什么  行为
        public void RunGame()
        {
            Console.WriteLine("当前电脑的品牌是 "+brand);
            Console.WriteLine("当前电脑的型号是 "+type);
            Console.WriteLine("当前电脑的颜色是 "+color);
            //输出一些内容
            Console.WriteLine("电脑正在运行 CS Go 游戏");
        }
​
        public void playMusic()
        {
            Console.WriteLine("电脑正在播放'大意了,没有闪'音乐");
        }
        //方法可以一直写下去
        //根据题意编写对应的属性和方法
​
​
        //构造方法   构造函数
        //如果没有无参构造函数,程序在运行的时候会默认提供
        //如果我们自己定义的有无参构造函数,程序就会执行我们的构造函数
        public  Computer()
        {
            Console.WriteLine("++++++++++++++");
        }
​
        //有参构造函数
        public Computer(string b, string t)
        {
            brand = b;
            type = t;
        }
​
        public Computer(string brand, string type, string color, int size, string date, int price) : this(brand, type)
        {
            this.color = color;
            this.size = size;
            this.date = date;
            this.price = price;
        }
    }
}
​
​
//========================
​
  Computer computer4 = new Computer("苹果", "macbook","银色",
            16,"2021-11-11",10000);
        computer4.RunGame();

this 的使用

 //有参构造函数
        public Computer(string b, string t)
        {
            brand = b;
            type = t;
        }
​
        public Computer(string brand, string type, string color, int size, string date, int price) : this(brand, type)
        {
            //把 局部变量color 的值给当前对象的color属性
            //this == 当前对象的
            this.color = color;
            this.size = size;
            this.date = date;
            this.price = price;
        }

注意:

  1. 如果类 没有任何构造函数, 那么程序在运行的时候会默认提供一个无参的构造函数

  2. 如果类有无参的构造函数,那么程序就不再提供

  3. 如果类只有有参的构造函数,那么程序也不再提供无参构造函数

六 get/set 方法

 public class Book
    {
​
        private string Name;
        public string Description;
        public string Author;
        public string dates;
        public int count;
​
        public string Name1 { get => Name; set => Name = value; }
​
        public Book() {
            Console.WriteLine("=====无参数的构造函数====");
​
        }
     }
     
     
     //第二种方式
      private int _count;
​
        public int Count
        {
            get { return _count; }
            set { _count = value; }
        }
        
        
     //第三种方式
     public int Age{get;set;}
     这种情况下,自动创建  private int age 成员。
        
        

七 访问修饰符

public
  • 成员可以被任何其他类访问,无论这些类位于哪个命名空间或程序集中。

  • 这是最宽松的访问级别。

private
  • 成员只能被其所在的类内部访问。

  • 这是最严格的访问级别,用于隐藏类的内部实现细节。

protected
  • 成员只能被其所在的类及其派生类访问。

  • 这对于实现基类和派生类之间的共享逻辑非常有用。

internal
  • 成员只能在其所在的程序集(Assembly)内部被访问。

  • 这对于在同一个程序集中但不同命名空间中的类之间的共享很有用。

八 匿名类型

var age=45
​
初始化数据的时候,类型会确定
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值