C#面向对象1 类 以及 类的继承(new、ovverride)

 

类 的典型代码==============================
包括 属性及其判断赋值  方法  构造函数及其重载
namespace 类的属性和方法
{
     public  class Car
    {       
         private  int _speed;    
         private  string _name;       

         public  int Speed              // 属性的封装,判断
        {        
             set {
                 if (value ==  20)
                {
                     this._speed = value;
                }
                 else
                {
                     this._speed =  50;
                }
            }        
             get{ return  this._speed;}     
        }    

         public  string Name    
        {        
             set {  this._name=value;}        
             get{ return  this._name;}     
        }    



         // 显式定义默认构造函数    
         public Car(){}    

         // 重载的带有参数的构造函数
         public Car( string name, int speed)    
        {        
             this._name=name;        
             this._speed=speed;    
        }    

         public  void ShowState()     
        {
            Console.WriteLine( " Car {0} is going {1} MPH "this._name, this. _speed);
        }
    }

 

     class Program
    {
         static  void Main( string[] args)
        {
            Car car1=  new Car( " 胡章诚 ", 19);
            car1.Speed =  19;
            Console.WriteLine( " 车名是{0},时速{1} ", car1.Name, car1.Speed);
            car1.ShowState();
            Console.ReadKey();
        }
    }
}


 

 
一般类的继承=============================
1、类的继承——子类构造函数


概述:子类默认情况下会调用父类的的无参数构造函数
      如果父类写了有参数的构造函数,子类未调用父类的有参数的构造函数,则需要显示的写出父类的无参数构造函

namespace 类的继承_子类构造函数
{
     class Program
    {
         // 3、 定义父亲类Father(姓firstName,财产wealth,血型blood),儿子Son类(玩游戏PlayGame),女儿Daughter类(跳舞Dance),
        
//     调用父类构造函数给子类字段赋值。
         static  void Main( string[] args)
        {
            Son son =  new Son( " Green "20000" O "" Good ");                  // 实例化一个儿子的对象
            Console.WriteLine(son.firstName);
            son.PlayGame( " lol ");                // 调用儿子的方法
            Console.ReadKey();
        }
    }

     class Father
    {
         public  string firstName;
         public  int wealth;
         public  string blood;

         public Father() { }      // 如果子类写了一个造函数,此造函数没有调用父类带参数的构造函数,此时就要显示的写出父类的无参数构造函数

         public Father( string f,  int w,  string b)       // 定于父类带参数的构造函数
        {
             this.firstName = f;
             this.wealth = w;
             this.blood = b;
        }
    }

     class Son : Father
    {
         public  string health;          // 自己写的一个字段
         public  void PlayGame( string Game)            // 将儿子玩游戏写成一个方法
        {
            Console.WriteLine( " 我玩{0},哈哈哈哈哈; ", Game);
        }


         public Son( string f,  int w,  string b,  string h)
            :  base(f, w, b)                 // 利用base调用父类的构造函数
        {
             this.firstName = f;
             this.wealth = w;
             this.blood = b;
             this.health = h;
        }
    }

     class Daughter : Father
    {
         public  int age;               // 自己写的一个字段
         public  void Dance()           // 将女儿跳舞写成一个方法
        {
            Console.WriteLine( " 我跳拉丁,呵呵 ");
        }

         public Daughter( string f,  int w,  string b,  int a)
            :  base(f, w, b)              // 利用base调用父类的构造函数
        {
             this.firstName = f;
             this.wealth = w;
             this.blood = b;
             this.age = a;
        }
    }
}



。。。。。。。。。。。。。。。。。。。。。。。。。

2、类的继承——用new关键字覆盖产生新方法

namespace 类的继承
{
     class Program
    {
         static  void Main( string[] args)
        {
            OtherClass oc =  new OtherClass();
            oc.Method1(oc.Field1);
            
            Console.ReadKey();
        }

 

         class SomeClass
        {
             public  string Field1 =  " base class field1 ";
             public  void Method1( string value) 
            {
                Console.WriteLine( " Base class -- Method1:{0} ",value);
            }
        }

         class OtherClass:SomeClass
        {
             new  public  string Field1 =  " OtherClass Field1 ";      // 掩盖旧成员
             new  public  void Method1( string value)                // 掩盖旧成员
            {
                Console.WriteLine( " OtherClass.Method:{0} ",value);
                Console.WriteLine( base.Field1);                  // 访问基类成员
            }
        }
    }
}



。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

3、类的继承——虚方法、抽象方法的重写产生新方法

namespace 虚方法和复写方法
{
     class Program
    {
         static  void Main( string[] args)
        {
            MyDrivedClass drived =  new MyDrivedClass();
            MyBaseClass mybs = (MyBaseClass)drived;
            drived.Print();
            mybs.Print();
            Console.ReadKey();
        }
    }

     class MyBaseClass
    {
         virtual  public  void Print()
        {
            Console.WriteLine( " this is base class ");
        }
    }


     class MyDrivedClass:MyBaseClass
    {
         override  public  void Print()
        {
            Console.WriteLine( " this is drived class ");
        }
    }
}

 

转载于:https://www.cnblogs.com/key1309/p/3151873.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值