语法

关于类属性的访问权限

对于Python来说,并不存在私有属性——虽然提供了一个双下划线的“人为定义”,这使得你在直接访问时获取到Exception,但这只是提示你,访问该属性是一个危险操作,应该规避这种访问方式——你还是可以获取到该内存。

如果只是开发一个exe程序,这个方式简单而高效——我们仅需要编译器告诉我们哪些访问是危险的。因为绝大多数情况下,我们既然设计了某个属性,就意味着它作为变量,是允许读写的——至于错误的读写,是用户使用不当的问题!

当然库(广义概念的库,包括统一程序中的辅助类、基类等)的开发就是另一码事儿了。当某一个对象与外界交互时,外界并不需要知道你的所有细节——接口就够了,包括接口函数和接口属性。其他的,是你内部设计的问题,与外界无关。

这里的外界,还包括两部分——纯粹的外部系统,以及拓展子类。

C# 中,关于属性,有以下几种形式:

  • Filed
  • Property
  • Index
  • Attribute

Field,应该作为内部的变量,仅供类内使用(包括protected继承给子类)。所以一般定义为 protected string _abc;

Property 只读接口:

  • public string abc => _abc,表示提供 _abc 的访问接口,但不提供改写接口。
  • public string abc{get;} ,创建一个属性,并且只提供访问(且子类无权限set),相当于 private string _abc;  pulic string abc => abc;

Property 读写接口: publi string abc{get; set;},这与定义一个Filed: public string abc 作用相同。不同的是,我们可以重写get/set访问器,来实现自定义的读写操作,如控制写入参数的范围。

注意,一般的Property都是public修饰,因为如果不需要外部访问或仅仅对子类开放,定义为 protected Filed 即可。

总结:C# 访问是按照外部访问权限定义关键字的,一般的,对于Filed,多定义为protected(除非严格禁止子类继承时,使用private),其作用是存储数据;而对于Property,一般定义为public,其作用是 存储数据+外部交互,使用时更像一个接口(函数)。

关于C#多态的思考

https://www.cnblogs.com/brt3/p/9744070.html

架构与模式

抽象类实现接口类的类型冲突(未解决)

在《设计模式:示例与思考》里介绍桥接模式时,遇到一个问题:最初我的设计是面向接口的:

public interface ISoftWare{void run();
}public interface IHardWare{void drive();void software_install(ISoftWare software);void software_execute();
}

接着,我发现 ISoftWare 和 IHardWare 可以进一步实现:

public abstract class SoftWare: ISoftWare{public HardWare env{get;set;}protected abstract void function();  // 软件功能代码(我们假设功能代码与环境无关,也就是Java跨平台特性)public void run(){  // run() 就是一个 Template Method.this.env.drive();  // 软件运行需要载入系统运行时this.function();}
}

但在实现 HardWare 的抽象类时,遇到了问题:

public abstract class HardWare: IHardWare{public SoftWare software{get;set;}public abstract void drive();public void software_install(SoftWare software){  // we need change the ISoftWare to Abstract SoftWarethis.software = software;this.software.env = this;}public void software_execute(){this.software.run();}
}

由于 software_install 方法在实现时,需要定义属性 software 的属性值,但对于 IHardWare,它并不知道 software 属性的存在。

那么要想编译成功,我们就需要将接口改成 SoftWare 对象。但 interface 去依赖 ConcreteClass?这个并不合理。

没办法,只能选择了 HardWare 不再实现接口——但这个实现在逻辑上是有意义的:我们面向接口编程,而 HardWare 不过是对接口的一种实现而已,我们无法保证抽象类是顶层接口,它的若干实现并不一定具有普遍性——普遍性的依然是接口类。

转载于:https://www.cnblogs.com/brt3/p/9739197.html

my questions of C#相关推荐

  1. R使用LSTM模型构建深度学习文本分类模型(Quora Insincere Questions Classification)

    R使用LSTM模型构建深度学习文本分类模型(Quora Insincere Questions Classification) Long Short Term 网络-- 一般就叫做 LSTM --是一 ...

  2. 35+ Top Apache Tomcat Interview Questions And Answers【转】

    原文地址:https://www.softwaretestinghelp.com/apache-tomcat-interview-questions/ Most frequently asked Ap ...

  3. 机器学习面试题合集Collection of Machine Learning Interview Questions

    The Machine Learning part of the interview is usually the most elaborate one. That's the reason we h ...

  4. Reading Club Questions Feedback

    Reading Club Questions Feedback 文章目录 Reading Club Questions Feedback Paper 1 Title Keywords (Domain) ...

  5. THE QUESTIONS :The Top 25 (what we don't know?)

    Essays by our news staff on 25 big questions facing science over the next quarter-century. > What ...

  6. 转:C# Interview Questions

    转自: http://blogs.crsw.com/mark/articles/252.aspx C# Interview Questions This is a list of questions ...

  7. Some Essential JavaScript Questions And Answers(6)

    Some Essential JavaScript Questions And Answers Question11: Write a simple function (less than 160 c ...

  8. Some Essential JavaScript Questions And Answers(5)

    Some Essential JavaScript Questions And Answers Question 9: Discuss possible ways to write a functio ...

  9. Some Essential JavaScript Questions And Answers(4)

    Some Essential JavaScript Questions And Answers Question7: What is NaN? What is its type? How can yo ...

  10. Some Essential JavaScript Questions And Answers(3)

    Some Essential JavaScript Questions And Answers Question5: What is the significance, and what are th ...

最新文章

  1. 线性回归之案例:波士顿房价预测
  2. python爬虫之cookie方式自动登录巴比特网
  3. vue样式 引入图片_详解Vue.js中引入图片路径的几种方式
  4. 秒杀苹果carplay baidu车联网API冷艳北京车展
  5. 使用tensorflow查询机器上是否存在可用的gpu设备
  6. Nginx+memcached+tomcat配置集群session共享负载均衡
  7. mybatis简单案例源码详细【注释全面】——实体层(User.java)
  8. 支持javascript的ppt软件_强大CSS3的3D幻灯片工具reveal.js(推荐)
  9. python处理excel表格-Python利用pandas处理Excel数据的应用
  10. 常见的算法题目分类图
  11. 玉龙雪山还会存在多久
  12. 在linux下运行锐捷客户端,锐捷Linux客户端使用方法(基于ubuntu16.04)
  13. 2022年武汉CMMI3-CMMI5认证企业名录
  14. Perl教程-3.基础语法
  15. 山东省首版次高端软件产品申报
  16. NCCL无root权限编译安装
  17. c#反射,类型XX对象无法转换为类型XX,XX是一样的问题
  18. osgEarth在斜面内绕自身Z轴旋转的锥体
  19. python3-输入华氏度转化为摄氏度
  20. 修改Ubuntu的更新源

热门文章

  1. 2013汇总计算 广联达gcl_广联达图形算量GCL2013整体操作流程图文教程详解
  2. java 末级递归树_如何递归获取json里末级章节名称
  3. Greenplum-概念篇
  4. 深入Atlas系列:探究序列化与反序列化能力(下) - JavaScriptSerializer
  5. DelphiBCB一线程序员开发经验
  6. Windows下安装配置Maven
  7. html定位 浏览器兼容,IE6浏览器不支持固定定位(position:fixed)解决方案
  8. java 代码结构_代码结构说明
  9. git serialtool_Git学习笔记---协作的一般流程
  10. SpringMVC面试