C#基础入门 六
2021-02-14 18:21
标签:成员 ase console static 技术分享 new 例子 gpl let C#基础入门 六 标签:成员 ase console static 技术分享 new 例子 gpl let 原文地址:https://www.cnblogs.com/senlinmilelu/p/8445968.htmlC#基础入门 六
静态类进阶
静态构造方法
public static class Tool
{
static int length;
static void fun(){}
static Tool()
{
Console.WriteLine("public Tool()");//静态类构造方法
}
}
单例设计模式
public class Singleton
{
private static Singleton instance;//内建静态实例
private Singleton(){}//构造方法必须设为私有
public static Singleton Instance
{
get { return instance; }
}
}
static Singleton(){
if (instance==null){
instance=new Singleton();//为实例初始化
}}
抽象类
练习
class Player
{
public virtual void Practice()
{
Console.WriteLine("player‘s practice");
}
}
class Footballplayer : Player
{
public override void Practice()
{
Console.WriteLine("footballplayer‘s practice");
}
}
class Swimmingplayer : Player
{
public override void Practice()
{
base.Practice();
Console.WriteLine("swimmingplayer‘s practice");
}
}
class MainClass
{
public static void Main(string[] args)
{
Player player = new Footballplayer();
player.Practice();
}
}
public abstract void Practice();
abstract class Player
{
public abstract void Practice();
}
abstract class Animal
{
int age;
void func(){
Console.WriteLine("Animal‘s function");
}
}
abstract public class Player
{
abstract public void Practice();
}
public class Footballplayer : Player
{
public override void Practice()
{
Console.WriteLine("足球运动员在训练");
}
}
public static void Main(string[] args)
{
Footballplayer footballplayer =new Footballplayer();
footballplayer.Practice();
}