您的位置:首页 > 编程语言 > C#

c#基础1

2015-10-26 14:19 513 查看
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.实现多态,增加可扩展性

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: