• net基础


    看题,然后写出输出结果:(2.0的framework没法自动化属性)

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication13
    {
    
        public abstract class A
        {
            private string _name;
            public string Name { get { return _name; } set { value = this._name; } }
    
            private int a = 12;
            public A()
            {
                Console.WriteLine("A");
            }
            public virtual void Fun()
            {
                Console.WriteLine("A.Fun()");
            }
    
        }
        public class B : A
        {
            public int _age;
            public int age { get { return _age; } set { value = this._age; } }
    
            public int c = 12;
    
            public B()
            {
                Console.WriteLine("B");
            }
            public new void Fun()
            {
                Console.WriteLine("B.Fun()");
            }
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                //当有继承再的时候,先实例调用父类中的
                //1.先初始化本类中的字段
                //2.接着初始化父类中的字段
                //3.调用父类中的构造函数进行属性的初始化
                //4.最后调用本类中的构造函数进行属性的初始化
                //所以结果是:
                //A
                //B 
                //A.Fun()
               
    
                A a = new B();//子类实例化父类; 
                      
                a.Fun(); 
    
                Console.ReadLine();
    
    
            }
        }
    }

     变形一:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication13
    {
    
        public abstract class A
        {
            private string _name;
            public string Name { get { return _name; } set { value = this._name; } }
    
            private int a = 12;
            public A()
            {
                Console.WriteLine("A");
            }
            public virtual void Fun()
            {
                Console.WriteLine("A.Fun()");
            }
    
        }
        public class B : A
        {
            public int _age;
            public int age { get { return _age; } set { value = this._age; } }
    
            public int c = 12;
    
            public B()
            {
                Console.WriteLine("B");
            }
            public override void Fun()
            {
                Console.WriteLine("B.Fun()");
            }
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
              
                //结果是:
                //A
                //B 
                //B.Fun()
               
    
                A a = new B();//子类实例化父类; 
                      
                a.Fun(); 
    
                Console.ReadLine();
    
    
            }
        }
    }

    变形二:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication13
    {
    
        public abstract class A
        {
            private string _name;
            public string Name { get { return _name; } set { value = this._name; } }
    
            private int a = 12;
            public A()
            {
                Console.WriteLine("A");
            }
            public virtual void Fun()
            {
                Console.WriteLine("A.Fun()");
            }
    
        }
        public class B : A
        {
            public int _age;
            public int age { get { return _age; } set { value = this._age; } }
    
            public int c = 12;
    
            public B()
            {
                Console.WriteLine("B");
            }
            public new void Fun()
            {
                Console.WriteLine("B.Fun()");
            }
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
              
                //结果是:
                //A
                //B 
                //B.Fun()
    
                B b = new B();//实例化子类;
                      
                b.Fun(); 
    
                Console.ReadLine();
    
    
            }
        }
    }

    总结:

       

      public class father
        {
            public void normalFun()
            {
                Console.WriteLine("f-normal");
    
            }
    
            public virtual void virFun()
            {
                Console.WriteLine("f-virtual");
    
            }
    
        }
    
        public class son : father
        {
            public new void normalFun()
            {
                Console.WriteLine("s-normal");
            }
    
            public override void virFun()
            {
                Console.WriteLine("s-virtual");
            }
        }
    
    
    
        class Program
        {
            static void Main(string[] args)
            {
                father f = new son();
    
                f.normalFun(); //f-normal
                f.virFun(); // s-virtual
    
                //记住:
                //new     的本质是:覆盖.隐藏 父类中的方法。
                //ovrride 的本质是:对父类中方法的扩展;
    
                Console.ReadLine();
    
    
            }
        }

    然后接着:我们再来看一道题:

     public class A
        {
            public virtual void Fun1(int i)
            {
                Console.WriteLine(i);
            }
            public void Fun2(A a)
            {
                a.Fun1(1);
                Fun1(5);
            }
        }
        public class B : A
        {
            public override void Fun1(int i)
            {
                base.Fun1(i + 1);
            }
            public static void Main()
            {
                B b = new B();
                A a = new A();
                a.Fun2(b);  //2 5
                b.Fun2(a);  //1 6
                Console.ReadLine();
            }
        }
  • 相关阅读:
    C/C++ 编写一个通用的Makefile 来编译.c .cpp 或混编
    C/C++ 定义接口文件格式
    MySql存储过程例子1
    项目所遇问题
    linux下编译C++程序无法链接Mysql的问题
    linux 同步时间 调试core内核
    CentOS安装与更新git
    03 js基本数据类型、 js运算符1
    02 js运行原理 、js开发工具介绍 、js程序入门、 js基本语法
    01 js基本介绍
  • 原文地址:https://www.cnblogs.com/mc67/p/5693470.html
Copyright © 2020-2023  润新知