c#笔记(四)

访问基类成员

应该注意:

·调用基类上已被其他方法重写的方法;

·指定创建派生类实例时应调用的基类构造函数;

·基类访问只能在构造函数,实例方法或实例属性访问器中进行;

·从静态方法中使用base关键字是错误的。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication19
{
    public class Person
    {
        protected string name;
        protected int age;
        public Person(string name, int age)
        {
            this.name = name;
            this.age = age;
            Console.WriteLine("Parent!");
        }
        public void ShowInfo()
        {
            Console.Write("name:{0}\t age:{1}\t", name, age);
        }
    }

    public class Student : Person
    {
        private string studentID;
        public Student(string studentID, string name, int age)
            : base(name, age)
        {
            this.studentID = studentID;
            Console.WriteLine("Child!");
        }
        new public void ShowInfo()
        {
            Console.WriteLine("Student");
            base.ShowInfo();
            Console.WriteLine("studentID:{0}", studentID);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student("101000", "Ding", 23);
            student.ShowInfo();
        }
    }
}



override关键字

override关键字用于修改方法,具有override关键字修饰符的方法是对基类中同名方法的新实现,基类中的同名方法必须声明为virtual或abstract类型。给基类的方法添加virtual关键字表示可以在派生类中重写它的实现。当我们要扩展或修改继承的方法、属性、索引器或事件的抽象实现或虚实现时,必须使用override修饰符。在使用override关键字时,应该注意以下几点:

· 不能重写非虚方法或静态方法。重写的基方法必须是virtual、abstract或override的。

·override声明不能更改virtual方法的可访问性。override方法和virtual方法必须具有相同的访问级别修饰符。

·不能使用修饰符new、static、virtual或abstract来修改override方法。

·重写属性声明必须指定与继承属性完全相同的访问修饰符、类型和名称,并且被重写的属性必须是virtual、abstract或override。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication19
{
    public class Employee
    {
        public string name;
        protected decimal basepay;
        public Employee(string name, decimal basepay)
        {
            this.name = name;
            this.basepay = basepay;
        }
        public virtual decimal CalculatePay()
        {
            return basepay;
        }
    }
    public class EmployeeClass : Employee
    {
        private decimal pay;
        public EmployeeClass(string name, decimal basepay, decimal pay)
            : base(name, basepay)
        {
            this.pay = pay;
        }
        public override decimal CalculatePay()
        {
            return pay + basepay;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Employee n1 = new Employee("Ding", 1000);
            EmployeeClass n2 = new EmployeeClass("ZAN", 1000, 100);
            Console.WriteLine("{0}\t{1}", n2.name, n2.CalculatePay());
            Console.WriteLine("{0}\t{1}", n1.name, n1.CalculatePay());
        }
    }
}

输出:

ZAN 1100

Ding 1000



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public void Polygon_Trans4(IFeatureClass pFeatureCls, string strPath, string strFilename, ProgressBar pBar) { if (pFeatureCls == null || strPath == "" || strFilename == "") return; createNewShape(strPath, strFilename, pFeatureCls); IFeatureCursor pFeatCursor = pFeatureCls.Search(null, false); IFeature pFeature = pFeatCursor.NextFeature(); //打开目标图层 IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass(); IWorkspace pWS = pWSF.OpenFromFile(strPath, 0); IFeatureWorkspace pFWS = (IFeatureWorkspace)pWS; IFeatureClass pTargetFClass = pFWS.OpenFeatureClass(strFilename); IWorkspaceEdit pWSEdit = pWS as IWorkspaceEdit; pWSEdit.StartEditOperation(); pWSEdit.StartEditing(false); ITable pTble = (ITable)pFeatureCls; int lCunt = pTble.RowCount(null); pBar.Visible = true; pBar.Minimum = 0; pBar.Maximum = lCunt; pBar.Step = 1; IFeature pNewFeature; IPolygon pOutPolygon; IGeometryCollection pOutGeos; IPolygon pInPolygon; IGeometryCollection pInGeos; IPoint pOutPnt = new PointClass(); IPointCollection pOutPnts; while (pFeature != null) { pBar.PerformStep(); pNewFeature = pTargetFClass.CreateFeature(); pOutPolygon = new PolygonClass(); pOutGeos = pOutPolygon as IGeometryCollection; //获取原图形 pInPolygon = pFeature.Shape as IPolygon; pInGeos = pInPolygon as IGeometryCollection; for (int m = 0; m < pInGeos.GeometryCount; m++) { IGeometry pInGeo = pInGeos.get_Geometry(m); IPointCollection pInPnts = pInGeo as IPointCollection; pOutPnts = new PolygonClass(); object missing = Type.Missing; for (int n = 0; n < pInPnts.PointCount; n++) { IPoint pInPnt = pInPnts.get_Point(n); //转换点坐标 pOutPnt.X = ClsParas4.m_OffY + pInPnt.Y * ClsParas4.m_vb + pInPnt.X * ClsParas4.m_va; pOutPnt.Y = ClsParas4.m_OffX + pInPnt.Y * ClsParas4.m_va - pInPnt.X * ClsParas4.m_vb; pOutPnts.AddPoint(pOutPnt, ref missing, ref missing); } ISegmentCollection pSegmentCols = new RingClass(); pSegmentCols.AddSegmentCollection(pOutPnts as ISegmentCollection); pOutGeos.AddGeometry(pSegmentCols as IGeometry, ref missing, ref missing); } for (int i = 0; i < pFeature.Fields.FieldCount; i++) { if (pNewFeature.Fields.get_Field(i).Name.ToString().Equals("FID", StringComparison.CurrentCultureIgnoreCase) || pNewFeature.Fields.get_Field(i).Name.ToString().Equals("SHAPE", StringComparison.CurrentCultureIgnoreCase)) continue; pNewFeature.set_Value(i, pFeature.get_Value(i)); } pNewFeature.Shape = pOutPolygon as IGeometry; pNewFeature.Store(); pFeature = pFeatCursor.NextFeature(); } pBar.Visible = false; pWSEdit.StopEditOperation(); pWSEdit.StopEditing(true); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值