可以在构造函数中调用private方法并使用对象初始化程序调用类public方法吗? [英] Is it okay to call private method in constructor and call class public method with object initializer?

查看:440
本文介绍了可以在构造函数中调用private方法并使用对象初始化程序调用类public方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在我的课程中,我正在创建一个新的令牌并调用服务方法,然后删除一个令牌。我可以在构造函数中创建一个新的令牌,然后同时调用服务方法
而不是创建新的实例,生成新令牌然后调用服务方法吗?

In my class, i am creating a new token and calling service method which then delete a token. Instead of creating a new instance, generating new token and then calling service method, can i just create a new token in constructor and then call service method at the same time?


GCService类中的所有方法都需要令牌来执行某些操作,如获取余额,验证客户等。

All the methods in GCService class require token to perform some operation like get balance, verify customer and etc.


我已经读过在构造函数中调用非虚方法是合法的。我之前从未使用过这种方法所以不确定它是否是正常的做法并且可以改进吗?

I have read that it is legit to call non virtual methods in constructor. I have never used this approach before so not sure if its a normal practise and can this be improved?


第一种方法


Public Class Customer : Implements ICustomer
{
   //creating a new instance , setting class properties in constructor and then calling class method.

Dim result = New GCService(a,b).GetBalance(a);
}

public class GCService : IGCService
{
private string Token { get; set; }

//constructor 
public GCService(string _a, string _a)
{
    //set some properties and get token
    Token = GetToken(_a, _b);
}

private string GetToken(string a, string b)
{
    //get token and return response
    return response;
}
}



第二种方法

Public Class Customer : Implements ICustomer { //creating a new instance , setting class properties in constructor and then calling class method. Dim result = New GCService(a,b).GetBalance(a); } public class GCService : IGCService { private string Token { get; set; } //constructor public GCService(string _a, string _a) { //set some properties and get token }

public BalanceService GetBalance string _a

public BalanceService GetBalance(string _a)

{

GetToken ();

GetToken();

//调用服务方法以获得平衡

//call service method to get balance

< span class ="pun"style ="color:#303336; FONT-FAMILY:继承;字体大小:13像素;字体风格:继承;字体变:继承;字体重量:继承;空白:预;背景色:#eff0f1;保证金:0像素;填充:0像素;边框:0像素;行高:继承; vertical-align:baseline">}



私人 字符串 GetToken string a < span class ="pun"style ="margin:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; font-family:inherit ; vertical-align:baseline; color:#303336">, string b { //获取令牌并返回响应

} private string GetToken(string a,string b){ //get token and return response

返回 响应 ;
}
}

return response; } }

推荐答案

你没有虚拟方法。虚方法的问题是在最低继承级别调用构造函数,虚方法是最高继承级别。 

You don't have virtual methods there. Problem of virtual method is that constructor is called in lowest inheritance level and virtual method is top inheritance level. 

https://stackoverflow.com/questions/ 119506 / virtual-member-call-in-a-constructor

私有方法不能是虚拟的,因为在继承的类中不可见。公共或受保护只能是虚拟的。  

Private methods cannot be virtual because there are not visible in inherited class. Public or protected could be virtual only. 

所以我认为你的代码是对的。

So I think your code is right.


这篇关于可以在构造函数中调用private方法并使用对象初始化程序调用类public方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