学习记录:抽象类,静态类和单例模式

今天是学习C#第15天,但却是我第一次写博客。听我老师说,会写博客是个古老习俗,是每个程序员必备的技能。从今天起我每学习一天,将会记下今天所学重点,写一篇博客,不为别的什么,只为自己能记住所学,记录过程 。

前两周学了C#的基础,学了许多类的运用。今天学了抽象类,静态类和单例模式。



抽象类 :

抽象⽅法的关键词abstruct
抽象⽅法没有方法体。

抽象⽅法和虚⽅法最⼤的区别:

抽象⽅法必须其派⽣类中得以实现
⽽虚⽅法不是⼀定要在其派⽣类去重写

示例代码:

 /// <summary>
/// 运动员
/// </summary>

abstract class Sportsman
{
    private string name;

    public Sportsman(string name)
    {
        this.name = name;
    }

    /// <summary>
    /// 训练
    /// </summary>
    public abstract void Train();
    /*
     * 抽象方法必须在抽象类中存在
     * 但是,抽象类中不一定要有抽象方法,一般在抽象类中都会有抽象方法
     * 但是,抽象类中也可以存在普通方法
     */

    public void Eat()
    {
        Console.WriteLine("吃饭");
    }
}

/// <summary>
/// 足球运动员
/// </summary>
class FootBallSportsman : Sportsman
{
    //问题2:训练方法可以重写,也可以不重写
    // public override void Train()
    // {
    //     Console.WriteLine("跑步、传球、射门");
    // }
    public override void Train()
    {
        Console.WriteLine("跑步、传球、射门");
    }

    public FootBallSportsman(string name) : base(name)
    {
    }
}

abstract class SwimSportsman : Sportsman
{
    /*
     *     抽象方法在普通子类中,必须重写
     *     然而,在抽象子类中,可以选择重写或不重写
     */

    // public override void Train()
    // {
    // }
    protected SwimSportsman(string name) : base(name)
    {
    }
}

/// <summary>
/// 自由泳
/// </summary>
class FreedomSwimSportsman : SwimSportsman
{
    public override void Train()
    {
        Console.WriteLine("摆臂、蹬腿、憋气");
    }

    public FreedomSwimSportsman(string name) : base(name)
    {
    }
}

/// <summary>
/// 蛙泳
/// </summary>
class Breaststroke : SwimSportsman
{
    public override void Train()
    {
        Console.WriteLine("扩臂、蹬腿、憋气");
    }

    public Breaststroke(string name) : base(name)
    {
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Sportsman liuxiang = new Sportsman();
        // //问题1:方法体为空,不知道训练什么
        // liuxiang.Train();

        //抽象类允许声明对象
        Sportsman yaoming;
        //抽象类不能进行实例化对象
        //new Sportsman();
        
        Sportsman wulei = new FootBallSportsman("武磊");
        wulei.Train();
        
        Sportsman xiaoming = new FreedomSwimSportsman("xiaoming");
        Sportsman xiaohong = new Breaststroke("xiaohong");
        
        xiaoming.Train();
        xiaohong.Train();
    }
}

静态类:
关键词 static;
静态成员在何时开辟的内存
第⼀次访问这个类的时候【第⼀次⽤到这个类的时候】

⽐如:⽤这个类名去实例化⼀个对象     
⽐如:⽤这个类型去访问⼀个静态字段

静态成员使用static关键字修饰。
class Person
{
//人口数量
public static long population = 7000000000;
//设置人口数量
public static void SetPopulation(long population) {
Person.population = population; } }


静态成员要通过类名去访问
public static void Main(string[] args)
{
Person.SetPopulation(6000000000);
Console.WriteLine(Person.population);
}

※※※※
静态⽅法中是不可以访问⾮静态的成员的字段、属性和方法
但⾮静态⽅法中是可以访问静态成员的字段、属性和方法


静态构造函数
只有⼀种写法

static 类名()

静态构造函数必须⽆参数

静态构造函数在什么时候才会调⽤:

静态构造函数在程序运⾏期间只会执⾏⼀次
在第⼀次访问该类的时候调⽤
⽤这个类去new⼀个对象
⽤这个类去访问某个静态成员
⽤这个类去调⽤某个静态⽅法

如果有继承关系静态构造函数的执⾏顺序是:

