c# 基類派生類成員方法訪問

wisdomone1發表於2012-03-17

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Class1
    {
        private  int length;
        private  int weight;
        public int x;
        public int y;
        public int LENGTH
        {
            get
            {
                return length;
            }
            set
            {

                length = value < 0 ? -value : value;
            }
        }

        public int WEIGHT
        {
            get
            {
                return weight;
            }
            set
            {

                weight = value < 0 ? -value : value;
            }
        }

        public  int area;
        public Class1(int l, int w)
        {
            length = l;
            weight = w;
        }
       
        public  void SetArea()
        {
            area = length * weight;
        }
        public  int Area()
        {
            return area;
        }
    }
}

 

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;//arraylist派生於array類

namespace ConsoleApplication1
{
    class Program:Class1
    {
        int jj;
        public Program(int w, int h)
            : base(w, h)
        {

        }
       
        public int Ax()
        {
            //在派生類中的方法規(僅)可以訪問基類的public成員
            return LENGTH;
        }

        public void testbase()
        {
            jj = x * y;//在派生類方法中訪問
        }
        public int aj()
        {
            return jj;
        }
      
        static void Main(string[] args)
        {


            Program p1 = new Program(20,45);
            //派生類例項物件可以訪問基類的成員方法(公共)及屬性及欄位
            p1.SetArea();
            Console.WriteLine(p1.Area());
            Console.ReadKey();
        }
     
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-718835/,如需轉載,請註明出處,否則將追究法律責任。

相關文章