面向对象(二)
来源:互联网 发布:少儿学围棋软件 编辑:程序博客网 时间:2024/05/09 21:46
1.自动属性
2.接口的实现(正常实现,显示实现)
3.静态的初始化与生命周期
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _04自动属性{ abstract class Person // 李村 { private string name; // 属性可读可写,没有约束 public string Name { get { return name; } set { name = value; } } // 懒 public int Age { get; set; } //未实现(接口里面的写法) public abstract char Sex { get; set; } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _02接口{ //public interface I1 //{ // void Func(); //} //public interface I2 //{ // void Func(); //} //class MyClass : I1, I2 //{ //} public interface IMyInterface { // 接口里面可以有的成员: // 方法、属性、索引、事件 // 是对能力的抽象 // 成员写法:1、不需要访问修饰符;2、没有方法体 void Func(); string Name { get; set; } string this[int index] { get; set; } event Action MyEvent; } // 实现接口:1、正常实现;2、显式实现 // 1、就像没有接口一样使用 class MyClass : IMyInterface { public void Func() { throw new NotImplementedException(); } public string Name { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } public string this[int index] { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } public event Action MyEvent; } // 2、显式实现接口: // 为什么要有:实现多个接口中有重名的方法 // 不要修饰符,使用接口名.成员名实现 class MyClass2 : IMyInterface { void IMyInterface.Func() { throw new NotImplementedException(); } string IMyInterface.Name { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } string IMyInterface.this[int index] { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } event Action IMyInterface.MyEvent { add { throw new NotImplementedException(); } remove { throw new NotImplementedException(); } } } // 使用注意:显式实现的接口对象,只允许使用接口类型进行调用 public interface I { void Func(); } class Class : I { void I.Func() { throw new NotImplementedException(); } } class Program { static void Main(string[] args) { Class c = new Class(); I i = c; i.Func(); // 接口就是为了多态而诞生的 } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _06静态的初始化与生命周期以及使用建议{ // 静态在访问这个类型的第一次进行初始化,在程序结束后才会被释放资源 class MyClass { // public int numPublic = 10; // 这里的操作是构造方法做的,是为了方便程序员给的简单的语法 // public static int numStatic = 20; public int numPublic; public static int numStatic; public MyClass() { numPublic = 10; } // 静态构造方法 static MyClass() { numStatic = 20; // 静态构造方法目的在初始化静态成员,不允许手动调用 // 不允许有重载继承等内容 } } class Program { static void Main(string[] args) { MyClass m1 = new MyClass(); // 访问MyClass->初始化静态成员(调用静态构造方法) // ->初始化实例成员(调用实例构造方法) MyClass m2 = new MyClass(); // 访问MyClass->实例构造方法 } }}
0 0
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- 面向对象(二)
- NYOJ-32组合数
- 从前有座灵剑山--凡人与神明的界限
- hdu 4122 Alice's mooncake shop (线段树)
- Postgresql 9.3.4 最新windows精简安装版。
- linux常用命令
- 面向对象(二)
- Core Java 8 学习笔记(四) 打印一个简单的日历
- OracleDBConsoleorcl无法启动 解决方法
- mysql之触发器trigger
- 黑马程序员 java面向对象<四> 多线程
- 单点登录CAS与Spring Security集成(数据库验证,向客户端发送更多信息)
- 英语总结-14年6月
- css+div
- 线段树学习——TOJ 练习