先执⾏⼦类的静态构造,再执⾏⽗类的静态构造

静态构造有什么作⽤:

⼀般⽤于对静态成员进⾏初始化

静态构造函数

class Hero
{
public Hero(string name, int HP, string type) {//实例构造函数
this.name = name; this.HP = HP; this.type = type; }
static Hero()
{
Console.WriteLine("Hero类的静态构造函数");
} }

示例代码:

class Equip
{
    
}

static class GameConst
{
    //静态类中的常数定义的可以使用全大写or大驼峰
    public const float PLAYER_HEIGHT = 1.8f;
    public static float Player_MoveSpeed = 5;
    public static Equip[] Player_Equips;

    //静态构造函数
    //1、[前面不允许添加访问修饰符]
    //2、[不能有参数]
    //3、[执行的时间节点:第一次访问该类的静态字段、属性、方法]
    static GameConst()
    {
        Player_Equips = new Equip[5];
        Console.WriteLine("GameConst 静态构造");
    }
}

//在静态类中,只能有出现静态的东西,不能有成员的东西
static class MathTool
{
    //成员的字段、属性、方法,都不能存在
    // private string name;
    // public string Name
    // {
    //     get => name;
    //     set => name = value;
    // }
    // public void Demo()
    // {
    // }

    public static float[] Sort(float[] array)
    {
        return null;
    }
}

//普通的类中,也可以设置静态的字段、属性、方法
class Person
{
    public static float PLAYER_HEIGHT = 1.8f;
    
    //静态字段
    private static long population = 7000000000;
    public static int historyLength = 5000;

    //静态属性
    public static long Polulation
    {
        get { return population;}
        set { population = value; }
    }

    //静态方法
    public static long GetPopulation()
    {
        //静态方法里,不能出现成员的字段、属性、方法
        // Console.WriteLine(name);
        return Person.population;
    }

    static Person()
    {
        Console.WriteLine("Person 静态构造");
    }

    public void SayHello()
    {
        //成员方法里可以访问访问静态字段、方法
        Console.WriteLine(population);
        Console.WriteLine(historyLength);
        GetPopulation();
    }

    public string name;
}

class Program
{
    static void Main(string[] args)
    {
        //静态字段直接通过类名去访问
        // Console.WriteLine(Person.population);
        Console.WriteLine(Person.historyLength);
        
        Person xiaoming = new Person();
        // xiaoming.name
        Person xiaohong = new Person();
        // xiaohong.name


        Person.GetPopulation();
        Console.WriteLine(Person.historyLength);
        Console.WriteLine(Person.Polulation);

        //静态类不能进行实例化对象
        // new MathTool();
        //静态类的主要用途是,用于调用类中的静态字段、属性、方法
        MathTool.Sort(new float[] {2, 3, 5, 6, 3});
        
        //访问常数?常数一直保持不变,跟对象也毫无关系
        //访问方式,也是通过类名访问,类名.常数变量
        Console.WriteLine(GameConst.PLAYER_HEIGHT);
        Console.WriteLine(GameConst.Player_MoveSpeed);
        Console.WriteLine(GameConst.Player_MoveSpeed);
    }
}

单例模式:
一种设计模式;
最早在一本《GoF》的书上有,这本书共记录了23种设计模式


单例模式的三要点:

(1)某个类只能有一个实例
(2)必须自行创建这个实例
(3)必须自行向外界提供这个实例

单例的几种方法:

    #region ReadOnly方式

    //让某个静态字段只能读,不能写 readonly
    public static readonly GameData instance = new GameData();

    #endregion

    #region 使用只读属性方式

    //可以用只读属性解决            
    public static GameData Instance
    {
        get { return instance;}
    }

    #endregion

    #region 使用静态方法【懒加载】
    
    private static GameData instance;

    public static GameData GetInstance()
    {
        if (instance == null)
        {
            instance = new GameData();
        }
        return instance;
    }

    #endregion

    #region 使用单例

    public string gameTime;

    #endregion

※※※※※
单例类写法【懒加载】:
【使用懒加载单例类时不能用静态字段、属性、方法】

/// <summary>
/// 单例类
/// </summary>
class Singleton
{
    private static Singleton instance;
    public static Singleton GetInstance()
    {
        if (instance == null)
        {
            instance = new Singleton();
        }
        return instance;
    }

    private Singleton()
    {
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容