c#基础1

1.语法规范

注释:单行注释//  多行注释/*  */  文档注释///

命名规范:Camel(骆驼命名规范)常用于变量、字段  首单词的首字母大写,其余单词首字母大写 注:字段名一般以下划线开头

                    PasCal:常用于方法、类      例:SumMax

2.简单工厂和抽象类

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

namespace 模拟磁盘打开文件
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("请输入要进入的磁盘");
                string path = Console.ReadLine();
                Console.WriteLine("请选择要打开的文件");
                string fileName = Console.ReadLine();
                FileFather ff = GetFile(fileName, path + fileName);
                ff.OpenFile();//父类对象中装子类
                Console.ReadKey();
            }
        }

        public static FileFather GetFile(string fileName,string fullPath)
        {
            string extension = Path.GetExtension(fileName);//引入命名空间快捷键Alt+Shift+F10
            FileFather ff = null;
            switch(extension)
            {
                case ".txt":ff = new TxtPath(fullPath);
                    break;
                case ".jpg":ff = new JpgPath(fullPath);
                    break;
                case ".wmv":ff = new WmvPath(fullPath);
                    break;
            }
            return ff;
        }
    }
    public abstract class FileFather
    {
        public string fileName
        {
            get;
            set;
        }
        public FileFather(string fileName)
        {
            this.fileName = fileName;
        }
        public abstract void OpenFile();
    }

    public class TxtPath : FileFather
    {
        public TxtPath(string fileName) : base(fileName)
        {

        }
        
        public override void OpenFile()
        {
            ProcessStartInfo psi = new ProcessStartInfo(this.fileName);
            Process p = new Process();
            p.StartInfo = psi;
            p.Start();
        }
    }
    public class JpgPath : FileFather
    {
        public JpgPath(string fileName) : base(fileName)
        {

        }
        public override void OpenFile()
        {
            ProcessStartInfo psi = new ProcessStartInfo(this.fileName);
            Process p = new Process();
            p.StartInfo = psi;
            p.Start();
        }
    }

    public class WmvPath : FileFather
    {
        public WmvPath(string fileName) : base(fileName)
        {

        }
        public override void OpenFile()
        {
            ProcessStartInfo psi = new ProcessStartInfo(this.fileName);
            Process p = new Process();
            p.StartInfo = psi;
            p.Start();
        }
    }
}
整个体系有点不太明白。。。


进程类的使用

            ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\Administrator\Desktop\1.txt");
            Process p = new Process();
            p.StartInfo = psi;
            p.Start();


3.面向对象

三个特点:封装、继承、多态

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

namespace 面向对象
{
    class Person
    {
        //字段、属性、构造函数、方法、接口
        //类内部成员 默认用private修饰
        string _name;
        //字段:存贮数据,应该用private修饰
        public string Name
        {
            get { return _name; }
            set { _name=value; }
        }
        //属性:保护字段  可以在get,set中对值得范围进行限制
        //new:堆中开辟空间  建立对象  调用构造函数
        int _age;
        public int Age
        {
            get
            {
                return _age;
            }

            set
            {
                _age = value;
            }
        }
        string _gender;
        public string Gender//生成属性快捷键Ctrl+R+E;可以使用自动属性,会自动生成字段
        {
            get
            {
                return _gender;
            }

            set
            {
                _gender = value;
            }
        }     
        
        public Person()//构造函数可以重载,默认为无参的
        {

        }
        //构造函数:创建对象的时候调用构造函数
        //对字段的保护1.set 2.get 3.构造函数
        //this 1.指向当前的对象 2.调用全参的构造函数
        public void sayHello()
        {
            Console.WriteLine("{0}----{1}----{2}", this.Name, this.Age, this.Gender);
        }
    }
    //继承:1.解决代码冗余问题 2.实现多态,增加可扩展性

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值