一、注释
单行注释://内容
多行注释:/*内容*/
文档注释:///内容
Html: <!--内容-->
Css: /*内容*/
二、命名规范
camel 首单词小写,后面单词首字母大写;用于变量,字段。
pasical 每单词首字母大写。用于方法或类。方法以动词开头。
三、面向对象
进程
使用进程打开指定的文件
ProcessStartInfo psi = new ProcessStartInfo(@"E:\1.txt");
Process p = Process.Start(psi);
抽象类
父类不实现,无方法体;在子类中进行实现。
简单工厂模型:
不知道用户输入什么类型的文件,所以给用户返回一个统一的父类。这个父类中装的肯定是
子类。
方法中类型的判断,将子类赋值给父类是关键。
练习1:磁盘上有不同文件,如txt,avi,xls等,要求:编写控制台程序,用户输入一带后缀
名的文件,将该文件名传递到一个方法中,该方法根据用户输入的文件名后缀,返回一个对
应的文件对象。提示:编写不同的类模拟不同的该文件,为不同的文件类创建统一的父类,
使用简单工厂实现。(提示:多态,简单工厂模型,进程)
先建抽象父类及子类,各类要用全路径,因此在父类加属性,并分别通过构造传递。
public abstract class FileType
{
public string FullPath
{
set; get;
}
public FileType(string fullPath)
{
this.FullPath = fullPath;
}
public abstract void ShowFile();
}
public class TxtFile : FileType
{
public TxtFile(string str) : base(str)
{
}
public override void ShowFile()
{
Console.WriteLine(Path.GetFileName(FullPath) + "是txt文件,用记事本打开");
Process.Start(new ProcessStartInfo(FullPath));
}
}
public class WavFile : FileType
{
public WavFile(string str) : base(str)
{
}
public override void ShowFile()
{
Console.WriteLine(Path.GetFileName(FullPath) + "是wav文件,用play打开");
Process.Start(new ProcessStartInfo(FullPath));
}
}
public class XlsFile : FileType
{
public XlsFile(string str) : base(str)
{
}
public override void ShowFile()
{
Console.WriteLine(Path.GetFileName(FullPath) + "是xls文件,用excel打开");
Process.Start(new ProcessStartInfo(FullPath));
}
}
public class PicFile : FileType
{
public PicFile(string str) : base(str)
{
}
public override void ShowFile()
{
Console.WriteLine(Path.GetFileName(FullPath) + "是Pic文件,用mspaint打开");
Process.Start(new ProcessStartInfo(FullPath));
}
}
在主类中主方法与调用方法中使用转换及返回统一父类。
private static void Main(string[] args)
{
Console.WriteLine("输入要进入的目录:");
string d = Console.ReadLine();
Console.WriteLine("输入要打开的文件:");
string f = Console.ReadLine();
string p = Path.Combine(d, f);
if (!File.Exists(p))
{
Console.WriteLine("文件不存在!");
return;
}
FileType fi = GetInfo(p);//统一返回,统一显示
fi.ShowFile();
Console.ReadKey();
}
public static FileType GetInfo(string str)
{
FileType fi;
string ext = Path.GetExtension(str).Substring(1);
switch (ext.ToLower())//核心,转换
{
case "txt":
fi = new TxtFile(str);
break;
case "wav":
fi = new WavFile(str);
break;
case "png":
fi = new PicFile(str);
break;
default:
fi = new XlsFile(str);
break;
}
return fi;
}
四、面向对象复习
封装,继承,多态(多停留几分钟回忆)
1、封装
字段(_name):
属性(Name):
构造函数:作用,初始化。重载
方法,接口
new关健字三个作用:开辟,创建,初始
this:当前对象,显式调用当前构造函数
特殊:静态类,静态方法
return的作用:结束,返回
对数据的保护限制的三种方法: set,get,构造
问题留下:创建单一实例?
2、继承
解决代码冗余,实现多态,增加代码的扩展性,便于维护。
单根性
传递性
一般子类继承父类属性与方法,不会继承字段(私有)
子类没有继承父类的构造函数,而是调用父类的构造函数。
一般是调用无参构造,一旦父类有参构造,则需要子类显式调用父类构造函数
base(param1,param2,...)
子类可以用自身成员,也可以用父类成员;但是父类只能用父类成员。
子类这间不能调用对方的成员。
五、里氏转换
子类可赋值给父类;若父类装的是子类,则可将父类转换为子类。
对象转换
as 成功返回对象,失败为null
is 成功返回true,失败false
很多参数调用的都是父类对象,可以用子类代替进入。这样屏蔽子类间的差异,实现多态。
多态三种:
1) 虚方法,
2)抽象类,
3)接口。
案例1:猜拳游戏
案例2:动物类继承
案例3:面向对象计算器
案例4:外部设备(抽象方法)
案例5:登记案例(接口)
技巧:vs2022升级了,快速片段输入不再是2个Tab,而是一个了。比如输入cw再按一下tab
直接就出Console.WriteLine();
探讨:vs2022升级后,swith语句,只要没有赋值的,最后一句必须用default,查了好久
原因是这里,最后一个case改成default.好像是强制标准了。
面向对象计算器
分析:两个数,一个运算符,结果。难的是运算符,把运算符当作抽象父类,通过子类
进行具体运算符计算。利用简单工厂设计进行子类转转成统一的父类返回。
namespace ConsoleApp1
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("第一个数:");
double n1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("第二个数:");
double n2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("运算符:");
string str = Console.ReadLine();
Cal c = ChangeToFather(n1, n2, str);
Console.WriteLine(c.GetResult());
Console.ReadKey();
}
public static Cal ChangeToFather(double one, double two, string str)
{
Cal c;
switch (str)
{
case "+":
c = new Add(one, two); break;
case "-":
c = new Jian(one, two); break;
case "*":
c = new Sheng(one, two); break;
default:
c = new Chu(one, two); break;
}
return c;
}
}
public abstract class Cal
{
public double One
{
set; get;
}
public double Two
{
get; set;
}
public Cal(double one, double two)
{
this.One = one;
this.Two = two;
}
public abstract double GetResult();
}
public class Add : Cal
{
public Add(double one, double two) : base(one, two)
{
}
public override double GetResult()
{
return One + Two;
}
}
public class Jian : Cal
{
public Jian(double one, double two) : base(one, two)
{
}
public override double GetResult()
{
return One - Two;
}
}
public class Chu : Cal
{
public Chu(double one, double two) : base(one, two)
{
}
public override double GetResult()
{
return One / Two;
}
}
public class Sheng : Cal
{
public Sheng(double one, double two) : base(one, two)
{
}
public override double GetResult()
{
return One * Two;
}
}
}