C#基础(1)
来源:互联网 发布:java闰年计算 编辑:程序博客网 时间:2024/05/10 10:12
1.
using System;class A{ public A() { PrintFields(); } public virtual void PrintFields(){}}class B:A{ int x=1; int y; public B() { y=-1; } public override void PrintFields() { Console.WriteLine("x={0},y={1}",x,y); }}
当使用new B()创建B的实例时,产生什么输出?
答:X=1,Y=0
当使用New创建B的实例时,因为B继承了A类,A的构造函数首先会被运行,但应当注意的是,虽然运行的是A的构造函数,但在A的构造函数中调用的函数首先是在B类中查找,因为PrintFields已被重写,所以最终运行B类中的PrintFields函数。
2.什么是单例模式,写个运用单例模式的类
Singleton模式包含的角色只有一个,就是Singleton。Singleton拥有一个私有构造函数,确保用户无法通过new直接
实例它
// Singleton pattern -- Structural example using System;// "Singleton"class Singleton{ // Fields private static Singleton instance; // Constructor protected Singleton() {} // Methods public static Singleton Instance(){ // Uses "Lazy initialization" if( instance == null ) instance = new Singleton(); return instance; }}/**//// <summary>/// Client test/// </summary>public class Client{ public static void Main(){ // Constructor is protected -- cannot use new Singleton s1 = Singleton.Instance(); Singleton s2 = Singleton.Instance(); if( s1 == s2 ) Console.WriteLine( "The same instance" ); }}
- C#基础(1)
- C#基础知识整理:C#基础(1)
- c#学习(基础1)
- (1)C#语法基础
- C#基础笔记(1)
- Ambow--C#基础笔记1(20110723)
- c#基础(1)之 构造函数
- C#编程基础 实验(1)
- 补充C#基础1
- C#基础篇---1
- C# 基础(1)
- 1、C#基础整理
- C#基础篇1
- C#基础1
- c#基础学习1
- C#基础1
- c#基础1
- C#语言基础-1
- 能够通过保存副本的方式复制族表吗?
- 一个提高渲染效率的小技巧
- Android实现XML解析技术
- gvim乱码解决方案
- MUTC 1 A - Divide Chocolate 方案统计DP
- C#基础(1)
- (转载)Hibernate 的 10 个常见面试问题及答案
- C#抽象类及其方法的学习
- 特效的批量绘制
- #pragma once与#ifndef的区别
- UML类图关系大全
- 安装JDK并变换版本
- FreeModbus源码分析
- ArrayList、LinkedList、 Vector、Map