1 .NET(C#)加强——三个关键字

本文介绍了C#编程中的new、this和base关键字的使用。new关键字用于隐藏父类成员,避免冲突;this关键字代表当前对象实例,用于区分成员变量和局部变量;base关键字则用于调用父类的成员,确保继承链上的方法正确执行。通过示例代码和效果展示,解释了这三个关键字在实际编程中的作用和应用场景。
摘要由CSDN通过智能技术生成

1-new
1) 创建对象
在堆中开辟空间(因为引用类型就是在堆中)
在开辟的堆空间中创建对象
调用对象的构造函数
2)隐藏父类的成员
3)代码展示
Person.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace _04三个关键字
{
    class Person
    {
        public void SayHi()
        {
            Console.WriteLine("世界和平");
        }
    }
    class Student:Person
    {
        public void SayHi()
        {
            Console.WriteLine("学海无涯");
        }
    }
}

Program.cs

using _04三个关键字;
using System;

namespace _04三个关键字
{
    class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student();

        }
    }
}

效果展示
报错:
在这里插入图片描述
原因:如果子类的成员名称跟父类成员名称一样的话,此时,子类成员就会把父类成员隐藏,但是隐藏不彻底,子类就无法访问到父类,
但 是会报错
解决办法:将子类成员添加new
修改如下

using System;
using System.Collections.Generic;
using System.Text;

namespace _04三个关键字
{
    class Person
    {
        public void SayHi()
        {
            Console.WriteLine("世界和平");
        }
    }
    class Student:Person
    {
        public **new** void SayHi()
        {
            Console.WriteLine("学海无涯");
        }
    }
}

编译成功
2-this 关键字
1)代表当前类的对象
2)代码展示
Person.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace _04三个关键字
{
    class Person
    {
        public void SayHi()
        {
            Console.WriteLine("世界和平");
        }
    }
    class Student:Person
    {
        public new void SayHi()
        {
            Console.WriteLine("学海无涯");
        }
        public string Name { get; set; }
        public void Test()
        {
            string Name = "王者农药";
            Console.WriteLine("我的名字叫{0}"+Name);
        }
    }
}

Program.cs

using _04三个关键字;
using System;

namespace _04三个关键字
{
    class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student();
            s.Name = "王者荣耀";
            s.Test();         
            Console.ReadKey();
        }
    }
}

效果展示
在这里插入图片描述
如果将上面Person.cs的代码改为

using System;
using System.Collections.Generic;
using System.Text;

namespace _04三个关键字
{
    class Person
    {
        public void SayHi()
        {
            Console.WriteLine("世界和平");
        }
    }
    class Student:Person
    {
        public new void SayHi()
        {
            Console.WriteLine("学海无涯");
        }
        public string Name { get; set; }
        public void Test()
        {
            string Name = "王者农药";
            Console.WriteLine("我的名字叫{0}"+**this**.Name);
        }
    }
}

效果展示:
在这里插入图片描述
原因分析:1)this是指对象
2) 局部变量的优先级大于成员变量,方法中变量为局部变量,而方法为成员变量
因此string Name=“王者农药”; 优先级要大于s.Test(),因此不加this关键字时候,会出现第一种情况
3)加上this关键字时候,就会第二种情况,因为this是指引用,前面new了一个student为s,s.Name为王者荣耀,因此输出
王者荣耀

3-base

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值