C#_密封方法,命名空间,万物之父中的方法

抽象方法和虚方法都可以被子类 无限的重写下去

override前面加上一个sealed关键字 子类就不能继续重写该方法了

命名空间

1.命名空间是个工具包,用来管理类的

2.不同命名空间中可以有同名类

3.不同命名空间中相互使用 需要用using引用命名空间

或者  指明出处 (命名空间.)

4.命名空间可以包裹命名空间

using引用命名空间不能用里面包裹着的命名空间

需要 using 命名空间.其中包裹着的命名空间  才能访问包裹着的命名空间

namespace UI
{
    class Image
    { }
}
namespace Graph
{
    class Image
    { }
}
class Main
{ 
    public static void main()
    {
        UI.Image image = new UI.Image();    
        Graph.Image image2 = new Graph.Image(); 
    }
        
}

万物之父中的方法

1.虚方法 toString 自定字符转换规则

2.成员方法GetType 反射相关

3.成员方法 MemberwiseClone 浅拷贝(是保护修饰的 要 在类中做处理返回出去)

4.虚方法 Equals 自定义判断相等的规则

toString

class Player
{
    public string Name { get; set; }
    public int Hp { get; set; }
    public int  Def { get; set; }
    public int Atk {  get; set; }
    public Player(string Name,int Hp ,int Def,int Atk)
    {
        this .Name = Name;
        this .Hp = Hp;
        this .Def = Def;
        this .Atk = Atk;
    }
    public override string ToString()
    {
        return "玩家"+Name+"血量"+Hp+"攻击力"+Atk+"防御力"+Def;
    }
}

class Imain
{ 
    public static void Main()
    {
        Player player = new Player("C",10,3,1);
        Console.WriteLine (player);
    }
        
}

MemberwiseClone

class Monster
{
    public string Name { get; set; }
    public int Hp { get; set; }
    public int  Def { get; set; }
    public int Atk {  get; set; }
    public  Text text;
    public Monster (string Name,int Hp ,int Def,int Atk,Text text)
    {
        this .Name = Name;
        this .Hp = Hp;
        this .Def = Def;
        this .Atk = Atk;
        this .text = text;
    }
    public Monster Clone()
    {
        return MemberwiseClone() as Monster;
    }
}
class Text
{
   public   int i = 99;
}

class Imain
{ 
    public static void Main()
    {
        Text text = new Text();

        Monster A = new Monster("C", 20, 5, 10,text );
        Monster B = A.Clone();

        Console.WriteLine(B.Name + B.Hp + B.Def + B.Atk + B.text.i);
        Console.WriteLine(A.Name + A.Hp + A.Def + A.Atk + A.text.i);
        B .Name = "B";
        B.Hp = 40;
        B.text.i = 66;
        Console.WriteLine(B.Name + B.Hp + B.Def + B.Atk + B.text.i);
        Console.WriteLine(A.Name + A.Hp + A.Def + A.Atk + A.text.i);
        B.text = new Text();
        B.text.i = 0;
        Console.WriteLine(B.Name + B.Hp + B.Def + B.Atk + B.text.i);
        Console.WriteLine(A.Name + A.Hp + A.Def + A.Atk + A.text.i);
    }
        
}

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值