38 派生类中的构造函数 object类,ToString

派生类中的构造函数(有参,无参)、

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gz
{
    class Program
    {
        static void Main(string[] args)
        {
            C abc = new C();
        }
    }
    class A
    {
        private int a;
        public A()
        {
            a=1;
            Console.WriteLine("调用构造函数A");
        }
        
        }
    
        class B:A
        {
        private int b;
        public B()
        {
            b=1;
            Console.WriteLine("调用构造函数B");
        }
}
        class C : B
        {
            private int c;
            public C()
            {
                c = 1;
                Console.WriteLine("调用构造函数C");
            }
        }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gz
{
    class Program
    {
        static void Main(string[] args)
        {
            B b11 = new B(22,45);
            Console.WriteLine(b11.A1);
        }
    }
    class A
    {
        private int a;
        public int A1
        {
            get { return a; }
            set { a = value; }
        }
        public A(int a1)
        {
            a = a1;
            Console.WriteLine("调用构造函数A");
        }
        
        }
    
        class B:A
        {
        private int b;
        public int B1
        {
            get { return b; }
            set { b = value; }
        }
        public B(int b1,int a1):base(a1)
        {
            b = b1;

            Console.WriteLine("调用构造函数B");
        }
}
}


object万类之源

Tostring。

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 90;
            string s = i.ToString();
            Console.WriteLine(s);
        }
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            B c = new B();
            c.A = 700;
            string d = c.ToString();
            Console.WriteLine(d);
        }
    }
    class B
    {
        private int a;
        public int A
        {
            get { return a; }
            set { a = value; }
        }
        public virtual string ToString()
        {
            return "我是" + a;
        }
    }
}

 

 

posted on 2013-05-07 21:22  杨柳清枫2012  阅读(136)  评论(0)    收藏  举报

导航