java子类的定义_如何定义子类使用的常量?

我需要定义一些将由基类及其子类使用的常量 . 不确定定义它们的正确方法是什么 .

我理解const,readonly,static const以及public,protected和private之间的区别(虽然我很少看到C#中使用了“protected”) . 应该如何定义这些常量?它们应该是公共const,公共只读,私有常量还是私有只读,并使用公共getter / setter来使用子类,还是应该将它们定义为受保护?

另一个问题是关于BaseClass中的变量FilePath . FilePath将被BaseClass中的某些函数用作占位符(实际值将由子类提供),我应该将其定义为虚拟吗?

有人可以提供一般规则吗?以下是我所拥有的一个例子:

public class BaseClass

{

public const string Country = "USA";

public const string State = "California";

public const string City = "San Francisco";

public virtual string FilePath

{

get

{

throw new NotImplementedException();

}

set

{

throw new NotImplementedException();

}

}

}

public class Class1 : BaseClass {

public Class1() {

FilPath = "C:\test";

}

public string GetAddress() {

return City + ", " + State + ", " + Country;

}

public void CreateFile() {

if (!Directory.Exist(FilePath)) {

//create folder, etc

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值