base(C# 参考)

base 关键字用于从派生类中访问基类的成员:

  • 调用基类上已被其他方法重写的方法。

  • 指定创建派生类实例时应调用的基类构造函数。

基类访问只能在构造函数、实例方法或实例属性访问器中进行。

从静态方法中使用 base 关键字是错误的。

所访问的基类是类声明中指定的基类。例如,如果指定 class ClassB : ClassA,则无论 ClassA 的基类如何,从 ClassB 上访问 ClassA 的成员。

在本例中,基类 Person 和派生类 Employee 都有一个名为 Getinfo 的方法。通过使用 base 关键字,可以从派生类中调用基类的Getinfo 方法。

C#
 
public class Person
{
    protected string ssn = "444-55-6666";
    protected string name = "John L. Malgraine";

    public virtual void GetInfo()
    {
        Console.WriteLine("Name: {0}", name);
        Console.WriteLine("SSN: {0}", ssn);
    }
}
class Employee : Person
{
    public string id = "ABC567EFG";
    public override void GetInfo()
    {
        // Calling the base class GetInfo method:
        base.GetInfo();
        Console.WriteLine("Employee ID: {0}", id);
    }
}

class TestClass
{
    static void Main()
    {
        Employee E = new Employee();
        E.GetInfo();
    }
}
/*
Output
Name: John L. Malgraine
SSN: 444-55-6666
Employee ID: ABC567EFG
*/

有关其他示例,请参见 newvirtual 和 override

本示例显示如何指定在创建派生类实例时调用的基类构造函数。

C#
 
public class BaseClass
{
    int num;

    public BaseClass()
    {
        Console.WriteLine("in BaseClass()");
    }

    public BaseClass(int i)
    {
        num = i;
        Console.WriteLine("in BaseClass(int i)");
    }

    public int GetNum()
    {
        return num;
    }
}

public class DerivedClass : BaseClass
{
    // This constructor will call BaseClass.BaseClass()
    public DerivedClass() : base()
    {
    }

    // This constructor will call BaseClass.BaseClass(int i)
    public DerivedClass(int i) : base(i)
    {
    }

    static void Main()
    {
        DerivedClass md = new DerivedClass();
        DerivedClass md1 = new DerivedClass(1);
    }
}
/*
Output:
in BaseClass()
in BaseClass(int i)
*/

转载于:https://www.cnblogs.com/huibin-benteng/p/5044151.html

欧易(OKEx)是一家知名的加密货币交易所,提供了丰富的API接口供开发者使用。通过这些API接口,开发者可以实现自动化交易、获取市场数据等操作。以下是一个使用C#编写的欧易量化交易参考代码示例: ```csharp using System; using System.Net.Http; using System.Threading.Tasks; using Newtonsoft.Json.Linq; namespace OKExExample { class Program { static async Task Main(string[] args) { string apiKey = "your_api_key"; string secretKey = "your_secret_key"; string passphrase = "your_passphrase"; string baseUrl = "https://www.okex.com"; var client = new HttpClient { BaseAddress = new Uri(baseUrl) }; // 获取账户信息 var accountResponse = await client.GetAsync($"/api/v5/account/balance?type=spot"); var accountContent = await accountResponse.Content.ReadAsStringAsync(); Console.WriteLine("账户信息:"); Console.WriteLine(accountContent); // 下单 var orderRequest = new { instId = "BTC-USDT", tdMode = "cash", side = "buy", ordType = "limit", px = "30000", sz = "0.001" }; var orderContent = new StringContent(JObject.FromObject(orderRequest).ToString(), System.Text.Encoding.UTF8, "application/json"); var orderResponse = await client.PostAsync("/api/v5/trade/order", orderContent); var orderResult = await orderResponse.Content.ReadAsStringAsync(); Console.WriteLine("下单结果:"); Console.WriteLine(orderResult); } } } ``` 这个示例代码展示了如何使用C#调用欧易的API接口,包括获取账户信息和下单操作。请注意,实际使用中需要替换`your_api_key`、`your_secret_key`和`your_passphrase`为你的实际API密钥和密码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值