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.实现多态,增加可扩展性
}
注释:单行注释// 多行注释/* */ 文档注释///
命名规范: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.实现多态,增加可扩展性
}
相关文章推荐
- C#字符串格式化(摘抄的,留下来用用)
- (原创)c#学习笔记05--变量的更多内容02--复杂的类型变量02--结构
- C#计算两个时间年份月份差
- (原创)c#学习笔记05--变量的更多内容02--复杂的类型变量01--枚举
- c#正则表达式
- C#四舍五入保留一位小数
- C# winform 判断日期大小 比较日期大小
- c# resources
- C#代码生成器
- 将C#文档注释生成.chm帮助文档
- C# Access修改字段说明
- C#中用SharpZipLib生成gzip/解压文件
- C#多线程技术总结(异步)
- C# ListView得到选中项及子项
- C# lock 为什么要设置成只读
- C#用流读写大文件
- c#序列化
- C#如何判断程序调用的exe已结束
- c#中//注释和///注释的区别
- C#编程 MWArray 报错 原因以及解决办法