学习OO思想

 

没抓住主线,请指教。

 

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;

namespace Study
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class Test
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Son_Class Son 
= new Son_Class();
            Son.CommonMethod();
            ((Father_Class)Son).CommonMethod();
//方法二:类型转换

            Console.Read();
        }

    }


    
abstract class Father_Class //抽象类才能有抽象方法;不能实例化
ExpandedSubBlockStart.gifContractedSubBlock.gif
    {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Father_Class(string parameter) { Console.WriteLine(parameter); }//构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
        public virtual void virtualMethod() { Console.WriteLine("Father_Class.virtualMethod"); }
        
public abstract void abstractMethod();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public void CommonMethod() { Console.WriteLine("Father_Class.CommonMethod"); }
    }

    
class Son_Class : Father_Class
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Son_Class() : base("子类给基类通信的方法:用base调用基类的构造函数"{ }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public sealed override void virtualMethod() { Console.WriteLine("Son_Class.virtualMethod ; sealed override方法已经不能在孙类override"); }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public override void abstractMethod() { Console.WriteLine("Son_Class.abstractMethod"); }
        
public new void CommonMethod()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Console.WriteLine(
"Son_Class.CommonMethod");
            
base.CommonMethod();//方法一
        }

    }

}

ExpandedBlockStart.gifContractedBlock.gif
/**//*
 继承:实现功能扩展和复用
 * virtual可以自己实现,也可以由子类override;abstract只能由子类override;用new可以任意重写一个父类的同名方法(不用new只会警告)
 * sealed类可以防止被继承;子类中sealed override的方法可以防止被孙类override
 * 子类给基类通信的方法:用base调用基类的构造函数
 * 子类中访问基类被override成员的方法:
 
 封装:
 多态:
 * 编译时多态:重载;运行时多态的实现:接口、继承、抽象类
 
 易混概念:
 * 接口用于定义类的能力(多用able结尾);抽象类是能提供部分实现的接口,多用于定义属性,如版本控制;C#的派生类只能从一个类中继承,只能通过接口实现多重继承
 * static = share,用类访问并被类的所有实例共享,避免冗余,如sqlconstr
 
*/

转载于:https://www.cnblogs.com/hbreset/archive/2008/12/02/1345887.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值